Hi,
To check if the key already exists in cache I shoulde be able to do the following :
if(Cache["MyKey"] != null)
This does however not work? If I create an instance from the Cache class i will be able to get the object this way :
cache.Get("MyKey") or cache["MyKey"]
But even if I check for null like this :
if(cache["MyKey"] != null)
It will throw an NullRefException?
What am I doing wrong?
Edit1 :
This is how I instansiate the cache
private Cache cache
{
get {
if (_cache == null)
_cache = new Cache();
return _cache; }
}
Checking for a null value is how to test whether an object for a certain key is in the Cache. Therefore,
is correct.
However, you should not instantiate a new Cache object. You may use
System.Web.HttpContext.Current.Cacheinstead. This is the instance of the Cache and lives in the application domain.From MSDN: