asp.net mvc 缓存CaChe使用
生活随笔
收集整理的這篇文章主要介紹了
asp.net mvc 缓存CaChe使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
緩存存儲幫助類
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Caching;public class TCatche{/// <summary> /// 獲取數據緩存 /// </summary> /// <param name="cacheKey">鍵</param> public static object GetCache(string cacheKey){var objCache = HttpRuntime.Cache.Get(cacheKey);return objCache;}/// <summary> /// 設置數據緩存 /// </summary> public static void SetCache(string cacheKey, object objObject){var objCache = HttpRuntime.Cache;objCache.Insert(cacheKey, objObject);}/// <summary>/// /// </summary>/// <param name="cacheKey">緩存key</param>/// <param name="objObject">要緩存的值</param>/// <param name="timeout">期限</param>public static void SetCache(string cacheKey, object objObject, int timeout = 7200){try{if (objObject == null) return;var objCache = HttpRuntime.Cache;//相對過期 //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null); //絕對過期時間 objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), Cache.NoSlidingExpiration, CacheItemPriority.Low, null);}catch (Exception){//throw; }}/// <summary> /// 移除指定數據緩存 /// </summary> public static void RemoveAllCache(string cacheKey){var cache = HttpRuntime.Cache;cache.Remove(cacheKey);}/// <summary> /// 移除全部緩存 /// </summary> public static void RemoveAllCache(){var cache = HttpRuntime.Cache;var cacheEnum = cache.GetEnumerator();while (cacheEnum.MoveNext()){cache.Remove(cacheEnum.Key.ToString());}}}具體使用:
var test= TCatche.GetCache(bStrCacheKey);if (test != null && test.ToString() != ""){result = test.ToString();}總結
以上是生活随笔為你收集整理的asp.net mvc 缓存CaChe使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两颗自研芯片加持!小米12S Ultra
- 下一篇: java 垃圾回收 null_java方