Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check when an item in MemoryCache will expire?

Is it possible to read the expiration time of an item in MemoryCache?

I'm using the .NET System.Runtime.Caching.MemoryCache to store my configuration information for 30 min before I reload it from the database. As part of a status page I would like to show how old a specific cache item is or when it will expire.

object config = LoadSomeStuffFromDatabase();
CacheItemPolicy cachePolicy = new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddMinutes(30) }; 
Cache.Add("MyConfigKey", config, cachePolicy);

// And now I want to do something like this :-)
DateTime dt = Cache.SomeMagicMethod("MyConfigKey");

Today I'm using a variable that is updated every time I reload the configuration. But it would be much nicer to read this information from the cache itself.

like image 716
Mattias Lindberg Avatar asked Nov 13 '15 13:11

Mattias Lindberg


1 Answers

I believe that this has already been answered:

How to get expiry date for cached item?

If that isn't what you're looking for, consider the following:

The API doesn't support getting the policy back from the retrieval of the cached item. Since you can cache any object, you could cache the policy in conjunction with the object and do some time based comparisons when you need to later access the object.

like image 200
David Pine Avatar answered Sep 18 '22 14:09

David Pine