app2:user123 表示 App2 中 ID 为 123 的用户信息。
抽象组件
IDistributedCache 接口提供以下方法来处理分布式缓存实现中的项:Remove、RemoveAsync:根据字符串键删除缓存项。
public abstract class DistributedCacheDecorator : IDistributedCache { protected readonly IDistributedCache _innerCache; protected DistributedCacheDecorator(IDistributedCache innerCache) { _innerCache = innerCache; } public virtual byte[] Get(string key) { return _innerCache.Get(key); } public virtual Task<byte[]> GetAsync(string key, CancellationToken token = default) { return _innerCache.GetAsync(key, token); } public virtual void Set(string key, byte[] value, DistributedCacheEntryOptions options) { _innerCache.Set(key, value, options); } public virtual Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default) { return _innerCache.SetAsync(key, value, options, token); } public virtual void Refresh(string key) { _innerCache.Refresh(key); } public virtual Task RefreshAsync(string key, CancellationToken token = default) { return _innerCache.RefreshAsync(key, token); } public virtual void Remove(string key) { _innerCache.Remove(key); } public virtual Task RemoveAsync(string key, CancellationToken token = default) { return _innerCache.RemoveAsync(key, token); } }具体装饰器
public class PrefixDistributedCache : DistributedCacheDecorator { private readonly string _prefix; public PrefixDistributedCache(IDistributedCache innerCache, string prefix) : base(innerCache) { _prefix = prefix; } private string AddPrefix(string key) { return $"{_prefix}:{key}"; } public override byte[] Get(string key) { return base.Get(AddPrefix(key)); } public override Task<byte[]> GetAsync(string key, CancellationToken token = default) { return base.GetAsync(AddPrefix(key), token); } public override void Set(string key, byte[] value, DistributedCacheEntryOptions options) { base.Set(AddPrefix(key), value, options); } public override Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default) { return base.SetAsync(AddPrefix(key), value, options, token); } public override void Refresh(string key) { base.Refresh(AddPrefix(key)); } public override Task RefreshAsync(string key, CancellationToken token = default) { return base.RefreshAsync(AddPrefix(key), token); } public override void Remove(string key) { base.Remove(AddPrefix(key)); } public override Task RemoveAsync(string key, CancellationToken token = default) { return base.RemoveAsync(AddPrefix(key), token); } }使用
public static class PrefixDistributedCacheExtentions { public static IServiceCollection AddPrefixDistributedCache(this IServiceCollection services, string prefix) { return services.AddSingleton<IDistributedCache>(x => { // 堆代码 duidaima.com var prefixDistributedCache = new PrefixDistributedCache(new MemoryDistributedCache(null), prefix); return prefixDistributedCache; }); } }然后,就可以在我们的项目中使用了:
var builder = WebApplication.CreateBuilder(args); builder.Services.AddPrefixDistributedCache("myio-demo"); var app = builder.Build(); app.MapGet("/cache/{key}/{value}", (IDistributedCache cache, string key,string value) => cache.SetString(key,value)); app.Run();访问 API 后,让我们看一下redis数据库,它应该包含带有"myio-demo"前缀的缓存键: