当前位置: 代码迷 >> 综合 >> C#使用自定义的CacheHelper缓存辅助类
  详细解决方案

C#使用自定义的CacheHelper缓存辅助类

热度:72   发布时间:2024-02-27 22:28:41.0

1、添加缓存辅助类:CacheHelper.cs

需要先添加引用:引用 → 添加引用 →  .NET → system.web

using System;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using System.Web.Caching;namespace myTools
{/// <summary>/// 缓存类/// </summary>public static class CacheHelper{/// <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>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), TimeSpan.Zero, CacheItemPriority.High, 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());}}}
}

2、调用方法:

//1.读取缓存
var cache = CacheHelper.GetCache("key键");
if (cache == null)
{//2.添加缓存int timeout = 300;//缓存时间(单位秒)CacheHelper.SetCache("key键", "要保存的值", timeout);
}

 

  相关解决方案