我注意到GetOrAdd()始终执行工厂委托,即使该值存在于字典中。例如:ConcurrentDictionary.GetOrAdd始终执行委托方法
class Program
{
private static ConcurrentDictionary<string, string> _cache = new ConcurrentDictionary<string, string>();
static void Main(string[] args)
{
string value;
value = GetValueFromCache("A"); // cache is empty, CacheValueFactory executes, A is added
value = GetValueFromCache("A"); // cache contains A, CacheValueFactory executes
value = GetValueFromCache("C"); // cache contains A, CacheValueFactory, C is added
value = GetValueFromCache("A"); // cache contains A and C, CacheValueFactory executes
}
private static string GetValueFromCache(string key)
{
string val = _cache.GetOrAdd(key, CacheValueFactory(key));
return val;
}
private static string CacheValueFactory(string key)
{
if (key == "A")
return "Apple";
else if (key == "B")
return "Banana";
else if (key == "C")
return "Cherry";
return null;
}
}
第一次调用GetValueFromCache(“A”)时,缓存为空,并且A:Apple被添加。加入调试器,我注意到在对GetValueFromCache(“A”)的第二次和第三次调用中,CacheValueFactory()方法始终执行。这是预期的吗?如果密钥存在于字典中,我会认为委托方法不会执行。
Ooops。你是对的:) – Bullines 2010-12-09 16:05:40