1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.Caching; 5 using System.Text; 6 7 namespace Components.Helper 8 { 9 /// <summary> 10 /// 一般缓存类 11 /// </summary> 12 public class CacheHelper 13 { 14 static CacheItemPolicy cacheItemPolicy = new CacheItemPolicy() 15 { 16 RemovedCallback = new CacheEntryRemovedCallback(CacheEntryRemoved), 17 SlidingExpiration = new TimeSpan(0, 20, 0) 18 }; 19 20 static void CacheEntryRemoved(CacheEntryRemovedArguments arg) 21 { 22 //Entities<T> db = new Entities<T>(); 23 //var list = db.ESet.ToList(); 24 // Cache<T>.AddCache(list); 25 } 26 27 28 /// <summary>新增缓存</summary> 29 /// <param name="key">缓存Key</param> 30 /// <param name="data">缓存数据</param> 31 public static void AddCache(string key, object data) 32 { 33 if (MemoryCache.Default.Contains(key)) 34 { 35 MemoryCache.Default.Remove(key); 36 } 37 MemoryCache.Default.Add(key, data, cacheItemPolicy); 38 39 } 40 41 42 43 /// <summary> 44 /// 新增缓存 45 /// </summary> 46 /// <param name="key">缓存的唯一标识key</param> 47 /// <param name="data">缓存的数据</param> 48 /// <param name="outMinutes">过期时间(分钟)</param> 49 public static void AddCache(string key, object data, int outMinutes) 50 { 51 52 if (MemoryCache.Default.Contains(key)) 53 { 54 MemoryCache.Default.Remove(key); 55 } 56 CacheItemPolicy _cacheItemPolicy = new CacheItemPolicy() 57 { 58 RemovedCallback = new CacheEntryRemovedCallback(CacheEntryRemoved), 59 SlidingExpiration = new TimeSpan(0, outMinutes, 0) 60 }; 61 MemoryCache.Default.Add(key, data, _cacheItemPolicy); 62 63 } 64 65 /// <summary> 66 /// 根据key获取单个缓存 67 /// </summary> 68 /// <param name="key"></param> 69 /// <returns></returns> 70 public static object GetCache(string key) 71 { 72 73 if (MemoryCache.Default.Contains(key)) 74 { 75 return (object)MemoryCache.Default[key]; 76 } 77 return null; 78 79 80 } 81 82 83 /// <summary>移除缓存</summary> 84 /// <param name="key">缓存Key</param> 85 public static void RemoveCache(string key) 86 { 87 if (MemoryCache.Default.Contains(key)) 88 { 89 MemoryCache.Default.Remove(key); 90 } 91 } 92 93 /// <summary> 94 /// 移除缓存,针对空间 95 /// </summary> 96 /// <param name="regionName"></param> 97 public static void RemoveRegionCache(string regionName) 98 { 99 var cacheList = MemoryCache.Default.Where(q => q.Key.StartsWith(regionName + ":")).ToList(); 100 foreach (var item in cacheList) 101 { 102 MemoryCache.Default.Remove(item.Key); 103 } 104 } 105 106 /// <summary> 107 /// 新增分布式缓存 108 /// </summary> 109 /// <param name="key">缓存Key</param> 110 /// <param name="data">缓存数据</param> 111 /// <param name="p_IsOutDate">是否过期 默认过期事件20分钟 webconfig中配置</param> 112 public static void AddMemcachedCache(string key, object data, bool p_IsOutDate) 113 { 114 if (MemcachHelper.IsEnableScatteredCache) 115 { 116 if (MemcachHelper.KeyExists(key)) 117 { 118 MemcachHelper.Delete(key); 119 } 120 if (p_IsOutDate) 121 { 122 //初始化过期时间 123 DateTime m_expiryTime = DateTime.Now.AddMilliseconds(MemcachHelper.DefaultCacheTime); 124 MemcachHelper.Add(key, data, m_expiryTime); 125 } 126 else 127 { 128 MemcachHelper.Add(key, data); 129 } 130 } 131 } 132 133 /// <summary> 134 /// 根据key获取单个分布式缓存 135 /// </summary> 136 /// <param name="key"></param> 137 /// <returns></returns> 138 public static object GetMemcachedCache(string key) 139 { 140 if (MemcachHelper.IsEnableScatteredCache) 141 { 142 if (MemcachHelper.KeyExists(key)) 143 { 144 return MemcachHelper.Get(key); 145 } 146 } 147 return null; 148 } 149 150 /// <summary> 151 /// 根据key删除单个分布式缓存 152 /// </summary> 153 /// <param name="key"></param> 154 /// <returns></returns> 155 public static void RemoveMemcachedCache(string key) 156 { 157 if (MemcachHelper.IsEnableScatteredCache) 158 { 159 if (MemcachHelper.KeyExists(key)) 160 { 161 MemcachHelper.Delete(key); 162 } 163 } 164 } 165 } 166 }