前言
我们在开发时有的业务需求要请求第三方接口(如百度腾讯等),此时通常需要先获取token,而此token不可以经常获取,所以要暂存,便于多次使用,接下来介绍下如何在netcore中使用服务器内部缓存IMemoryCach,后续会再写一篇关于分布式缓存IDistrubute的。
服务端的缓存基础知识
缓存可以减少生成内容所需的工作,从而显著提高应用程序的性能和可伸缩性。 缓存最适用于不经常更改,或者生成成本很高的数据。 通过缓存,可以比从数据源返回的数据的副本速度快得多。
ASP.NET Core 支持多个不同的缓存, 最简单的缓存是IMemoryCache ,其将数据存储在 web服务器的内存中。
内存中缓存可以存储任何对象。分布式缓存(使用IDistrubute+Redis实现)接口仅限byte[]。 内存中和分布式缓存将缓存项作为键值对。- 使用过期限制缓存增长。
- 使用 SetSize、Size 和 SizeLimit 限制缓存大小], ASP.NET Core 运行时不会根据内存压力
限制缓存大小, 开发人员需要限制缓存大小。
第一步、在ConfigureServices中注册
不需要安装Microsoft.Extensions.Caching.Memory,netcore自带了。
// IMemoryCache注册
services.AddMemoryCache();
第二步、构造函数自动注入IMemoryCache
public class CacheService
{
private readonly IMemoryCache _memoryCache;
public CacheService(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
}
第三步、获取设置缓存
1. 最基本的使用:
- Set方法根据Key设置缓存,默认缓存不过期,如果设置了过期时间,则到期后缓存自动失效
- Get方法根据Key取出缓存
string cacheKey = "timestamp";
//set cache
_memoryCache.Set(cacheKey, DateTime.Now.ToString());
//get cache
Console.WriteLine(_memoryCache.Get(cacheKey));
//删除缓存
_memoryCache.Remove(cacheKey);
2. 使用提供的语法糖方法:TryGetValue来实现
// 语法糖写法:
public string GetToken()
{
string token;
// 如果本地服务器无缓存,则重新获取
if (!_memoryCache.TryGetValue("token", out token))
{
// 不存在,则发起api请求token
var result = doSomething(); //api请求业务代码
// Save data in cache and set the relative expiration time to one day
_memoryCache.Set("token", result, TimeSpan.FromDays(1));
token = result;
}
// 存在未过期的token
return token;
}
//原始写法:
string cacheKey = "timestamp";
if (_memoryCache.Get(cacheKey) != null)
{
_memoryCache.Set(cacheKey, DateTime.Now.ToString());
}
else
{
Console.WriteLine(_memoryCache.Get(cacheKey));
}
缓存的进阶使用
1. 缓存过期策略(详见官网使用案例)
- 绝对到期(指定在一个固定的时间点到期)
- 滑动到期(在一个时间长度内没有被命中则过期)
- 组合过期 (绝对过期+滑动过期)
2. 缓存依赖策略
设置一个缓存A 设置一个缓存B,依赖于缓存A 如果缓存A失效,缓存B也失效
/// <summary>
/// 缓存依赖策略
/// </summary>
public void CacheDependencyPolicy()
{
string DependentCTS = "DependentCTS";
string cacheKeyParent = "CacheKeys.Parent";
string cacheKeyChild = "CacheKeys.Child";
var cts = new CancellationTokenSource();
_memoryCache.Set(DependentCTS, cts);
//创建一个cache策略
using (var entry = _memoryCache.CreateEntry(cacheKeyParent))
{
//当前key对应的值
entry.Value = "parent" + DateTime.Now;
//当前key对应的回调事件
entry.RegisterPostEvictionCallback(MyCallback, this);
//基于些key创建一个依赖缓存
_memoryCache.Set(cacheKeyChild, "child" + DateTime.Now, new CancellationChangeToken(cts.Token));
}
string ParentCachedTime = _memoryCache.Get<string>(cacheKeyParent);
string ChildCachedTime = _memoryCache.Get<string>(cacheKeyChild);
string callBackMsg = _memoryCache.Get<string>("callbackMessage");
Console.WriteLine("第一次获取");
Console.WriteLine(ParentCachedTime + "|" + ChildCachedTime + "|" + callBackMsg);
//移除parentKey
_memoryCache.Get<CancellationTokenSource>(DependentCTS).Cancel();
Thread.Sleep(1000);
ParentCachedTime = _memoryCache.Get<string>(cacheKeyParent);
ChildCachedTime = _memoryCache.Get<string>(cacheKeyChild);
callBackMsg = _memoryCache.Get<string>("callbackMessage");
Console.WriteLine("第二次获取");
Console.WriteLine(ParentCachedTime + "|" + ChildCachedTime + "|" + callBackMsg);
}
3. 缓存状态变化事件
当缓存更新、删除时触发一个回调事件,记录缓存变化的内容。
/// <summary>
/// cache状态变化回调
/// </summary>
public void CacheStateCallback()
{
MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
options.AbsoluteExpiration = DateTime.Now.AddSeconds(3
);
options.RegisterPostEvictionCallback(MyCallback, this);
//show callback console
string cacheKey = "absoluteKey";
_memoryCache.Set(cacheKey, DateTime.Now.ToString(), options);
Thread.Sleep(500);
_memoryCache.Set(cacheKey, DateTime.Now.ToString(), options);
_memoryCache.Remove(cacheKey);
}
private static void MyCallback(object key, object value, EvictionReason reason, object state)
{
var message = $"Cache entry state change:{key} {value} {reason} {state}";
((CacheService)state)._memoryCache.Set("callbackMessage", message);
Console.WriteLine(message);
}
IMemoryCach更多用法见官网:Cache in-memory in ASP.NET Core | Microsoft Learn