1

I'd like to be able to determine the age of an item in the HttpRuntime cache and was wondering if there was any way to do this. Basically what I in my class is parse a third party XML file into an object and then store the object in the cache. Rather than setting an expiration on the object in the cache though, I'd rather try to pull the updated XML when the object needs to be refreshed so that I can keep my cached object if the parser fails. I'm also open to ideas if anyone has an idea of how to accomplish this a different/better way.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
Kyle
  • 4,261
  • 11
  • 46
  • 85

2 Answers2

2

You could create a key that corresponds to the objects key with "_Date" or some other suffix

public object MyProperty
{
    get { return HttpContext.Cache["MyKey"] as object; }
    set 
    {
        HttpContext.Cache["MyKey"] = value;
        MyPropertyDate = DateTime.Now;
    }
}

public DateTime MyPropertyDate
{
    get { return HttpContext.Cache["MyKey_Date"] as DateTime; }
    set { HttpContext.Cache["MyKey_Date"] = value; }
}
hunter
  • 62,308
  • 19
  • 113
  • 113
1

From your description, it sounds like you should look at the CacheItemUpdateCallback delegate.

If you use this, you can be notified before your item is removed from the cache.

So you can attempt to regenerate the object from your updated XML, and if parsing fails, reinsert the original object.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • I haven't had a chance to try this out yet, but thanks for this tip. This sounds like a better solution. – Kyle Nov 16 '10 at 15:50