企业库的文件说:我的IBackingStore需要线程安全吗?
因为Cache对象进行操作,保证您 任何后备存储将被称为在单线程方式 的方式。这 意味着你不必让你的 实现线程安全。
以及有关的CacheManager:
通过 的CacheManager对象进行的每一个方法调用是线程安全的。
但是一个简单的测试证明相反:
这里是我的自定义备份存储(仅Add方法是相关的)
public class MyStore : IBackingStore
{
volatile bool isEntered = false;
#region IBackingStore Members
public void Add(CacheItem newCacheItem)
{
if(isEntered)
throw new NotImplementedException();
isEntered = true;
Thread.Sleep(1000);
isEntered = false;
}
public int Count
{
get
{
throw new NotImplementedException();
}
}
public void Flush()
{
throw new NotImplementedException();
}
public System.Collections.Hashtable Load()
{
return new System.Collections.Hashtable();
}
public void Remove(string key)
{
throw new NotImplementedException();
}
public void UpdateLastAccessedTime(string key, DateTime timestamp)
{
throw new NotImplementedException();
}
#endregion
#region IDisposable Members
public void Dispose()
{
throw new NotImplementedException();
}
#endregion
}
,这里是一个测试至极访问相同的CacheManager通过两个不同的线程:
DictionaryConfigurationSource configSource = new DictionaryConfigurationSource();
CacheManagerSettings cacheSettings = new CacheManagerSettings();
configSource.Add(CacheManagerSettings.SectionName, cacheSettings);
CacheStorageData storageConfig = new CacheStorageData("MyStorage", typeof(MyStore));
cacheSettings.BackingStores.Add(storageConfig);
CacheManagerData cacheManagerData = new CacheManagerData("CustomCache", 120, 100, 5, storageConfig.Name);
cacheSettings.CacheManagers.Add(cacheManagerData);
cacheSettings.DefaultCacheManager = cacheManagerData.Name;
CacheManagerFactory cacheFactory = new CacheManagerFactory(configSource);
ICacheManager cacheManager = cacheFactory.CreateDefault();
Thread thread = new Thread(() =>
{
cacheManager.Add("item1", "odaiu");
});
thread.Start();
cacheManager.Add("item2", "dzaoiudoiza");
Add方法在两个不同的线程中执行两次(因为它会抛出Add方法的“NotImplementedException”)。
我的代码或企业库的文档有问题吗?