1 public class CacheHeloer 2 { 3 4 ///5 /// 默认缓存 6 /// 7 private static CacheHeloer Default { get { return new CacheHeloer(); } } 8 9 ///10 /// 缓存初始化 11 /// 12 private MemoryCache cache = MemoryCache.Default; 13 14 ///15 /// 锁 16 /// 17 private object locker = new object(); 18 19 ///20 /// 构造器 21 /// 22 private CacheHeloer() 23 { 24 //CacheItemPolicy policy = new CacheItemPolicy(); //创建缓存项策略 25 ////过期时间设置,以下两种只能设置一种 26 //policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(5)); //设定某个时间过后将逐出缓存 27 //policy.SlidingExpiration = new TimeSpan(0, 0, 10); //设定某个时间段内未被访问将逐出缓存 28 ////逐出通知,以下两种只能设置一种 29 //policy.UpdateCallback = arguments => { Console.WriteLine("即将逐出缓存" + arguments.Key); }; 30 } 31 32 ///33 /// 从缓存中获取对象 34 /// 35 ///对象类型 36 /// 键 37 ///缓存对象 38 public static object Get(string key) 39 { 40 return Default.GetFromCache(key); 41 } 42 43 ///44 /// 从缓存中获取对象 45 /// 46 ///对象类型 47 /// 键 48 ///缓存对象 49 private object GetFromCache(string key) 50 { 51 lock (locker) 52 { 53 if (cache.Contains(key)) 54 { 55 return cache[key]; 56 } 57 return null; 58 } 59 } 60 61 ///62 /// 设置缓存指定时间未访问过期 63 /// 64 ///对象 65 /// 键 66 /// 数据对象 67 /// 过期时间 68 public static bool Set(string key, Object value, TimeSpan expiresIn) 69 { 70 var policy = new CacheItemPolicy() 71 { 72 SlidingExpiration = expiresIn 73 }; 74 return Default.SetToCache(key, value, policy); 75 } 76 ///77 /// 设置缓存绝对时间过期 78 /// 79 ///80 /// 81 /// 82 /// 83 /// 84 public static bool Set(string key, Object value, DateTimeOffset expiresIn) 85 { 86 var policy = new CacheItemPolicy() 87 { 88 AbsoluteExpiration = expiresIn 89 }; 90 return Default.SetToCache(key, value, policy); 91 } 92 93 /// 94 /// 添加到缓存 95 /// 96 ///缓存对象类型 97 /// 键 98 /// 值 99 ///结果状态 100 public static bool Set(string key, object value)101 {102 CacheItemPolicy policy = new CacheItemPolicy()103 {104 Priority = CacheItemPriority.NotRemovable,105 };106 return Default.SetToCache(key, value, policy);107 }108 109 ///110 /// 数据对象装箱缓存111 /// 112 ///对象 113 /// 键114 /// 数据对象115 /// 过期时间116 private bool SetToCache(string key, object value, CacheItemPolicy policy)117 {118 lock (locker)119 {120 cache.Set(key, value, policy);121 return true;122 }123 }124 125 ///126 /// 获取键的集合127 /// 128 ///键的集合 129 public static ICollectionKeys()130 {131 return Default.GetCacheKeys();132 }133 134 /// 135 /// 获取键的集合136 /// 137 ///键的集合 138 private ICollectionGetCacheKeys()139 {140 lock (locker)141 {142 IEnumerable > items = cache.AsEnumerable();143 return items.Select(m => m.Key).ToList();144 }145 }146 147 /// 148 /// 判断缓存中是否有此对象149 /// 150 /// 键151 ///是否存在 152 public static bool Contain(string key)153 {154 return Default.ContainKey(key);155 }156 157 ///158 /// 判断缓存中是否有此对象159 /// 160 /// 键161 ///是否存在 162 private bool ContainKey(string key)163 {164 lock (locker)165 {166 return cache.Contains(key);167 }168 }169 170 ///171 /// 数据对象从缓存对象中移除172 /// 173 /// 键174 public static bool Remove(string key)175 {176 return Default.RemoveFromCache(key);177 }178 179 ///180 /// 数据对象从缓存对象中移除181 /// 182 /// 键183 private bool RemoveFromCache(string key)184 {185 lock (locker)186 {187 if (cache.Contains(key))188 {189 cache.Remove(key);190 return true;191 }192 return false;193 }194 }195 196 ///197 /// 清除实例198 /// 199 public static void Clear()200 {201 Default.ClearCache();202 }203 204 ///205 /// 清除实例206 /// 207 private void ClearCache()208 {209 lock (locker)210 {211 cache.ToList().ForEach(m => cache.Remove(m.Key));212 }213 }214 215 ///216 /// 获取缓存对象集合217 /// 218 ///缓存对象类型 219 ///缓存对象集合 220 public static ICollectionValues ()221 {222 return Default.GetValues ();223 }224 225 /// 226 /// 获取缓存对象集合227 /// 228 ///缓存对象类型 229 ///缓存对象集合 230 private ICollectionGetValues ()231 {232 lock (locker)233 {234 IEnumerable > items = cache.AsEnumerable();235 return items.Select(m => (T)m.Value).ToList();236 }237 }238 239 /// 240 /// 获取缓存尺寸241 /// 242 ///缓存尺寸 243 public static long Size()244 {245 return Default.GetCacheSize();246 }247 248 ///249 /// 获取缓存尺寸250 /// 251 ///缓存尺寸 252 private long GetCacheSize()253 {254 lock (locker)255 {256 return cache.GetCount();257 }258 }259 }