支持高并发高性能 通用缓存容器 浓缩的精华 代码优化版
Posted jingyechong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了支持高并发高性能 通用缓存容器 浓缩的精华 代码优化版相关的知识,希望对你有一定的参考价值。
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————
1 public static class CustomCache 2 3 private static List<ConcurrentDictionary<string, DataModel>> cacheContainer = null;//缓存容器 4 5 private static readonly int cpuCount = Environment.ProcessorCount;//获取CPU个数 6 7 private static readonly int intervalTime = 900;//间隔时间 秒 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, out DataModel dataModel); 43 44 45 46 47 ); 48 49 50 /// <summary> 51 /// 添加或更新缓存 52 /// </summary> 53 /// <param name="key">键</param> 54 /// <param name="keyValue">键值</param> 55 /// <param name="lifeCycle">生命周期</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">键</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)//如果缓存为Null值返回类型默认值 85 86 return default(T); 87 88 if (typeof(T) != temp.CacheData.GetType())//判断类型是否一致,不一致将抛出异常 89 90 throw new Exception("This key value specifies that the type does not match the container cache type."); 91 92 return (T)temp.CacheData; 93 94 return default(T);//键值不存在也将返回类型默认值 95 96 97 /// <summary> 98 /// 获取缓存数据 99 /// </summary> 100 /// <typeparam name="T"></typeparam> 101 /// <param name="key">键值</param> 102 /// <param name="func">委托</param> 103 /// <param name="lifeCycle">生命周期</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)//如果缓存为Null值返回类型默认值 111 112 return default(T); 113 114 if (typeof(T) != temp.CacheData.GetType())//判断类型是否一致,不一致将抛出异常 115 116 throw 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 132 133 /// <summary> 134 /// 是否存在缓存数据 135 /// </summary> 136 /// <param name="key">键</param> 137 /// <param name="containerIndex">容器索引</param> 138 /// <returns>返回 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 DataModel 2 3 /// <summary> 4 /// 生命周期类型 5 /// </summary> 6 public LifeCycle LifeCycleType get; set; 7 8 /// <summary> 9 /// 缓存时间 10 /// </summary> 11 public DateTime CacheDateTime get; set; 12 13 /// <summary> 14 /// 缓存数据 15 /// </summary> 16 public object CacheData get; set; 17
1 public enum LifeCycle 2 3 Lasting = 0,//持久 4 Temp = 1//临时 5
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————
以上是关于支持高并发高性能 通用缓存容器 浓缩的精华 代码优化版的主要内容,如果未能解决你的问题,请参考以下文章
2018最新技术Java架构师高并发高性能高可用分布式集群电商缓存性能调优设计模式项目实战视频教程