ASP.NET Core - ResponseCache源码分析【未完成】
2021-03-02 23:27
标签:参数 其他 var public caching str tor size 文件中 最近项目中要加页面缓存功能,因为网站内存有限,页面只能存在硬盘中,ASP.NET Core提供了ResponseCache中间件,但是只支持内存缓存,我们需要将缓存存在页面文件中 看到这就知道,如果需要使用其他方式缓存接口内容,需要实现此接口 IResponseCache接口的实现类 缓存key提供器,后台修改产品数据,我需要找到对应的页面文件,所以缓存key也必须由我生成,比如以下格式:product_001_参数哈希 缓存策略,内部几个方法用来判断是否应该使用缓存 Response.Body流的装饰器 ASP.NET Core - ResponseCache源码分析【未完成】 标签:参数 其他 var public caching str tor size 文件中 原文地址:https://www.cnblogs.com/fanfan-90/p/14311288.html需求
并且如果产品、系列在后台被修改,需要尽可能实时的修改页面
所以需要通过ResponseCache中间件源码查看如何扩展源码
ResponseCachingMiddleware构造函数
internal ResponseCachingMiddleware(
RequestDelegate next,
IOptions
IResponseCache:提供缓存的读取和写入,框架中只提供了一个内存缓存的实现类,如果需要将缓存写入其他位置,可实现此接口
internal interface IResponseCache
{
IResponseCacheEntry? Get(string key);
void Set(string key, IResponseCacheEntry entry, TimeSpan validFor);
}
MemoryResponseCache
internal class MemoryResponseCache : IResponseCache
{
private readonly IMemoryCache _cache;
internal MemoryResponseCache(IMemoryCache cache)
{
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
}
public IResponseCacheEntry? Get(string key)
{
var entry = _cache.Get(key);
if (entry is MemoryCachedResponse memoryCachedResponse)
{
return new CachedResponse
{
Created = memoryCachedResponse.Created,
StatusCode = memoryCachedResponse.StatusCode,
Headers = memoryCachedResponse.Headers,
Body = memoryCachedResponse.Body
};
}
else
{
return entry as IResponseCacheEntry;
}
}
public void Set(string key, IResponseCacheEntry entry, TimeSpan validFor)
{
if (entry is CachedResponse cachedResponse)
{
_cache.Set(
key,
new MemoryCachedResponse
{
Created = cachedResponse.Created,
StatusCode = cachedResponse.StatusCode,
Headers = cachedResponse.Headers,
Body = cachedResponse.Body
},
new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = validFor,
Size = CacheEntryHelpers.EstimateCachedResponseSize(cachedResponse)
});
}
else
{
_cache.Set(
key,
entry,
new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = validFor,
Size = CacheEntryHelpers.EstimateCachedVaryByRulesySize(entry as CachedVaryByRules)
});
}
}
}
IResponseCachingKeyProvider
IResponseCachingPolicyProvider、ResponseCachingPolicyProvider
比如:必须是get、head请求,且Header中没有Authorization,才取缓存ResponseCachingStream
下一篇:js基础部分
文章标题:ASP.NET Core - ResponseCache源码分析【未完成】
文章链接:http://soscw.com/index.php/essay/59253.html