RPM1984 RPM1984 - 1 year ago 142
ASP.NET (C#) Question

How to inspect cache policies inside System.Runtime.Caching.ObjectCache?

I'm making use of the new .NET 4.0 Caching namespace:


Now, i'm just doing some prototype/fiddling with the new API, in order to work out the best fit for the real app.

In line with that, i'm trying to create a page (ASP.NET MVC) that basically dumps out everything in the cache, particularly the following info:

  • Cache Key

  • Cache Object

  • Cache Policy (expiry date, etc)

  • Cache Dependencies (if any)

However, i can't seem to get anything except the key/object.

Here's the code i'm currently playing with:

public ActionResult Index()
var cache = MemoryCache.Default;

// i can get the list of cache keys like this:
var cacheKeys = cache.Select(kvp => kvp.Key).ToList();

// i can also get a strongly-typed "CacheItem" like this:
CacheItem item = cache.GetCacheItem("someKey");


I would have hoped the "CacheItem" class would expose the information i require (expiry, dependencies, etc - at least as "getters").

But it doesn't. All it has is properties for key, value and region name.

How can i inspect the items in the cache and spit out the information i require?

Is there a namespace/class i'm missing?


Looks like there is a ChangeMonitor class, but again - this doesn't give expiration info, it just allows you to subscribe to events when cache items are removed.

There must be a way to just grab the items in the cache, and when they expire.


Don't know if this should be a seperate question, but also - i'm confused as to what lifetime i should give my ObjectCache. MSDN says it's not a singleton, and you can in fact create multiple ObjectCache instances. What does that mean though, i have to use a fully-locked singleton when accessing the ObjectCache instance?

Answer Source

It doesn't look to me that there is a way to retrieve the CacheItemPolicy once it's been added to the cache collection.

The best way around this is can think of is to cache the policy object along with the item you want to cache but just appending "Policy" to the key name so that you can later retrieve the policy. This obviously assumes you have control over actually adding the item to the cache in the first place. Example below:

public ActionResult Index()
        string key = "Hello";
        string value = "World";

        var cache = MemoryCache.Default;
        CacheItemPolicy policy = new CacheItemPolicy();
        policy.AbsoluteExpiration = DateTime.Now.AddDays(1);
        cache.Add(new CacheItem(key, value), policy);
        cache.Add(new CacheItem(key + "Policy", policy), null);

        CacheItem item = cache.GetCacheItem(key);
        CacheItem policyItem = cache.GetCacheItem(key + "Policy");
        CacheItemPolicy policy2 = policyItem.Value as CacheItemPolicy;

        ViewBag.Message = key + " " + value;

        return View();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download