Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MemoryCache.Add returns true but does not add item to cache

Tags:

c#

.net

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);
    }
like image 916
Jason Avatar asked May 19 '11 13:05

Jason


People also ask

How do I initialize memory cache?

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.

Is MemoryCache a singleton?

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.

How does MemoryCache work C#?

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.

Is IMemoryCache thread safe?

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.


1 Answers

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));
like image 148
LukeH Avatar answered Oct 09 '22 19:10

LukeH