开发者

Expiring a cached item manually/immediately

I'd like to place an item in the ASP.NET cache, say for instance a dataset, and not have it expire at all, until an event occurs in my application that requires 开发者_如何转开发the item to be refreshed. But until that happens, it should not expire, ever.

So a) is it possible to set a cached item never to expire (apart from setting expiry say 1 year in the future), and b) how does one manually force an item to expire?

Thanks!


I'm thinking this will work for you. Then when you're ready to refresh the item, you remove it form the cache manually and set it again...

Page.Cache.Add("object", 
            "something", 
            null, 
            System.Web.Caching.Cache.NoAbsoluteExpiration, 
            System.Web.Caching.Cache.NoSlidingExpiration, 
            CacheItemPriority.NotRemovable, 
            new CacheItemRemovedCallback((s, o, r) =>
            {
                // some callback code if you want...
            }));

UPDATED (better demo):

    private int _counter = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        // You can add the cache key like this
        AddToCache("key", () => "some object " + _counter++);

        //Any time you want to refresh the value, you can call RefreshCachedValue
        RefreshCachedValue("key");
        RefreshCachedValue("key");
        RefreshCachedValue("key");
        RefreshCachedValue("key");
        RefreshCachedValue("key");
        // In this demo, the cached value is now "some object 5"
    }

    private void AddToCache(string key, Func<object> getValueFunction)
    {
        Page.Cache.Add(key,
            getValueFunction(), 
            null, 
            System.Web.Caching.Cache.NoAbsoluteExpiration, 
            System.Web.Caching.Cache.NoSlidingExpiration, 
            CacheItemPriority.NotRemovable, 
            new CacheItemRemovedCallback((s, o, r) =>
            {
                AddToCache(s, getValueFunction);
            }));
    }

    private void RefreshCachedValue(string key)
    {
        Page.Cache.Remove(key);
    }


is it possible to set a cached item never to expire (apart from setting expiry say 1 year in the future)

Sort of - the Insert method has an overload that supports this: (reference)

DataSet myDataSet = getDataSet();

Page.Cache.Insert("MyDataSetCacheKey", myDataSet)

This will add the object to the cache with no sliding expiration and no absolute expiration, however it uses the default priority, not NotRemovable. If you wanted to force that, you'd have to write an extension method for Insert yourself.

how does one manually force an item to expire?

I'm assuming that you mean here 'I've cached this data forever, but now I want to change it'. In which case, you wouldn't expire it, you'd just remove it from the cache:

Page.Cache.Remove("MyDataSetCacheKey")

Logically, there's no difference between the item being removed from the cache because it expired, was flushed by the server trying to scavenge memory or you removing it manually.


There are some kinds of CacheDependency, you can use FileBased CacheDependency referring to an empty file.

If your data changes, just overwrite the file. As soon as the file changes, the cache will be reset.

http://msdn.microsoft.com/en-us/magazine/cc163955.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜