High-performance general-purpose cache supports concurrent concentrated essence container code optimization Edition

 

—————————————————————————————————————————————————————————————————————————————————————————————————————————————————

  . 1  public  static  class CustomCache
   2      {
   . 3          Private  static List <The ConcurrentDictionary < String , the DataModel >> cacheContainer = null ; // buffer container 
  . 4  
  . 5          Private  static  Readonly  int cpuCount = Environment.ProcessorCount; // Get the number of the CPU 
  . 6  
  . 7          Private  static  Readonly  int intervaltime = 900 ; // interval seconds 
  . 8  
  . 9          Private  static  Readonly  int expirationTime = 15;//过期时间 分
 10 
 11 
 12         /// <summary>
 13         /// 构造初始化
 14         /// </summary>
 15         static CustomCache()
 16         {
 17             cacheContainer = new List<ConcurrentDictionary<string, DataModel>>();
 18             for (int i = 0; i < cpuCount; i++)
 19             {
 20                 cacheContainer.Add(new ConcurrentDictionary<string, DataModel>());
 21             }
 22             AutoClaerCache();
 23         }
 24 
 25         /// <summary>
 26         /// 自动清理缓存
 27         /// </summary>
 28         private static void AutoClaerCache()
 29         {
 30             Task.Factory.StartNew(() =>
 31             {
 32                 while (true)
 33                 {
 34                     Thread.Sleep(intervalTime);
 35                     for (int i = 0; i < cacheContainer.Count; i++)
 36                     {
 37                         var cache = cacheContainer[i].Where(w => w.Value.LifeCycleType == LifeCycle.Temp);
 38                         foreach (var item in cache)
 39                         {
 40                             if (DateTime.Now > item.Value.CacheDateTime)
 41                             {
 42                                 cacheContainer[i].TryRemove(item.Key, outThe dataModel the DataModel);
 43 is                              }
 44 is                          }
 45                      }
 46 is                  }
 47              });
 48          }
 49  
50          ///  <Summary> 
51 is          /// add or update the cache
 52 is          ///  </ Summary> 
53 is          ///  <param name = "key"> key </ param> 
54 is          ///  <param name = "the keyValue"> key </ param> 
55          ///  <param name = "lifeCycle"> life cycle </ param>
 56         /// <returns></returns>
 57         public static DataModel AddOrUpdate(string key, object keyValue, LifeCycle lifeCycle)
 58         {
 59             var model = new DataModel { LifeCycleType = lifeCycle, CacheData = keyValue, CacheDateTime = lifeCycle == LifeCycle.Lasting ? DateTime.Now : DateTime.Now.AddMinutes(expirationTime) };
 60             return cacheContainer[Math.Abs(key.GetHashCode() % cpuCount)].AddOrUpdate(key, model, (k, v) => { return model; });
 61         }
 62 
 63         /// <summary>
 64         /// 删除缓存
 65         /// </summary>
 66         /// <param name="key"></param>
 67         /// <returns></returns>
 68         public static bool Remove(string key)
 69         {
 70             return cacheContainer[Math.Abs(key.GetHashCode() % cpuCount)].TryRemove(key, out DataModel dataModel);
 71         }
 72 
 73         /// <summary>
 74         /// 获取缓存数据
 75         /// </summary>
 76         /// <typeParam name = "T"> </ typeParam> 
77          ///  <param name = "Key"> key <param /> 
78          ///  <Returns> </ Returns> 
79          public  static T GetCacheData <T> ( String Key)
 80          {
 81              IF (IsExistsCacheData (Key, OUT  int I))
 82              {
 83                  var TEMP = cacheContainer [I] [Key];
 84                  IF (temp.CacheData == null ) // If the cache is null default value return type value of 
85                  {
 86                      return default (T);
 87                  }
 88                  IF ( typeof ! (T) = temp.CacheData.GetType ()) // determines whether the type of consistency, inconsistency will throw an exception 
89                  {
 90                      the throw  new new Exception ( " This value specifies that The Key the match does not Container type Cache type. " );
 91 is                  }
 92                  return (T) temp.CacheData;
 93              }
 94              return  default (T); // key does not exist will default value return type 
95          }
 96  
97          // / <Summary> 
98          /// Get the data cache
 99          ///  </ Summary> 
100          ///  <typeParam name = "T"> </ typeParam> 
101          ///  <param name = "Key"> key </ param> 
102          ///  <param name = "FUNC"> Principal </ param> 
103          ///  <param name = "lifeCycle"> life cycle </ param> 
104          ///  <Returns> </ Returns> 
105          public  static T GetCacheData <T> ( String Key, Func <T> FUNC,
LifeCycle lifeCycle)
106         {
107             if (IsExistsCacheData(key, OUT  int I))
 108              {
 109                  var TEMP = cacheContainer [I] [Key];
 110                  IF (temp.CacheData == null ) // If the cache is Null value Return Type Default 
111                  {
 112                      return  default (T);
 113                  }
 114                  iF ( typeof (T)! = temp.CacheData.GetType ()) // determines whether the same type, the inconsistency thrown 
115                  {
 1 16                      the throw  new new exception ( "This key value specifies that the type does not match the container cache type.");
117                 }
118                 return (T)temp.CacheData;
119             }
120             T data = default(T);
121             try
122             {
123                 data = func.Invoke();
124             }
125             catch
126             {
127                 throw new Exception("Func Invoke Exception."); ;
128             }
129             AddOrUpdate (Key, Data, LifeCycle);
 130.              return Data;
 131 is          }
 132  
133          ///  <Summary> 
134          /// whether there is buffered data
 135          ///  </ Summary> 
136          ///  <param name = "Key" > key </ param> 
137          ///  <name = "containerIndex"> param container index </ param> 
138          ///  <returns> Back Bool </ returns> 
139          public  static  BOOL IsExistsCacheData ( String key,out int containerIndex)
140         {
141             containerIndex = Math.Abs(key.GetHashCode() % cpuCount);
142             if (cacheContainer[containerIndex].ContainsKey(key))
143             {
144                 var cache = cacheContainer[containerIndex][key];
145                 if (cache.LifeCycleType == LifeCycle.Temp && DateTime.Now > cache.CacheDateTime)
146                 {
147                     cacheContainer[containerIndex].TryRemove(key, out DataModel dataModel);
148                     return false;
149                 }
150                 return true;
151             }
152             return false;
153         }
154     }

 

. 1  public  class the DataModel
 2      {
 . 3          ///  <Summary> 
. 4          /// life cycle type
 . 5          ///  </ Summary> 
. 6          public LifeCycle LifeCycleType { GET ; SET ;}
 . 7  
. 8          ///  <Summary> 
. 9          /// cache time
 10          ///  </ Summary> 
. 11          public the DateTime CacheDateTime { GET ; SET ;}
 12 is  
13 is          ///  <Summary> 
14          /// cache data
 15         /// </summary>
16         public object CacheData { get; set; }
17     }

 

. 1  public  enum LifeCycle
 2      {
 . 3          Lasting = 0 , // lasting 
. 4          the Temp = . 1 // Temporary 
5      }

 —————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 

Guess you like

Origin www.cnblogs.com/JingYeChong/p/11106134.html