【问题标题】:Multi threaded Cache多线程缓存
【发布时间】:2014-05-13 09:07:35
【问题描述】:

我正在尝试为 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);

            }
        }
    }

【问题讨论】:

  • 这是作为练习吗?如果没有,您可以重用现有的实现,例如 Guava 的 LoadingCache。还有,为什么 c# Java?
  • 建议堆栈溢出,这不是练习,我正在为可移植库实现低延迟代码
  • 好吧,到目前为止你已经有了一个界面。对于实施:您尝试过什么?另外:可移植类库是模棱两可的:它需要支持哪些框架?这可能很重要。

标签: c# asp.net multithreading caching


【解决方案1】:

试试下面的代码,让你的缓存管理器成为单例和线程安全的

/// <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!");

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

【讨论】:

    【解决方案2】:

    好吧,有很多假设和很多方法可以做,下面只是一个记下的代码 sn-p 希望能给你一些方向:-

       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;
    
            }        
        }
    

    【讨论】:

      猜你喜欢
      • 2011-09-11
      • 2013-04-24
      • 2012-07-01
      • 2012-07-09
      • 2021-07-10
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多