I'm trying to add items to the MemoryCache.Default instance using the Add method as below:
bool result= MemoryCache.Default.Add(cacheKey, dataToCache, cacheItemPolicy)
The value of result is true, indicating that the item has been added to the cache yet when I attempt to retrieve it immediately afterwards the cache is empty. I have also tried to add the item using the Set method with the same result of an empty cache.
The cache has the default 99Mb memory limit so it doesn't appear as if there is no space to add new items.
Any ideas?
private static void InsertCachedData(string cacheKey, object dataToCache, string[] dependantCacheKeys)
{
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now, new TimeSpan(hours: 0, minutes: 0, seconds: 3600));
if (dependantCacheKeys != null && dependantCacheKeys.Length > 0)
{
cacheItemPolicy.ChangeMonitors.Add(MemoryCache.Default.CreateCacheEntryChangeMonitor(dependantCacheKeys));
}
MemoryCache.Default.Add(cacheKey, dataToCache, cacheItemPolicy);
logger.DebugFormat("Cache miss for VehiclesProvider call with key {0}", cacheKey);
}
Refer to the following example to see how to initialize the cacheItemPolicy class and add a time value to store the data in the cache. var cacheItemPolicy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset. Now. AddSeconds(120.0) }; var result = cache.
Note that the MemoryCache is a singleton, but within the process. It is not (yet) a DistributedCache. Also note that Caching is Complex(tm) and that thousands of pages have been written about caching by smart people.
In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you're running the same process on several servers, you will have a separate cache for each server. Persistent in-process Cache is when you back up your cache outside of process memory.
The Core 2.2 IMemoryCache is in theory thread safe. But if you call GetOrCreateAsync from multiple threads the factory Func will be called multiple times. Which could be a bad thing. A very simple fix to this is using a semaphore.
You're not setting the AbsoluteExpiration
property correctly.
The TimeSpan
argument that you pass to the DateTimeOffset
constructor should be the offset from UTC of the passed DateTime
value, not some arbitrary timespan that you want to add to generate your offset. You're passing in 3600 seconds -- ie, one hour -- which is working purely by coincidence because, presumably, you're based in the UK where BST is currently one hour ahead of UTC.
You're passing DateTime.Now
as the DateTime
argument, so what you're effectively doing is setting the cached item to expire immediately.
If you want your cached item to live for an hour then set the expiration like this:
cacheItemPolicy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddHours(1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With