开发者

Does Cache.Insert override the previous AbsoluteExpiration?

If i have an item that already exists in my ASP.NET Cache ... and I just wish to update the value of that cached item .. not the Absolute Expiry value, or Cache Dependencies, etc.. nothing else BUT the value ... can I use Cache.Insert?

If not, is there anyway I can retrieve all those values for the cached item .. and then re-use them开发者_如何学Python when I do the Cache.Insert?

Cheers :)


You can create functions to handle adding fresh value or updating the existing one as follows:

private static Cache cachingControl;

public void UpdateToCache(object key, object updateValue)
{
    try
    {
        if (key != null)
        {
            cachingControl.Remove(key);
            AddToCache(key, updateValue);
        }
    }
    catch (Exception ex)
    {
        //**ToDo[Logging]** Code for logging
    }
}

public void AddToCache(object key, object saveValue)
{
    try
    {
        if (key != null)
        {
            cachingControl.Insert(key, saveValue,null,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(30));
        }
    }
    catch (Exception ex)
    {
        //**ToDo[Logging]** Code for logging
    }
}

Here you can use AddToCache function to insert new values and UpdateToCache function to update new values to existing key.(This basically involves in removing the existing key and adding it again with updated values.)

There is no direct way to update the existing values.


The Cache.Insert overload that takes just the key and object will simply use default values for the caching behaviour.

From MSDN:

Inserts an item into the Cache object with a cache key to reference its location, using default values provided by the CacheItemPriority enumeration.

You'd be best to create your own helper class to store the values for you as I don't believe there's a way to get at the cached item's behavioural properties.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜