views:

268

answers:

4

1 - If I insert to Cache by assigning the value:

Cache["key"] = value;

what's the expiration time?

2 - Removing the same 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.

+2  A: 
  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")

Dror
Important note: Using Cache[key]=null will throw an exception when used under stress, even though it works fine under normal conditions. Cache.Remove() doesn't explode under stress.
Nir
A: 
  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

lomaxx
1. Thanks. 2. OK, but what about the removal itself? What's the best practice in that case - Assigning null or Remove() ?
Nir
A: 
  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

Veer
+3  A: 

1 Cache["key"] = value is equal to Cahce.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 Cahce.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

bniwredyc
in addition, it appears that removing from cache using Cache["key"]=null will cause your application to crash under stress. So I'd recommend Remove().
Nir
Cache["key"]=null will throw an ArgumentNullException - this is nothing to do with "stress".
Joe