开发者

C# - Inserting and Removing from Cache

开发者 https://www.devze.com 2022-12-31 04:13 出处:网络
If I insert to Cache by assigning the value: Cache[\"key\"] = value; what\'s the expiration time? Removing the sam开发者_如何学Goe value from Cache:
  1. If I insert to Cache by assigning the value:

    Cache["key"] = value;

    what's the expiration time?

  2. Removing the sam开发者_如何学Goe value from Cache:

    I want to check if the value is in Cache by if(Cache["key"]!=null), is it better to remove it from Cache by Cache.Remove("key") or Cache["key"]=null ?

-- Edit --

After having tried Cache.Remove and Cache["key"]=null, DO NOT USE Cache["key"]=null, as it will throw exceptions when used in stress.


1 Cache["key"] = value is equal to Cache.Insert("key", value)

MSDN Cache.Insert - method (String, Object):

This method will overwrite an existing cache item whose key matches the key parameter. The object added to the cache using this overload of the Insert method is inserted with no file or cache dependencies, a priority of Default, a sliding expiration value of NoSlidingExpiration, and an absolute expiration value of NoAbsoluteExpiration.

2 It's better to remove values from cache by Cache.Remove("key"). If you use Cache["key"] = null it's equal to Cache.Insert("key", null). Take a look at the Cache.Insert implementation:

public void Insert(string key, object value)
{
    this._cacheInternal.DoInsert(true, key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, true);
}

and CacheInternal.DoInsert:

internal object DoInsert(bool isPublic, string key, object value, CacheDependency dependencies, DateTime utcAbsoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, bool replace)
{
    using (dependencies)
    {
        object obj2;
        CacheEntry cacheKey = new CacheEntry(key, value, dependencies, onRemoveCallback, utcAbsoluteExpiration, slidingExpiration, priority, isPublic);
        cacheKey = this.UpdateCache(cacheKey, cacheKey, replace, CacheItemRemovedReason.Removed, out obj2);
        if (cacheKey != null)
        {
            return cacheKey.Value;
        }
        return null;
    }
}

Compare it to Cache.Remove:

public object Remove(string key)
{
    CacheKey cacheKey = new CacheKey(key, true);
    return this._cacheInternal.DoRemove(cacheKey, CacheItemRemovedReason.Removed);
}

CacheInternal.DoRemove:

internal object DoRemove(CacheKey cacheKey, CacheItemRemovedReason reason)
{
    object obj2;
    this.UpdateCache(cacheKey, null, true, reason, out obj2);
    return obj2;
}

And finally Cache.Remove("key") is much more readble than Cache["key"] = null


  1. Add it to cache using The add an item to the cache with expiration policies to specify exact time frame.
    As for the default, see in ASP.NET Caching: Techniques and Best Practices - the section titled Storing Data in the Cache specifies:

    Cache["key"] = "value";

    This will store the item in the cache without any dependencies, so it will not expire unless the cache engine removes it in order to make room for additional cached data.

  2. As the cache takes an object - Null is an object! if you want an entry with value of null, use Cache["key"]=null if you want no entry by the name "key" use Cache.Remove("key")


  1. When no expiration time is specified NoSlidingExpiration and NoAbsoluteExpiration is set where NoAbsoluteExpiration is the largest possible DateTimeValue. Hence it stays there forever till it is removed

  2. Better remove the cache


  1. I believe that by default there is no default cache expiry time so I believe it stays in there until the app pool recycles.

  2. This is the only way I know of to check if an item exists in cache

0

精彩评论

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