佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > CreateOutputCachedItemKey 緩存key的創(chuàng)建

CreateOutputCachedItemKey 緩存key的創(chuàng)建

熱門(mén)標(biāo)簽:Linux服務(wù)器 團(tuán)購(gòu)網(wǎng)站 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 電子圍欄 阿里云 服務(wù)器配置 銀行業(yè)務(wù) Mysql連接數(shù)設(shè)置
有關(guān)OutputCache的相關(guān)資料大家可以查看 OutputCacheProvider OutputCache的一點(diǎn)點(diǎn)認(rèn)識(shí) ,我們還是復(fù)習(xí)一下OutputCache內(nèi)容,OutputCache 的處理是在OutputCacheModule類(lèi)中注冊(cè)ResolveRequestCache、UpdateRequestCache這2個(gè)方法,一個(gè) 用于獲取一個(gè)用于設(shè)置緩存。緩存內(nèi)容分為兩部分,一部分為緩存策略CachedVary,一部分為緩存數(shù)據(jù)CachedRawResponse,一個(gè)頁(yè)面 緩存策略只有一個(gè)CachedVary,但是它卻可以有多個(gè)緩存內(nèi)容CachedRawResponse。緩存內(nèi)容的獲取和設(shè)置主要是依賴(lài)于HttpResponse的GetSnapshot() UseSnapshot(HttpRawResponse rawResponse, bool sendBody)方法。一般我們的緩 存都是要占用存儲(chǔ)空間的盡量減少緩存內(nèi)容的副本是非常重要的,那么我們現(xiàn)在就來(lái)看看緩存key是如何創(chuàng)建的,key的創(chuàng)建取決于 CreateOutputCachedItemKey方法。CreateOutputCachedItemKey方法的內(nèi)容還是比較復(fù)雜的,現(xiàn)在讓我們一 起來(lái)看看它的具體實(shí)現(xiàn)吧。

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í)例。我這里的代碼是通過(guò)Reflector.exe反編譯得到,感覺(jué)和真實(shí)的代碼有點(diǎn)差別,不過(guò)邏輯上是一樣的。
我還是以一個(gè)實(shí)際的例子來(lái)變解析邊說(shuō)明,我這里是用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:
復(fù)制代碼 代碼如下:

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í)行:
復(fù)制代碼 代碼如下:

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++;
}
}
}

這段代碼說(shuō)白了就是給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 類(lèi)型的數(shù)據(jù),這里循環(huán)serverVarsWithoutDemand里面的每個(gè)key,每個(gè)key對(duì)應(yīng)的追加字符串為N+key+V+value,如果value是null則從新賦值為“+n+”,可以看見(jià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的取值如下:
復(fù)制代碼 代碼如下:

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

可見(jiàn)varyByAllParams基本都是false,只有varyByParams有且緊有一個(gè)元素并且為*時(shí),varyByAllParams才為true,varyByAllParams為false時(shí)這里的serverVarsWithoutDemand取值為GetServerVarsWithoutDemand方法,與我們的QueryString、Form3沒(méi)什么關(guān)系。GetServerVarsWithoutDemand()方法大家可能都不怎么熟悉,我們來(lái)看看它的定義:
復(fù)制代碼 代碼如下:

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

對(duì)這個(gè)方法不了解不要緊,我們有一個(gè)ServerVariables(獲取 Web 服務(wù)器變量的集合)屬性和他相似:
復(fù)制代碼 代碼如下:

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

其中GetServerVarsWithDemand方法也是調(diào)用GetServerVars方法。現(xiàn)在serverVarsWithoutDemand的數(shù)據(jù)我們也搞清楚了。
builder.Append("C"); 接下來(lái)在追加字符C
接下來(lái)我們?cè)撎幚砭彺鎋varyByCustom的配置了
復(fù)制代碼 代碼如下:

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");
復(fù)制代碼 代碼如下:

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ā)。
復(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沒(méi)什么說(shuō)的,與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過(guò)來(lái)的數(shù)據(jù)有關(guān)。

標(biāo)簽:萍鄉(xiāng) 棗莊 衡水 江蘇 蚌埠 衢州 大理 廣元

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《CreateOutputCachedItemKey 緩存key的創(chuàng)建》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    北流市| 广平县| 湟中县| 奉化市| 东平县| 大庆市| 二连浩特市| 建昌县| 乌拉特前旗| 阆中市| 墨竹工卡县| 嘉善县| 昌宁县| 巩义市| 敦煌市| 巴里| 惠安县| 正安县| 广灵县| 牡丹江市| 会宁县| 东宁县| 枣庄市| 虹口区| 塘沽区| 都昌县| 青海省| 滨州市| 盱眙县| 安平县| 香格里拉县| 榆树市| 共和县| 东台市| 遵义市| 芦山县| 安宁市| 娄底市| 深州市| 辉县市| 大渡口区|