zoukankan      html  css  js  c++  java
  • .net简单的缓存分片

    实现一个内存里面放入三个字典实现字典分片

      static CacheCutting()
            {
                _DictionaryList = new List<Dictionary<string, object>>();
                _ObjectList = new List<object>();
                _DictionaryList.Add(_KeyValues1);
                _DictionaryList.Add(_KeyValues2);
                _DictionaryList.Add(_KeyValues3);
                //加线程锁
                for (int i = 0; i < _DictionaryList.Count; i++)
                {
                    _ObjectList.Add(new object());
                }
            }
            private static Dictionary<string, object> _KeyValues1 = new Dictionary<string, object>();
            private static Dictionary<string, object> _KeyValues2 = new Dictionary<string, object>();
            private static Dictionary<string, object> _KeyValues3 = new Dictionary<string, object>();
    
            private static List<Dictionary<string, object>> _DictionaryList;
            private static List<object> _ObjectList;
            public static void SetValue(string key, object value)
            {
    
                int hashKey = key.GetHashCode();
                int index = GetInt(key);
                lock (_ObjectList[index])
                    _DictionaryList[index].Add(key, value);
            }
            public static object GetValue(string key)
            {
                object result = null;
                int hashKey = key.GetHashCode();
                int index = GetInt(key);
                lock (_ObjectList[index])
                {
                    if (_DictionaryList[index].ContainsKey(key))
                        result = _DictionaryList[index][key];
                }
                return result;
            }
            private static int GetInt(string key)
            {
                int hashKey = key.GetHashCode();
                int hashList = hashKey % _DictionaryList.Count;
                return Math.Abs(hashList);
            }
    

      

  • 相关阅读:
    Pagodas
    Bazinga
    取石子问题
    Sudoku Killer(hdu 1426 数独)
    欧拉函数
    CCPC Ancient Go
    ZZNU 1992: 情人节的尴尬
    fzu Problem 2128 最长子串(KMP + strstr 经典好题)
    POJ
    HDU
  • 原文地址:https://www.cnblogs.com/zxp6/p/10710821.html
Copyright © 2011-2022 走看看