OutputCacheProvider OutputCache的一点点认识

在asp.net4.0后我们可以实现自己的OutputCacheProvider来控制缓存的位置了,但是我发现很多人多OutputCacheProvider的调用并不是很清楚。首先我们要知道缓存是在哪里注册的。答案是OutputCacheModule

void IHttpModule.Init(HttpApplication app)
{
    if (RuntimeConfig.GetAppConfig().OutputCache.EnableOutputCache)
    {
        app.ResolveRequestCache += new EventHandler(this.OnEnter);
        app.UpdateRequestCache += new EventHandler(this.OnLeave);
    }
}

(RuntimeConfig.GetAppConfig().OutputCache.EnableOutputCache这句很明白检查我们的配置,看看你是否启用缓存。OnEnter是在处理前读取缓存数据,处理后设置缓存。

那么先让我们看看OnEnter的方法把,我把里面多数代码删除 只留主要的结果。

 internal void OnEnter(object source, EventArgs eventArgs)
    {
        this._key = null;       
        string str;

       HttpResponse response = context.Response;
        this._key = str = this.CreateOutputCachedItemKey(context, null);
        object obj2 = OutputCache.Get(str);

if (obj2 != null)
   {
                CachedVary cachedVary = obj2 as CachedVary;
             if (cachedVary != null)
                {
                     str = this.CreateOutputCachedItemKey(context, cachedVary);
                    obj2 = OutputCache.Get(str);}       

                    CachedRawResponse response2 = (CachedRawResponse) obj2;
                    HttpCachePolicySettings settings = response2._settings;
                    HttpRawResponse rawResponse = response2._rawResponse;
                    response.UseSnapshot(rawResponse, sendBody);

                    application.CompleteRequest();

                    return;
                }
             return;
            }            
    }

这里有两次调用obj2 = OutputCache.Get(str),我们来分析一下吧,第一次取出来的数据被转化为cachedVary,这个东西就是保存我们OutputCache中的那些属性配置的一个实例,所以OutputCache不同的属性就会产生不同的cachedVary值,而第二次读取的obj2才是我们真正需要保存的东西,把它取到后做一系列的处理最后结束此次http请求。

如取值是取2次那么设置也一定是设置2次了。

internal void OnLeave(object source, EventArgs eventArgs)
    {
      
            CachedVary vary;
            string str;
            vary = new CachedVary(varyByContentEncodings, varyByHeaders, varyByParams, varyByAllParams, currentSettings.VaryByCustom);
            str = this.CreateOutputCachedItemKey(context, vary);


             HttpRawResponse snapshot = response.GetSnapshot();
             string kernelCacheUrl = response.SetupKernelCaching(null);
             Guid cachedVaryId = (vary != null) ? vary.CachedVaryId : Guid.Empty;
             CachedRawResponse rawResponse = new CachedRawResponse(snapshot, currentSettings, kernelCacheUrl, cachedVaryId);
             CacheDependency dependencies = response.CreateCacheDependencyForResponse();
             OutputCache.InsertResponse(this._key, vary, str, rawResponse, dependencies, noAbsoluteExpiration, noSlidingExpiration);
                
        }
    }

在这里我们看到了CachedVary、CachedRawResponse的两个实例,在这里我们可以确定先前第二次调用obj2 = OutputCache.Get(str)的obj2应该是一个CachedRawResponse实例。而OutputCache的InsertResponse方法:

internal static void InsertResponse(string cachedVaryKey, CachedVary cachedVary, string rawResponseKey, CachedRawResponse rawResponse, CacheDependency dependencies, DateTime absExp, TimeSpan slidingExp)
{
    OutputCacheProvider provider = GetProvider(HttpContext.Current);  
   provider.Set(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
    provider.Set(rawResponseKey, entry, absExp);

}

我相信大家看到这里就明白了,OutputCache的保存和读取都是操作2个实例,一个是OutputCache配置实例CachedVary ,另一个是真正的数据流CachedRawResponse 

看到OutputCache的InsertResponse方法这里提到了OutputCacheProvider provider = GetProvider(HttpContext.Current)而在OutputCacheProvider 的Get方法中也调用了这句

而OutputCache的GetProvider的方法干什么的我想我就不提了,相信大家都会明白。

所以要实现自定义的缓存,可以有两种方案分别是实现自己的OutputCacheProvider和注册自己OutputCacheModule

  相应的配置是

 

 <caching>
      <outputCache defaultProvider="ChannelInMemory">
        <providers>
            <add name="ChannelInMemory" type="MvcApp.MemoryCacheProvider,MvcApp" />
          </providers>
      </outputCache>
    </caching>


    <httpModules>
      <remove name="OutputCache"/>
      <add name="OutputCache" type="MvcApp.CustOutputCacheModule" />
    </httpModules>

 

posted on   dz45693  阅读(3438)  评论(3编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2012年11月 >
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示