创建CacheHelper.cs
csharp
public class CacheHelper
{
public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
/// <summary>
/// 缓存绝对过期时间
/// </summary>
///<param name="key">Cache键值</param>
///<param name="value">给Cache[key]赋的值</param>
///<param name="minute">minute分钟后绝对过期</param>
public static void AddCacheWithAbsolute(string key, object value, int minute)
{
if (value == null) return;
_memoryCache.Set(key, value, new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(minute)));
}
/// <summary>
/// 缓存相对过期,最后一次访问后minute分钟后过期
/// </summary>
///<param name="key">Cache键值</param>
///<param name="value">给Cache[key]赋的值</param>
///<param name="minute">滑动过期分钟</param>
public static void AddCacheWithRelative(string key, object value, int minute)
{
if (value == null) return;
_memoryCache.Set(key, value, new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(minute)));
}
/// <summary>
///以key键值,把value赋给Cache[key].如果不主动清空,会一直保存在内存中.
/// </summary>
///<param name="key">Cache键值</param>
///<param name="value">给Cache[key]赋的值</param>
public static void AddCache(string key, object value)
{
_memoryCache.Set(key, value);
}
/// <summary>
///清除Cache[key]的值
/// </summary>
///<param name="key"></param>
public static void CleanCache(string key)
{
_memoryCache.Remove(key);
}
/// <summary>
///根据key值,返回Cache[key]的值
/// </summary>
///<param name="key"></param>
public static object GetCache(string key)
{
return _memoryCache.Get(key);
}
/// <summary>
/// 根据key查询value,如果不存在,则执行传入的查询方法并存入缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="myFunc"></param>
/// <returns></returns>
public static r GetCacheOrQuery<r>(string key, Func<r> myFunc)
{
r result;
if (GetCache(key) is not r value)
{
result = myFunc();
AddCache(key, result);
}
else
{
result = value;
}
return result;
}
public static r GetCacheOrQuery<p, r>(string key, Func<p, r> 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<p1, p2, r>(string key, Func<p1, p2, r> 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<p1, p2, p3, r>(string key, Func<p1, p2, p3, r> 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<p1, p2, p3, p4, r>
(string key, Func<p1, p2, p3, p4, r> 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<p1, p2, p3, p4, p5, r>
(string key, Func<p1, p2, p3, p4, p5, r> 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<p1, p2, p3, p4, p5, p6, r>
(string key, Func<p1, p2, p3, p4, p5, p6,r> 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;
}
}
测试
csharp
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
};
}
}
}
只有当第一次访问时,才会执行查询方法