Cache == null? Is it possible?
Asked Answered
H

4

5

Is it legal to make the following statement:

if(Cache[CACHE_KEY] == null)
{
    //do something to form cache
}
else
{
    //do something else that uses cache
}

I'm not sure my program is actually functioning properly (even though it compiles) and am wondering if a cache doesn't exist is it set to null?

Huehuebner answered 6/4, 2011 at 8:38 Comment(0)
M
6

Yes, that is legal (but the question in the title is not, see below for details).

Though, it may be wise to check that the type in the cache is what you're expecting rather than having to do this check twice, such as:

//in English, the following line of code might read:
//    if the item known in the cache by the specified key is in
//    in fact of type MyExpectedReferenceType, then give me it 
//    as such otherwise, give me a null reference instead...
var myCachedInstance = Cache[key] as MyExpectedReferenceType;
if (myCachedInstance == null)
{  
    //we retrieved a reference to an instance of an MyExpectedReferenceType
}
else
{
    //oh, no - we didn't!
}

On re-reading your question though, and thinking about your program not working properly, I'm tempted to say you have bigger issues than this; how is your program not working correctly? The Cache instance itself will never be null while accessible - it is a read-only field of Page. However, your expected cached value could be null and, if this is the problem, you should be receiving a NullReferenceException - is that the case?

UPDATE:

To address your comment, check out the comments I added to the code.

Misnomer answered 6/4, 2011 at 8:42 Comment(2)
sorry, what does "Cache[key] as MyExpectedReferenceType" do?Huehuebner
haha, it's actually much simpler than I think you're thinking. all i'm trying to do is figure out if a cache exists or not and if it does exist use it...Huehuebner
M
2

Very much legal; rather its better to check for a value before performing action, especially to make sure that key has not slided-out, or expired, etc.

Monteith answered 6/4, 2011 at 8:39 Comment(1)
Better than not checking at all; or better than taking for granted that it contains a valid value.Monteith
T
2

There is a potential race condition in the code you posted:

if(Cache[CACHE_KEY] == null) 
{     
    //do something to form cache 
} 
else 
{     
    // Another thread could have removed CACHE_KEY from the Cache before you get here

}

It's better to first extract the object from the cache, then test it for null, e.g.:

MyObject cachedObject = Cache[CACHE_KEY] as MyObject;
// or MyObject cachedObject = (MyObject) Cache[CACHE_KEY]; if you know it is of type MyObject
if(cachedObject == null) 
{     
    cachedObject = ... // generate the cached object
    Cache.Insert(CACHE_KEY, cachedObject, ...);
} 

// use cachedObject
Transmogrify answered 6/4, 2011 at 11:1 Comment(0)
S
0

Yes it`s possible, Cache will always be initialize (like session and application obj) But you can check if a key in the cache is null

Symphysis answered 6/4, 2011 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.