简体   繁体   English

HttpContext.Current.Cache是​​线程安全的吗?

[英]Is HttpContext.Current.Cache thread-safe ?

Please check the code below: 请检查以下代码:

objDDLTable = HttpContext.Current.Cache["TestSet"] as Hashtable;

if (objDDLTable == null)
{
   objDDLTable = new Hashtable();
   arrDDLItems = GetDropDownList("testDropDown");
   objDDLTable.Add("testDropDown", arrDDLItems);
   HttpContext.Current.Cache["TestSet"] = objDDLTable;
}
else if (objDDLTable != null && !objDDLTable.Contains("testDropDown"))
{
   arrDDLItems = GetDropDownList("testDropDown");
   objDDLTable.Add("testDropDown", arrDDLItems);
   HttpContext.Current.Cache["TestSet"] = objDDLTable;
}
else
{
   arrDDLItems = objDDLTable["testDropDown"] as DdlItem[];
}

The code, as you can infer, is basically to cache some values for a dropdown list on a web page. 正如您可以推断的那样,该代码基本上是为网页上的下拉列表缓存一些值。

First, it tries to read a HashTable object from the cache, then checks if the specific key exists in HashTable object read from the cache. 首先,它尝试从缓存中读取HashTable对象,然后检查从缓存中读取的HashTable对象中是否存在特定键。 If it does, the value (array of items) is read, else, it reads the array from the source and adds a new key in the HashTable, which is then stored back to the cache for subsequent usage. 如果是,则读取值(项目数组),否则,它从源读取数组,并在HashTable中添加新键,然后将其存储回缓存以供后续使用。

This works fine in most cases, however, we have got the following error occassionally : 在大多数情况下,此方法都可以正常工作,但是,有时会出现以下错误:

System.ArgumentException: Item has already been added. 
Key in dictionary: 'testDropDown' Key being added: 'testDropDown' at 
System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) at 
System.Collections.Hashtable.Add(Object key, Object value)

Logically, there should never be a case when the system is trying to add the key testDropDown in the HashTable when its already present, the first else condition should not allow it. 从逻辑上讲,永远不要有系统尝试在已存在的HashTable中添加键testDropDown时,第一个else条件不应允许它。

The only thing that comes to my mind is a possiblility of another thread adding the key to the HashTable at a time when the condition check has failed on the first thread and it also attemps to add the key. 我唯一想到的是,当第一个线程的条件检查失败时,另一个线程可能会将密钥添加到HashTable中,并且它还会尝试添加密钥。

In my knowledge, Cache is a thread safe static object, but I can't think of anything else causing that error. 就我所知, Cache是线程安全的静态对象,但是我想不出其他导致该错误的原因。 Can you guys please help me identify the cause here? 你们能帮我在这里找到原因吗?

The HttpContext.Current.Cache object itself is thread safe meaning that storing and reading from it are thread safe but obviously objects that you store inside might not be thread safe. HttpContext.Current.Cache对象本身是线程安全的,这意味着对其进行存储和读取是线程安全的,但是显然,存储在其中的对象可能不是线程安全的。 In your case you are storing a Hashtable which is not thread safe object meaning that this instance could potentially be shared between multiple threads and there could be concurrent read and writes to the hashtable and this could lead to problems. 在您的情况下,您存储的Hashtable不是线程安全的对象,这意味着该实例可能在多个线程之间共享,并且可能对Hashtable进行并发读取和写入,这可能会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM