using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Caching; namespace Server.Utils { /// /// 缓存操作,默认缓存1分钟 /// public class CacheHelper { #region 单例 private static CacheHelper ini; public static CacheHelper GetIntance() { if (ini == null) { ini = new CacheHelper(); } return ini; } private CacheHelper() { } #endregion /// /// 缓存容器 /// static System.Web.Caching.Cache objCache = new System.Web.Caching.Cache(); /// /// 设置数据缓存 /// public void SetCache(string CacheKey, object objObject, int timeout = 7200) { if (objCache.Get(CacheKey) != null) { objCache.Remove(CacheKey); } objCache.Insert(CacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero); } /// /// 获取当前应用程序指定CacheKey的Cache值 /// /// /// y public object GetCache(string CacheKey) { return objCache[CacheKey]; } /// /// 清除单一键缓存 /// /// public void RemoveKeyCache(string CacheKey) { try { objCache.Remove(CacheKey); } catch { } } /// /// 清除所有缓存 /// public void RemoveAllCache() { IDictionaryEnumerator CacheEnum = objCache.GetEnumerator(); if (objCache.Count > 0) { ArrayList al = new ArrayList(); while (CacheEnum.MoveNext()) { al.Add(CacheEnum.Key); } foreach (string key in al) { objCache.Remove(key); } } } /// /// 以列表形式返回已存在的所有缓存 /// /// public ArrayList ShowAllCache() { ArrayList al = new ArrayList(); if (objCache.Count > 0) { IDictionaryEnumerator CacheEnum = objCache.GetEnumerator(); while (CacheEnum.MoveNext()) { al.Add(CacheEnum.Key); } } return al; } } }