using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; using System.Text; namespace Components.Helper { /// <summary> /// 一般緩存類 /// </summary> public class CacheHelper { static CacheItemPolicy cacheItemPolicy = new CacheItemPolicy() { RemovedCallback = new CacheEntryRemovedCallback(CacheEntryRemoved), SlidingExpiration = new TimeSpan(0, 20, 0) }; static void CacheEntryRemoved(CacheEntryRemovedArguments arg) { //Entities<T> db = new Entities<T>(); //var list = db.ESet.ToList(); // Cache<T>.AddCache(list); } /// <summary>新增緩存</summary> /// <param name="key">緩存Key</param> /// <param name="data">緩存數據</param> public static void AddCache(string key, object data) { if (MemoryCache.Default.Contains(key)) { MemoryCache.Default.Remove(key); } MemoryCache.Default.Add(key, data, cacheItemPolicy); } /// <summary> /// 新增緩存 /// </summary> /// <param name="key">緩存的唯一標識key</param> /// <param name="data">緩存的數據</param> /// <param name="outMinutes">過期時間(分鐘)</param> public static void AddCache(string key, object data, int outMinutes) { if (MemoryCache.Default.Contains(key)) { MemoryCache.Default.Remove(key); } CacheItemPolicy _cacheItemPolicy = new CacheItemPolicy() { RemovedCallback = new CacheEntryRemovedCallback(CacheEntryRemoved), SlidingExpiration = new TimeSpan(0, outMinutes, 0) }; MemoryCache.Default.Add(key, data, _cacheItemPolicy); } /// <summary> /// 根據key獲取單個緩存 /// </summary> /// <param name="key"></param> /// <returns></returns> public static object GetCache(string key) { if (MemoryCache.Default.Contains(key)) { return (object)MemoryCache.Default[key]; } return null; } /// <summary>移除緩存</summary> /// <param name="key">緩存Key</param> public static void RemoveCache(string key) { if (MemoryCache.Default.Contains(key)) { MemoryCache.Default.Remove(key); } } /// <summary> /// 移除緩存,針對空間 /// </summary> /// <param name="regionName"></param> public static void RemoveRegionCache(string regionName) { var cacheList = MemoryCache.Default.Where(q => q.Key.StartsWith(regionName + ":")).ToList(); foreach (var item in cacheList) { MemoryCache.Default.Remove(item.Key); } } /// <summary> /// 新增分布式緩存 /// </summary> /// <param name="key">緩存Key</param> /// <param name="data">緩存數據</param> /// <param name="p_IsOutDate">是否過期 默認過期事件20分鐘 webconfig中配置</param> public static void AddMemcachedCache(string key, object data, bool p_IsOutDate) { if (MemcachHelper.IsEnableScatteredCache) { if (MemcachHelper.KeyExists(key)) { MemcachHelper.Delete(key); } if (p_IsOutDate) { //初始化過期時間 DateTime m_expiryTime = DateTime.Now.AddMilliseconds(MemcachHelper.DefaultCacheTime); MemcachHelper.Add(key, data, m_expiryTime); } else { MemcachHelper.Add(key, data); } } } /// <summary> /// 根據key獲取單個分布式緩存 /// </summary> /// <param name="key"></param> /// <returns></returns> public static object GetMemcachedCache(string key) { if (MemcachHelper.IsEnableScatteredCache) { if (MemcachHelper.KeyExists(key)) { return MemcachHelper.Get(key); } } return null; } /// <summary> /// 根據key刪除單個分布式緩存 /// </summary> /// <param name="key"></param> /// <returns></returns> public static void RemoveMemcachedCache(string key) { if (MemcachHelper.IsEnableScatteredCache) { if (MemcachHelper.KeyExists(key)) { MemcachHelper.Delete(key); } } } } }