jamesmontemagno / monkey-cache

Easily cache any data structure for a specific amount of time in any .NET application.
MIT License
670 stars 106 forks source link

Expiration time fails after about 6050 days from now #102

Open Qythyx opened 3 years ago

Qythyx commented 3 years ago

@jamesmontemagno

In FileStore.Barrel the DateTimeToEpochSeconds converts the expiration date to second from epoch in seconds as an int. This overflows in about 6050 days from now and becomes int.MinValue, which means it is considered expired. This int should probably be a long instead.

If I have time I'll try to patch this, but I wanted to mention this. Also, I didn't look at the other implementations, only FileStore.

In case you're wondering, I found this problem by trying to set a very distant expiration date. Initially I tried TimeSpan.MaxValue but that had this problem. I then tried TimeSpan.FromDays(9999) and had the same problem. Then I looked at the code and found the problem. I also think that TimeSpan.MaxValue should possibly be considered special and never expire.

Qythyx commented 3 years ago

Ok, I won't be able to patch this. I'm on a Mac and the project is super unhappy opening it in Visual Studio for Mac or VSCode on Mac.

Looking at the code I think just changing a few ints to longs maybe enough, but I would prefer to write a test to exercise this.

Jeanjean commented 2 years ago

Could this also be causing a System.ArgumentOutOfRangeException when calling Barrel.Current.Add(key, value, TimeSpan.MaxValue);? The error message is: The added or subtracted value results in an un-representable DateTime. Parameter name: value'. Looks like a regression of #30.

Changing the call to Barrel.Current.Add(key, value, TimeSpan.FromDays(6000)); seems to work as a workaround.