I'm trying to cache some values and my goal is to keep them until they get overwritten. So basically they should never expire.
var memoryCache = MemoryCache.Default;
if (!memoryCache.Contains("data"))
{
var timer = DateTimeOffset.UtcNow.AddMinutes(1);
var data = jsonContent;
memoryCache.Add("data", data, timer);
}
How can I work around the expiration parameter? I've read something about the CacheItemPolicy
but I didn't understand it.
Thanks for any help in advance.
Looking at the documentation it seems that you can set theSlidingExpiration
property on the CacheItemPolicy
object to NoSlidingExpiration
which seems to indicate that it will not expire, see here
A cache entry that is inserted into the cache with the NoSlidingExpiration field value set as the expiration value should never be evicted because of non-activity in a sliding time window.
So you can do something like the following:
memoryCache.Add(new CacheItem("data", data), new CacheItemPolicy
{
SlidingExpiration = System.Runtime.Caching.NoSlidingExpiration
});
Please note the code above might not compile, it's just an example on how to use it.