asp.net mvc 缓存CaChe使用

1 .net framework缓存存储帮助类

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();
    }

2 net 6 缓存存储辅助类

using Microsoft.Extensions.Caching.Memory;
using System;

public class TCache
{
  //缓存的配置
  static MemoryCacheOptions cacheOps = new MemoryCacheOptions()
  {
    //缓存最大为100份
    //##注意netcore中的缓存是没有单位的,缓存项和缓存的相对关系
    SizeLimit = 100,
    //缓存满了时,压缩20%(即删除20份优先级低的缓存项)
    CompactionPercentage = 0.2,
    //两秒钟查找一次过期项
    ExpirationScanFrequency = TimeSpan.FromSeconds(3)
  };
  static MemoryCache myCache = new MemoryCache(cacheOps);

  /// <summary>  
  /// 获取数据缓存  
  /// </summary>  
  /// <param name="cacheKey">键</param>  
  public static object GetCache(string cacheKey)
  {
    var objCache = myCache.Get(cacheKey);
    return objCache;
  }
  /// <summary>  
  /// 设置数据缓存  
  /// </summary>  
  public static void SetCache(string cacheKey, object objObject)
  {
    myCache.Set(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
    {
      //单个缓存项的配置
      MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions()
      {
        //绝对过期时间1
        //AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddSeconds(2)),
        //绝对过期时间2
        //AbsoluteExpirationRelativeToNow=TimeSpan.FromSeconds(3),
        AbsoluteExpirationRelativeToNow=TimeSpan.FromMinutes(timeout),
        //相对过期时间
        //SlidingExpiration = TimeSpan.FromSeconds(3),
        //优先级,当缓存压缩时会优先清除优先级低的缓存项
        Priority = CacheItemPriority.Low,//Low,Normal,High,NeverRemove
                                         //缓存大小占1份
        Size = 1
      };

      if (objObject == null) return;
      myCache.Set(cacheKey, objObject, cacheEntityOps);
    }
    catch (Exception)
    {
      //throw;  
    }
  }
  /// <summary>  
  /// 移除指定数据缓存  
  /// </summary>  
  public static void RemoveAllCache(string cacheKey)
  {
    myCache.Remove(cacheKey);
  }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FlYFlOWERANDLEAF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值