缓存==空?是否可以?

发布于 2024-10-30 08:47:05 字数 211 浏览 3 评论 0原文

做出以下声明是否合法:

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

我不确定我的程序实际上是否正常运行(即使它编译)并且想知道缓存是否不存在是否设置为空?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万劫不复 2024-11-06 08:47:05

是的,这是合法的(但标题中的问题不合法,请参阅下文了解详细信息)。

不过,检查缓存中的类型是否是您期望的类型可能是明智的,而不是必须执行两次此检查,例如:

//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!
}

在重新阅读您的问题时,并考虑您的程序无法正常工作好吧,我很想说你有比这更大的问题; 如何您的程序无法正常运行? Cache 实例本身在可访问时永远不会为 null - 它是 Page 的只读字段。但是,您预期的缓存值可能为 null,如果这是问题所在,您应该收到 NullReferenceException - 是这样吗?

更新:

要解决您的评论,请查看我添加到代码中的评论。

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.

心房的律动 2024-11-06 08:47:05

非常合法;相反,在执行操作之前最好检查值,尤其是确保密钥没有滑出或过期等。

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.

烂柯人 2024-11-06 08:47:05

您发布的代码中存在潜在的竞争条件:

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

}

最好首先从缓存中提取对象,然后测试它是否为空,例如:

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

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
别想她 2024-11-06 08:47:05

是的,这是可能的,缓存将始终被初始化(如会话和应用程序对象)
但你可以检查缓存中的某个键是否为空

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文