/// /// 創建緩存項的文件 /// /// 緩存Key /// object對象 public static void Insert(string key, object obj) { //創建緩存 HttpContext.C... ...
/// <summary> /// 創建緩存項的文件 /// </summary> /// <param name="key">緩存Key</param> /// <param name="obj">object對象</param> public static void Insert(string key, object obj) { //創建緩存 HttpContext.Current.Cache.Insert(key, obj); } /// <summary> /// 移除緩存項的文件 /// </summary> /// <param name="key">緩存Key</param> public static void Remove(string key) { //創建緩存 HttpContext.Current.Cache.Remove(key); } /// <summary> /// 創建緩存項的文件依賴 /// </summary> /// <param name="key">緩存Key</param> /// <param name="obj">object對象</param> /// <param name="fileName">文件絕對路徑</param> public static void Insert(string key, object obj, string fileName) { //創建緩存依賴項 CacheDependency dep = new CacheDependency(fileName); //創建緩存 HttpContext.Current.Cache.Insert(key, obj, dep); } /// <summary> /// 創建緩存項過期 /// </summary> /// <param name="key">緩存Key</param> /// <param name="obj">object對象</param> /// <param name="expires">過期時間(分鐘)</param> public static void Insert(string key, object obj, int expires) { HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0)); } /// <summary> /// 獲取緩存對象 /// </summary> /// <param name="key">緩存Key</param> /// <returns>object對象</returns> public static object Get(string key) { if (string.IsNullOrEmpty(key)) { return null; } try { return HttpContext.Current.Cache.Get(key); } catch { return null; } } /// <summary> /// 獲取緩存對象 /// </summary> /// <typeparam name="T">T對象</typeparam> /// <param name="key">緩存Key</param> /// <returns></returns> public static T Get<T>(string key) { object obj = Get(key); return obj == null ? default(T) : (T)obj; } /// <summary> /// 獲取數據緩存 /// </summary> /// <param name="CacheKey">鍵</param> public static object GetCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// <summary> /// 設置數據緩存 /// </summary> public static void SetCache(string CacheKey, object objObject) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject); } /// <summary> /// 移除指定數據緩存 /// </summary> public static void RemoveAllCache(string CacheKey) { System.Web.Caching.Cache _cache = HttpRuntime.Cache; _cache.Remove(CacheKey); } /// <summary> /// 移除全部緩存 /// </summary> public static void RemoveAllCache() { System.Web.Caching.Cache _cache = HttpRuntime.Cache; IDictionaryEnumerator CacheEnum = _cache.GetEnumerator(); while (CacheEnum.MoveNext()) { _cache.Remove(CacheEnum.Key.ToString()); } }