开发者

Determine the age of an object in the cache

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开发者_如何学JAVA if anyone has an idea of how to accomplish this a different/better way.


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; }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜