HTML5技术

ASP.NET 运行时详解 揭开请求过程神秘面纱 - heavi(3)

字号+ 作者:H5之家 来源:博客园 2015-12-30 17:08 我要评论( )

如果本篇内容对大家有帮助,请点击页面右下角的 关注 。如果觉得不好,也欢迎拍砖。你们的 评价 就是博主的动力!下篇内容,敬请期待! 附录 1.OutputCacheModule 所在管道步骤:ResolveRequestCache、UpdateReques

    如果本篇内容对大家有帮助,请点击页面右下角的关注。如果觉得不好,也欢迎拍砖。你们的评价就是博主的动力!下篇内容,敬请期待!

附录 1.OutputCacheModule

    所在管道步骤:ResolveRequestCache、UpdateRequestCache。查看OutputCacheModule实现的Init方法,代码如下:

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

 

    通过代码我们能看出它在ResolveRequestCache和UpdateRequestCache这两个管道事件上执行了某些操作。OnEnter事件查看缓存记录是否有缓存,有缓存则直接返回缓存,而不执行之后的管道流程了。代码如下:

internal void OnEnter(object source, EventArgs eventArgs) { if (OutputCache.InUse) { switch (request.HttpVerb) { case HttpVerb.GET: case HttpVerb.HEAD: case HttpVerb.POST: { string str; this._key = str = this.CreateOutputCachedItemKey(context, null); object obj2 = OutputCache.Get(str); if (obj2 != null) { response.Cache.ResetFromHttpCachePolicySettings(settings, context.UtcTimestamp); string originalCacheUrl = response2._kernelCacheUrl; if (originalCacheUrl != null) { response.SetupKernelCaching(originalCacheUrl); } PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_RATIO_BASE); PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_HITS); this._key = null; this._recordedCacheMiss = false; application.CompleteRequest(); return; } return; } } } }

    其实OnEnter里边的代码比我粘贴出来的多很多,但主流程是一致的。都是先通过请求的上下文信息(例如RequestPath、Method参数等)获取缓存主键,然后通过缓存主键到缓存队列里边去查看是否有主键对应的缓存。如果有缓存,则直接把缓存输出到Response.Output中,然后整个流程请求流程结束;如果没有缓存,则按管道流程执行下一步。
    既然在OnEnter里边能取出来缓存,那么肯定有写缓存的地方。写缓存正式通过OutputCacheModule的OnLeave方法写入,OnLeave方法代码如下:

internal void OnLeave(object source, EventArgs eventArgs) { bool flag = false; if (response.HasCachePolicy) { cache = response.Cache; if (((cache.IsModified() && (response.StatusCode == 200)) && ((request.HttpVerb == HttpVerb.GET) || (request.HttpVerb == HttpVerb.POST))) && response.IsBuffered()) { if (((((cache.GetCacheability() == HttpCacheability.Public) || (cache.GetCacheability() == HttpCacheability.ServerAndPrivate)) || ((cache.GetCacheability() == HttpCacheability.Server) || flag3)) && ((!cache.GetNoServerCaching() && !response.ContainsNonShareableCookies()) && (cache.HasExpirationPolicy() || cache.HasValidationPolicy()))) && ((!cache.VaryByHeaders.GetVaryByUnspecifiedParameters() && (cache.VaryByParams.AcceptsParams() || ((request.HttpVerb != HttpVerb.POST) && !request.HasQueryString))) && (!cache.VaryByContentEncodings.IsModified() || cache.VaryByContentEncodings.IsCacheableEncoding(context.Response.GetHttpHeaderContentEncoding())))) { flag = true; } } } if (flag) { CachedVary vary; string str; string[] varyByParams; this.RecordCacheMiss(); HttpCachePolicySettings currentSettings = cache.GetCurrentSettings(response); string[] varyByContentEncodings = currentSettings.VaryByContentEncodings; string[] varyByHeaders = currentSettings.VaryByHeaders; if (this._key == null) { this._key = this.CreateOutputCachedItemKey(context, null); } DateTime noAbsoluteExpiration = Cache.NoAbsoluteExpiration; TimeSpan noSlidingExpiration = Cache.NoSlidingExpiration; if (currentSettings.SlidingExpiration) { noSlidingExpiration = currentSettings.SlidingDelta; } else if (currentSettings.IsMaxAgeSet) { DateTime time2 = (currentSettings.UtcTimestampCreated != DateTime.MinValue) ? currentSettings.UtcTimestampCreated : context.UtcTimestamp; noAbsoluteExpiration = time2 + currentSettings.MaxAge; } if (noAbsoluteExpiration > DateTime.UtcNow) { 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); } } }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 如何在 ASP.NET Core 中发送邮件 - Savorboard

    如何在 ASP.NET Core 中发送邮件 - Savorboard

    2017-05-02 08:02

  • 十二个 ASP.NET Core 例子 - Savorboard

    十二个 ASP.NET Core 例子 - Savorboard

    2017-04-27 16:01

  • ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    ASP.NET MVC5请求管道和生命周期 - 雪飞鸿

    2017-04-24 08:04

  • ASP.NET Core MVC 源码学习:详解 Action 的激活 - Savorboard

    ASP.NET Core MVC 源码学习:详解 Action 的激活 - Savorboard

    2017-04-14 13:04

网友点评
/