CreateOutputCachedItemKey 緩存key的創(chuàng)建
CreateOutputCachedItemKey方法又是如何調(diào)用的了,創(chuàng)建緩存策略key的代碼:this.CreateOutputCachedItemKey(context, null);創(chuàng)建緩存key的代碼:this.CreateOutputCachedItemKey(context, cachedVary)區(qū)別就在于參數(shù)CachedVary 的值一個(gè)為null,一個(gè)是真正的CachedVary 實(shí)例。我這里的代碼是通過Reflector.exe反編譯得到,感覺和真實(shí)的代碼有點(diǎn)差別,不過邏輯上是一樣的。
我還是以一個(gè)實(shí)際的例子來變解析邊說明,我這里是用asp.net mvc建立的一個(gè)demo。請(qǐng)求url:http://localhost:7503/Home/index 那么path就應(yīng)該是:Home/index
首先我們的可以需要區(qū)分我們的請(qǐng)求是Get還是Post,Post以a1打頭,否則已a(bǔ)2打頭,緊接著追加當(dāng)前的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));
到這個(gè)時(shí)候我們的緩存策略key及確定的,我這里的策略key為:a2/home/index
如果我們的cachedVary不為null則繼續(xù)執(zhí)行:
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個(gè)字符,這個(gè)循環(huán)首先處理服務(wù)器的數(shù)據(jù),
array = cachedVary._headers;
serverVarsWithoutDemand = request.GetServerVarsWithoutDemand();
其次是處理QueryString數(shù)據(jù):
array = cachedVary._params;
serverVarsWithoutDemand = request.QueryString;
最后處理Form數(shù)據(jù)
array = cachedVary._params;
serverVarsWithoutDemand = request.Form;
serverVarsWithoutDemand是NameValueCollection 類型的數(shù)據(jù),這里循環(huán)serverVarsWithoutDemand里面的每個(gè)key,每個(gè)key對(duì)應(yīng)的追加字符串為N+key+V+value,如果value是null則從新賦值為“+n+”,可以看見不同的請(qǐng)求這里的key及有所不同了。在QueryString和Form時(shí)這里的serverVarsWithoutDemand的取值與
cachedVary._varyByAllParams有關(guān),cachedVary的創(chuàng)建是在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有且緊有一個(gè)元素并且為*時(shí),varyByAllParams才為true,varyByAllParams為false時(shí)這里的serverVarsWithoutDemand取值為GetServerVarsWithoutDemand方法,與我們的QueryString、Form3沒什么關(guān)系。GetServerVarsWithoutDemand()方法大家可能都不怎么熟悉,我們來看看它的定義:
internal NameValueCollection GetServerVarsWithoutDemand()
{
return this.GetServerVars();
}
對(duì)這個(gè)方法不了解不要緊,我們有一個(gè)ServerVariables(獲取 Web 服務(wù)器變量的集合)屬性和他相似:
public NameValueCollection ServerVariables
{
get
{
if (HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low))
{
return this.GetServerVars();
}
return this.GetServerVarsWithDemand();
}
}
其中GetServerVarsWithDemand方法也是調(diào)用GetServerVars方法?,F(xiàn)在serverVarsWithoutDemand的數(shù)據(jù)我們也搞清楚了。
builder.Append("C"); 接下來在追加字符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);
}
這個(gè)方法很好明白,如果_varyByCustom不為null那么我們就追加N+key+V+value格式的字符。其中key就是_varyByCustom字符串,value是調(diào)用context.ApplicationInstance.GetVaryByCustomString(context, cachedVary._varyByCustom)得到的value,如果value值為null,則設(shè)置為“+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追加一個(gè)字符D,然后在處理Post的請(qǐng)求(非表單request.Form.Count == 0)把請(qǐng)求的內(nèi)容(字節(jié))轉(zhuǎn)化為字符追加到key中,一般的http很少會(huì)發(fā)生此情況,典型的是HttpWebRequest發(fā)生的Post請(qǐng)求會(huì)觸發(fā)。
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追加一個(gè)字符E,然后最佳ContentEncoding,ContentEncoding的取值為 context.Response.GetHttpHeaderContentEncoding()并且在緩存策略中的 _contentEncodings存在才追加。
到現(xiàn)在為止我們的CreateOutputCachedItemKey方法講完了,緩存策略的key沒什么說的,與Http請(qǐng)求方式Get和Post、Request的Path屬性有關(guān)。但是緩存數(shù)據(jù)的key有關(guān)對(duì)象:
(1)與我們的_headers有關(guān),即配置中的 VaryByHeader屬性有關(guān),VaryByHeader取值不同,key則不同
(2)與_varyByAllParams有關(guān),當(dāng)它為true時(shí),實(shí)際上就是與 request.QueryString有關(guān),如果此請(qǐng)求是Post則還與request.Form有關(guān);_varyByAllParams默認(rèn)為 false,為true的情況也很單一 varyByAllParams = (varyByParams.Length == 1) && (varyByParams[0] == "*")
(3)與_varyByCustom有關(guān),它會(huì)把 context.ApplicationInstance.GetVaryByCustomString(context, cachedVary._varyByCustom)方法返回值追加到key中,
(4)與_contentEncodings有關(guān),如果 context.Response.GetHttpHeaderContentEncoding()返回的值在_contentEncodings中則追加其返回值。
注意:如果此Http處理是一個(gè)Post并且request.Form.Count ==0&& _varyByAllParams為rue的時(shí)候海域我們post過來的數(shù)據(jù)有關(guān)。
相關(guān)文章
詳解將ASP.NET Core應(yīng)用程序部署至生產(chǎn)環(huán)境中(CentOS7)
這篇文章主要介紹了詳解將ASP.NET Core應(yīng)用程序部署至生產(chǎn)環(huán)境中(CentOS7),具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12Microsoft Visual Studio 2010下如何添加命令提示行
這篇文章主要介紹了Microsoft Visual Studio 2010下如何添加命令提示行的相關(guān)資料,需要的朋友可以參考下2015-11-11為密碼文本框要求不可粘帖字符串只可手動(dòng)輸入(附演示動(dòng)畫)
以前開發(fā)程序時(shí),用戶登錄的密碼文本框,是可以粘帖密碼字符串的,為了安全起見用戶要求不要粘帖,只能由手動(dòng)輸入,感興趣的朋友可以了解下2013-01-01asp.net JavaScript插件 JavaScript Function Outliner
一個(gè)JavaScript Function Outliner插件 第四版本 支持內(nèi)嵌javascript,且可以對(duì)javascript進(jìn)行壓縮2008-07-07淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證
這篇文章主要介紹了淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12ASP.NET Core對(duì)Controller進(jìn)行單元測(cè)試的完整步驟
這篇文章主要給大家介紹了關(guān)于ASP.NET Core對(duì)Controller進(jìn)行單元測(cè)試的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06.NET?Core中簡(jiǎn)單的郵箱格式校驗(yàn)方式
這篇文章主要給大家介紹了關(guān)于.NET?Core中簡(jiǎn)單的郵箱格式校驗(yàn)方式的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03ASP.NET實(shí)現(xiàn)根據(jù)IP獲取省市地址的方法
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)根據(jù)IP獲取省市地址的方法,主要基于QQwry.dat純真IP數(shù)據(jù)庫(kù)來實(shí)現(xiàn)這一功能,非常實(shí)用,需要的朋友可以參考下2014-10-10