多线程缓存

时间:2014-04-02 08:18:27

标签: c# asp.net multithreading caching

我正在尝试为C#可移植库(.Net 4及更高版本(Asp.net MVC,Winodws 8,Widows Phone 8,Silver light,将来可能用于WPF)实现自定义缓存,低延迟代码,将是多线程缓存,尝试在单例上实现。怎么可以

如何使我的实现单例和线程安全。

interface  ICustomCache
{
    bool IsFound(string key, out value); //returns true if found the object
    void Set(string key, object value); //If there is already an object with such a key, then the set should replace the old object
}


 public class Cache : ICustomCache
    {
       private readonly Dictionary<string, object> _cacheDictionary = new Dictionary<string, object>();
       public bool IsFound(string key, out object value)
        {
            if (_cacheDictionary.ContainsKey(key))
            {

                return _cacheDictionary.TryGetValue(key, out value);

            }
           value = null;
           return false;
        }

        public void SetCachedObject(string key, object value)
        {
            if (_cacheDictionary.ContainsKey(key))
            {
                _cacheDictionary.Remove(key);
                _cacheDictionary.Add(key,value);
            }
            else
            {
                _cacheDictionary.Add(key, value);

            }
        }
    }

2 个答案:

答案 0 :(得分:2)

尝试以下代码使您的缓存管理器单例和线程安全

/// <summary>
/// Custom cache interface
/// </summary>
interface ICustomCache
{
    bool IsFound(string key, out object value); //returns true if found the object
    void Set(string key, object value); //If there is already an object with such a key, then the set should replace the old object
}

/// <summary>
/// In memory cache implementation
/// </summary>
public class CustomCache : ICustomCache
{
    // use thread safe dictionary
    private readonly ConcurrentDictionary<string, object> _cacheDictionary = new ConcurrentDictionary<string, object>();

    public bool IsFound(string key, out object value)
    {
        if (_cacheDictionary.ContainsKey(key))
        {
            return _cacheDictionary.TryGetValue(key, out value);
        }
        value = null;
        return false;
    }

    public void Set(string key, object value)
    {
        if (_cacheDictionary.ContainsKey(key))
        {
            object dummy;
            if (_cacheDictionary.TryRemove(key, out dummy))
            {
                _cacheDictionary.TryAdd(key, value);
            }
        }
        else
        {
            _cacheDictionary.TryAdd(key, value);
        }
    }
}

/// <summary>
/// Cache manager
/// </summary>
public static class CacheManager
{
    private static ICustomCache _cache = null;

    static CacheManager()
    {
        // alternatly use Ioc container like Unity to create the object
        _cache = new CustomCache();
    }

    public bool IsFound(string key, out object value)
    {
        return _cache.IsFound(key, out value);
    }

    public void Set(string key, object value)
    {
        _cache.Set(key, value);
    }
}

// usage
CacheManager.Set("test1", "hello world!");

这是一个非常简单的实现,为了完整起见,您可能需要考虑添加逐出策略来控制缓存的对象增长/应用程序内存,缓存统计信息等,

答案 1 :(得分:0)

那里有很多假设和许多方法,下面只是一个代码片段,记下希望给你一些方向: -

   interface  ICustomCache
   {
       bool IsFound(string key, out object value); 
        void Set(string key, object value); the set should replace the old object
    }
    public class SQLCustomCache:ICustomCache
    {
        #region ICustomCache Members

        public bool IsFound(string key, out object value)
        {
            "Your Implementation for cheking in SQl";
        }

        public void Set(string key, object value)
        {
            "Your Implementation for Storing it in SQl";
        }

        #endregion
    }
    public class FileSystemCustomCache : ICustomCache
    {
        #region ICustomCache Members

        public bool IsFound(string key, out object value)
        {
            "Your Implementation for cheking  in FileSystem";
        }

        public void Set(string key, object value)
        {
            "Your Implementation for Storing it in FileSystem";
        }

        #endregion
    }
    public class CustomCache
    {
        static ICustomCache mycacheObject = null;

        #region ICustomCache Members
        private CustomCache()
        { 

        }

        public ICustomCache GetCustomCacheInstance(string Type)
        {
            if(mycacheObject==null)
            {
                if (Type == "SQL")
                {
                    mycacheObject = new SQLCustomCache();
                }
                else
                {
                    mycacheObject = new FileSystemCustomCache();
                }
            }
            return mycacheObject;

        }        
    }