网站首页 网站源码
using System.Threading;
using ZiggyCreatures.Caching.Fusion;
namespace Dpz.Core.Service;
public abstract class AbstractCacheService(IFusionCache fusionCache) : ICacheService
{
protected abstract string CachePrefixKey { get; }
protected abstract TimeSpan CacheDefaultExpiration { get; }
public virtual string BuildCacheKey(string method, object? parameters = null) =>
GenerateCacheKey.Build(CachePrefixKey, method, parameters);
public virtual async Task<T> GetOrSetCacheAsync<T>(
string methodName,
Func<FusionCacheFactoryExecutionContext<T>, CancellationToken, Task<T>> factory,
object? parameters = null,
Action<FusionCacheEntryOptions>? setupAction = null
)
{
var cacheKey = BuildCacheKey(methodName, parameters);
return await fusionCache.GetOrSetAsync<T>(
cacheKey,
async (context, cancellationToken) =>
{
context.Tags = [CachePrefixKey, CachePrefixKey + methodName];
return await factory(context, cancellationToken);
},
setupAction ?? (options => options.SetDuration(CacheDefaultExpiration))
);
}
public virtual async ValueTask<MaybeValue<T>> TryGetFromCacheAsync<T>(
string methodName,
object? parameters = null
)
{
var cacheKey = BuildCacheKey(methodName, parameters);
return await fusionCache.TryGetAsync<T>(cacheKey);
}
public virtual async Task SetCacheAsync<T>(
string methodName,
T value,
object? parameters = null,
params string[] additionalTags
)
{
var cacheKey = BuildCacheKey(methodName, parameters);
var tags = new List<string> { CachePrefixKey, CachePrefixKey + methodName };
tags.AddRange(additionalTags);
await fusionCache.SetAsync(
cacheKey,
value,
options => options.SetDuration(CacheDefaultExpiration),
tags.ToArray()
);
}
public virtual async Task RemoveCacheAsync(string methodName, object? parameters = null)
{
var cacheKey = BuildCacheKey(methodName, parameters);
await fusionCache.RemoveAsync(cacheKey);
}
public virtual async Task<IPagedList<T>> GetOrSetPagedListAsync<T>(
string methodName,
Func<List<string>, Task<IPagedList<T>>> factory,
object? parameters = null,
Action<FusionCacheEntryOptions>? setupAction = null,
params string[] additionalTags
)
{
var cacheKey = BuildCacheKey(methodName, parameters);
var cache = await fusionCache.TryGetAsync<PagedListWarp<T>>(cacheKey);
if (cache.HasValue)
{
return cache.Value.ToPagedList();
}
var tags = new List<string> { CachePrefixKey, CachePrefixKey + methodName };
if (additionalTags.Length > 0)
{
tags.AddRange(additionalTags);
}
var pagedList = await factory(tags);
await fusionCache.SetAsync(
cacheKey,
pagedList.ToPagedListWarp(),
setupAction ?? (options => options.SetDuration(CacheDefaultExpiration)),
tags
);
return pagedList;
}
public virtual ValueTask RemoveByMethodAsync(string methodName)
{
return fusionCache.RemoveByTagAsync(CachePrefixKey + methodName);
}
public ValueTask RemoveByPrefixAsync()
{
return fusionCache.RemoveByTagAsync(CachePrefixKey);
}
}
上述代码定义了一个抽象类 AbstractCacheService
,它实现了一个缓存服务的基础功能,使用了 ZiggyCreatures.Caching.Fusion
库来处理缓存操作。这个类是一个通用的缓存服务,提供了一些方法来管理缓存,包括获取、设置、删除缓存等。以下是对代码功能的详细解释:
AbstractCacheService
是一个抽象类,必须由具体的子类实现。ICacheService
接口,表明它是一个缓存服务。CachePrefixKey
: 子类需要实现的属性,用于定义缓存键的前缀。CacheDefaultExpiration
: 子类需要实现的属性,用于定义缓存的默认过期时间。BuildCacheKey: 生成缓存键的方法,使用 CachePrefixKey
、方法名和参数来构建唯一的缓存键。
GetOrSetCacheAsync:
TryGetFromCacheAsync:
MaybeValue<T>
,表示可能存在的值。SetCacheAsync:
RemoveCacheAsync:
GetOrSetPagedListAsync:
RemoveByMethodAsync:
RemoveByPrefixAsync:
CachePrefixKey
为前缀的缓存条目。async
和 await
关键字,适合在现代应用程序中处理 I/O 密集型操作,如数据库访问或网络请求。AbstractCacheService
提供了一种灵活的方式来管理缓存,允许子类根据具体需求实现缓存键和过期策略。它封装了常见的缓存操作,使得开发者可以专注于业务逻辑,而不必重复实现缓存管理的细节。