这个实现线程安全吗?

时间:2016-10-07 08:34:18

标签: c# caching

我有以下课程:

public interface IApplicationEncryptionService
{
    string Encrypt(string message);

    string Decrypt(string cipher);
}

public class ApplicationEncryptionService : IApplicationEncryptionService
{
    private IApplicationEncryptionCacheHelper m_cacheHelper;

    public ApplicationEncryptionService(IApplicationEncryptionCacheHelper cacheHelper = null)
    {
        if (cacheHelper == null)
            m_cacheHelper = new ApplicationEncryptionCacheHelper();

    }

    public string Encrypt(string input)
    {
        //Encrypt
    }

    public string Decrypt(string input)
    {
        //Decrypt
    }
}

public interface IApplicationEncryptionCacheHelper
{
    void Add<T>(string key, T objectToCache) where T : class, new();
    T Get<T>(string key) where T : class, new();
    void Remove(string key);
}


class ApplicationEncryptionCacheHelper : IApplicationEncryptionCacheHelper
{
    private const string CACHE_NAME = "EncryptionCache";

    private static CacheProvider m_CacheProvider = new CacheProvider();     
    private CacheNameConfiguration m_defaultCacheConfiguration;

    public ApplicationEncryptionCacheHelper()
    {
        m_defaultCacheConfiguration = new CacheConfiguration
        {
            IsRemovable = true,
            ExpirationType = CacheNameConfiguration.ExpirationOption.Sliding,
            ExpirationInterval = new TimeSpan(0, 60, 0)
        };
    }

    public void Add<T>(string key, T objectToCache) where T : class, new()
    {
        m_CacheProvider.Add(CACHE_NAME, key, objectToCache, m_defaultCacheConfiguration);
    }

    public T Get<T>(string key) where T : class, new()
    {
        var itemFromCache = m_CacheProvider.Get(CACHE_NAME, key);
        return (T)itemFromCache;
    }

    public void Remove(string key)
    {
        m_CacheProvider.Remove(CACHE_NAME, key);
    }
}

class CacheProvider
{
    private ConcurrentDictionary<string, MemoryCache> m_localCacheLookup = new ConcurrentDictionary<string, MemoryCache>();

    public override void Add(string cacheName, string key, object itemToCache, CacheNameConfiguration cacheNameConfiguration)
    {
        MemoryCache cacheManager = GetCacheManager(cacheName);

        CacheItemPriority cacheItemPriority = cacheNameConfiguration.IsRemovable ? CacheItemPriority.Default : CacheItemPriority.NotRemovable;
        CacheItemPolicy policy;
        if (cacheNameConfiguration.ExpirationType == CacheNameConfiguration.ExpirationOption.Sliding)
        {
            policy = new CacheItemPolicy { SlidingExpiration = cacheNameConfiguration.ExpirationInterval, Priority = cacheItemPriority };
        }
        else
        {
            policy = new CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.UtcNow, cacheNameConfiguration.ExpirationInterval), Priority = cacheItemPriority };
        }

        cacheManager.Add(key, itemToCache, policy);
    }

    public override object Get(string cacheName, string key)
    {
        MemoryCache cacheManager = GetCacheManager(cacheName);

        return cacheManager.Get(key);
    }

    public override void Remove(string cacheName, string key)
    {
        MemoryCache cacheManager = GetCacheManager(cacheName);

        cacheManager.Remove(key);
    }

    #endregion

    #region Private Methods

    private MemoryCache GetCacheManager(string cacheName)
    {
        return m_localCacheLookup.GetOrAdd(cacheName, (cacheKey) =>
        {
            NameValueCollection config = new NameValueCollection();
            config.Add("pollingInterval", "00:30:00");
            return new MemoryCache(cacheName, config);
        });
    }
}

ApplicationEncryptionService将由不同的线程使用,并使用ApplicationEncryptionCacheHelper来缓存经常使用的数据。

ApplicationEncryptionCacheHelper包含一个声明为私有静态变量的CacheProvider实例。它是线程安全的实现吗?

0 个答案:

没有答案