创建CacheHelper.cs
public class CacheHelper
{
public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
///
/// 缓存绝对过期时间
///
///Cache键值
///给Cache[key]赋的值
///minute分钟后绝对过期
public static void AddCacheWithAbsolute(string key, object value, int minute)
{
if (value == null) return;
_memoryCache.Set(key, value, new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(minute)));
}
///
/// 缓存相对过期,最后一次访问后minute分钟后过期
///
///Cache键值
///给Cache[key]赋的值
///滑动过期分钟
public static void AddCacheWithRelative(string key, object value, int minute)
{
if (value == null) return;
_memoryCache.Set(key, value, new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(minute)));
}
///
///以key键值,把value赋给Cache[key].如果不主动清空,会一直保存在内存中.
///
///Cache键值
///给Cache[key]赋的值
public static void AddCache(string key, object value)
{
_memoryCache.Set(key, value);
}
///
///清除Cache[key]的值
///
///
public static void CleanCache(string key)
{
_memoryCache.Remove(key);
}
///
///根据key值,返回Cache[key]的值
///
///
public static object GetCache(string key)
{
return _memoryCache.Get(key);
}
///
/// 根据key查询value,如果不存在,则执行传入的查询方法并存入缓存
///
///
///
///
///
public static r GetCacheOrQuery(string key, Func myFunc)
{
r result;
if (GetCache(key) is not r value)
{
result = myFunc();
AddCache(key, result);
}
else
{
result = value;
}
return result;
}
public static r GetCacheOrQuery(string key, Func myFunc, p param1)
{
r result;
if (GetCache(key) is not r value)
{
result = myFunc(param1);
AddCache(key, result);
}
else
{
result = value;
}
return result;
}
public static r GetCacheOrQuery(string key, Func myFunc, p1 param1, p2 param2)
{
r result;
if (GetCache(key) is not r value)
{
result = myFunc(param1,param2);
AddCache(key, result);
}
else
{
result = value;
}
return result;
}
public static r GetCacheOrQuery(string key, Func myFunc, p1 param1, p2 param2, p3 param3)
{
r result;
if (GetCache(key) is not r value)
{
result = myFunc(param1,param2,param3);
AddCache(key, result);
}
else
{
result = value;
}
return result;
}
public static r GetCacheOrQuery
(string key, Func myFunc, p1 param1, p2 param2, p3 param3, p4 param4)
{
r result;
if (GetCache(key) is not r value)
{
result = myFunc(param1,param2,param3,param4);
AddCache(key, result);
}
else
{
result = value;
}
return result;
}
public static r GetCacheOrQuery
(string key, Func myFunc, p1 param1, p2 param2, p3 param3, p4 param4, p5 param5)
{
r result;
if (GetCache(key) is not r value)
{
result = myFunc(param1, param2, param3, param4,param5);
AddCache(key, result);
}
else
{
result = value;
}
return result;
}
public static r GetCacheOrQuery
(string key, Func myFunc, p1 param1, p2 param2, p3 param3, p4 param4, p5 param5, p6 param6)
{
r result;
if (GetCache(key) is not r value)
{
result = myFunc(param1, param2, param3, param4,param5,param6);
AddCache(key, result);
}
else
{
result = value;
}
return result;
}
}
测试
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Any;
namespace Cache.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public Object Get()
{
object queryData = CacheHelper.GetCacheOrQuery("queryValue", value);
return queryData;
}
private static int index = 1;
private object value()
{
Console.WriteLine($"这是第{index++}次查询数据");
return new
{
name = "zhangsan",
age = 22
};
}
}
}
只有当第一次访问时,才会执行查询方法