CreateOutputCachedItemKey 缓存key的创建

所属分类: 网络编程 / ASP.NET 阅读数: 1932
收藏 0 赞 0 分享
有关OutputCache的相关资料大家可以查看 OutputCacheProvider OutputCache的一点点认识 ,我们还是复习一下OutputCache内容,OutputCache 的处理是在OutputCacheModule类中注册ResolveRequestCache、UpdateRequestCache这2个方法,一个 用于获取一个用于设置缓存。缓存内容分为两部分,一部分为缓存策略CachedVary,一部分为缓存数据CachedRawResponse,一个页面 缓存策略只有一个CachedVary,但是它却可以有多个缓存内容CachedRawResponse。缓存内容的获取和设置主要是依赖于HttpResponse的GetSnapshot() UseSnapshot(HttpRawResponse rawResponse, bool sendBody)方法。一般我们的缓 存都是要占用存储空间的尽量减少缓存内容的副本是非常重要的,那么我们现在就来看看缓存key是如何创建的,key的创建取决于 CreateOutputCachedItemKey方法。CreateOutputCachedItemKey方法的内容还是比较复杂的,现在让我们一 起来看看它的具体实现吧。

CreateOutputCachedItemKey方法又是如何调用的了,创建缓存策略key的代码:this.CreateOutputCachedItemKey(context, null);创建缓存key的代码:this.CreateOutputCachedItemKey(context, cachedVary)区别就在于参数CachedVary 的值一个为null,一个是真正的CachedVary 实例。我这里的代码是通过Reflector.exe反编译得到,感觉和真实的代码有点差别,不过逻辑上是一样的。
我还是以一个实际的例子来变解析边说明,我这里是用asp.net mvc建立的一个demo。请求url:http://localhost:7503/Home/index 那么path就应该是:Home/index
首先我们的可以需要区分我们的请求是Get还是Post,Post以a1打头,否则已a2打头,紧接着追加当前的Path:
复制代码 代码如下:

if (verb == HttpVerb.POST)
{
builder = new StringBuilder("a1", path.Length + "a1".Length);
}
else
{
builder = new StringBuilder("a2", path.Length + "a2".Length);
}
builder.Append(CultureInfo.InvariantCulture.TextInfo.ToLower(path));

到这个时候我们的缓存策略key及确定的,我这里的策略key为:a2/home/index
如果我们的cachedVary不为null则继续执行:
复制代码 代码如下:

for (int i = 0; i <= 2; i++)
{
int num;
string[] array = null;
NameValueCollection serverVarsWithoutDemand = null;
bool flag = false;
switch (i)
{
case 0:
builder.Append("H");
array = cachedVary._headers;
if (array != null)
{
serverVarsWithoutDemand = request.GetServerVarsWithoutDemand();
}
break;
case 1:
builder.Append("Q");
array = cachedVary._params;
if (request.HasQueryString && ((array != null) || cachedVary._varyByAllParams))
{
serverVarsWithoutDemand = request.QueryString;
flag = cachedVary._varyByAllParams;
}
break;
default:
builder.Append("F");
if (verb == HttpVerb.POST)
{
array = cachedVary._params;
if (request.HasForm && ((array != null) || cachedVary._varyByAllParams))
{
serverVarsWithoutDemand = request.Form;
flag = cachedVary._varyByAllParams;
}
}
break;
}
if (flag && (serverVarsWithoutDemand.Count > 0))
{
array = serverVarsWithoutDemand.AllKeys;
num = array.Length - 1;
while (num >= 0)
{
if (array[num] != null)
{
array[num] = CultureInfo.InvariantCulture.TextInfo.ToLower(array[num]);
}
num--;
}
Array.Sort(array, InvariantComparer.Default);
}
if (array != null)
{
num = 0;
int length = array.Length;
while (num < length)
{
string str = array[num];
if (serverVarsWithoutDemand == null)
{
varyByCustomString = "+n+";
}
else
{
varyByCustomString = serverVarsWithoutDemand[str];
if (varyByCustomString == null)
{
varyByCustomString = "+n+";
}
}
builder.Append("N");
builder.Append(str);
builder.Append("V");
builder.Append(varyByCustomString);
num++;
}
}
}

这段代码说白了就是给key值追加HQF3个字符,这个循环首先处理服务器的数据,
array = cachedVary._headers;
serverVarsWithoutDemand = request.GetServerVarsWithoutDemand();
其次是处理QueryString数据:
array = cachedVary._params;
serverVarsWithoutDemand = request.QueryString;
最后处理Form数据
array = cachedVary._params;
serverVarsWithoutDemand = request.Form;
serverVarsWithoutDemand是NameValueCollection 类型的数据,这里循环serverVarsWithoutDemand里面的每个key,每个key对应的追加字符串为N+key+V+value,如果value是null则从新赋值为“+n+”,可以看见不同的请求这里的key及有所不同了。在QueryString和Form时这里的serverVarsWithoutDemand的取值与
cachedVary._varyByAllParams有关,cachedVary的创建是在OutputCacheModule 的OnLeave方法中:
vary = new CachedVary(varyByContentEncodings, varyByHeaders, varyByParams, varyByAllParams, currentSettings.VaryByCustom);
varyByAllParams的取值如下:
复制代码 代码如下:

bool varyByAllParams = false;
if (varyByParams != null)
{
varyByAllParams = (varyByParams.Length == 1) && (varyByParams[0] == "*");
}

可见varyByAllParams基本都是false,只有varyByParams有且紧有一个元素并且为*时,varyByAllParams才为true,varyByAllParams为false时这里的serverVarsWithoutDemand取值为GetServerVarsWithoutDemand方法,与我们的QueryString、Form3没什么关系。GetServerVarsWithoutDemand()方法大家可能都不怎么熟悉,我们来看看它的定义:
复制代码 代码如下:

internal NameValueCollection GetServerVarsWithoutDemand()
{
return this.GetServerVars();
}

对这个方法不了解不要紧,我们有一个ServerVariables(获取 Web 服务器变量的集合)属性和他相似:
复制代码 代码如下:

public NameValueCollection ServerVariables
{
get
{
if (HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low))
{
return this.GetServerVars();
}
return this.GetServerVarsWithDemand();
}
}

其中GetServerVarsWithDemand方法也是调用GetServerVars方法。现在serverVarsWithoutDemand的数据我们也搞清楚了。
builder.Append("C"); 接下来在追加字符C
接下来我们该处理缓存_varyByCustom的配置了
复制代码 代码如下:

if (cachedVary._varyByCustom != null)
{
builder.Append("N");
builder.Append(cachedVary._varyByCustom);
builder.Append("V");
try
{
varyByCustomString = context.ApplicationInstance.GetVaryByCustomString(context, cachedVary._varyByCustom);
if (varyByCustomString == null)
{
varyByCustomString = "+n+";
}
}
catch (Exception exception)
{
varyByCustomString = "+e+";
HttpApplicationFactory.RaiseError(exception);
}
builder.Append(varyByCustomString);
}

这个方法很好明白,如果_varyByCustom不为null那么我们就追加N+key+V+value格式的字符。其中key就是_varyByCustom字符串,value是调用context.ApplicationInstance.GetVaryByCustomString(context, cachedVary._varyByCustom)得到的value,如果value值为null,则设置为“+n+” builder.Append("D");
复制代码 代码如下:

if (((verb == HttpVerb.POST) && cachedVary._varyByAllParams) && (request.Form.Count == 0))
{
int contentLength = request.ContentLength;
if ((contentLength > 0x3a98) || (contentLength < 0))
{
return null;
}
if (contentLength > 0)
{
byte[] asByteArray = ((HttpInputStream) request.InputStream).GetAsByteArray();
if (asByteArray == null)
{
return null;
}
varyByCustomString = Convert.ToBase64String(MachineKeySection.HashData(asByteArray, null, 0, asByteArray.Length));
builder.Append(varyByCustomString);
}
}

这段代码主要是给key追加一个字符D,然后在处理Post的请求(非表单request.Form.Count == 0)把请求的内容(字节)转化为字符追加到key中,一般的http很少会发生此情况,典型的是HttpWebRequest发生的Post请求会触发。
复制代码 代码如下:

builder.Append("E");
string[] strArray2 = cachedVary._contentEncodings;
if (strArray2 != null)
{
string httpHeaderContentEncoding = context.Response.GetHttpHeaderContentEncoding();
if (httpHeaderContentEncoding != null)
{
for (int j = 0; j < strArray2.Length; j++)
{
if (strArray2[j] == httpHeaderContentEncoding)
{
builder.Append(httpHeaderContentEncoding);
break;
}
}
}
}

这段代码首先给key追加一个字符E,然后最佳ContentEncoding,ContentEncoding的取值为 context.Response.GetHttpHeaderContentEncoding()并且在缓存策略中的 _contentEncodings存在才追加。
到现在为止我们的CreateOutputCachedItemKey方法讲完了,缓存策略的key没什么说的,与Http请求方式Get和Post、Request的Path属性有关。但是缓存数据的key有关对象:
(1)与我们的_headers有关,即配置中的 VaryByHeader属性有关,VaryByHeader取值不同,key则不同
(2)与_varyByAllParams有关,当它为true时,实际上就是与 request.QueryString有关,如果此请求是Post则还与request.Form有关;_varyByAllParams默认为 false,为true的情况也很单一 varyByAllParams = (varyByParams.Length == 1) && (varyByParams[0] == "*")
(3)与_varyByCustom有关,它会把 context.ApplicationInstance.GetVaryByCustomString(context, cachedVary._varyByCustom)方法返回值追加到key中,
(4)与_contentEncodings有关,如果 context.Response.GetHttpHeaderContentEncoding()返回的值在_contentEncodings中则追加其返回值。
注意:如果此Http处理是一个Post并且request.Form.Count ==0&& _varyByAllParams为rue的时候海域我们post过来的数据有关。
更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多