激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - CreateOutputCachedItemKey 緩存key的創(chuàng)建

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

2019-10-11 10:58asp.net教程網(wǎng) ASP.NET教程

有關(guān)OutputCache的相關(guān)資料大家可以查看 OutputCacheProvider OutputCache的一點點認識 ,我們還是復習一下OutputCache內(nèi)容

有關(guān)OutputCache的相關(guān)資料大家可以查看 OutputCacheProvider OutputCache的一點點認識 ,我們還是復習一下OutputCache內(nèi)容,OutputCache 的處理是在OutputCacheModule類中注冊ResolveRequestCache、UpdateRequestCache這2個方法,一個 用于獲取一個用于設置緩存。緩存內(nèi)容分為兩部分,一部分為緩存策略CachedVary,一部分為緩存數(shù)據(jù)CachedRawResponse,一個頁面 緩存策略只有一個CachedVary,但是它卻可以有多個緩存內(nèi)容CachedRawResponse。緩存內(nèi)容的獲取和設置主要是依賴于HttpResponse的GetSnapshot() UseSnapshot(HttpRawResponse rawResponse, bool sendBody)方法。一般我們的緩 存都是要占用存儲空間的盡量減少緩存內(nèi)容的副本是非常重要的,那么我們現(xiàn)在就來看看緩存key是如何創(chuàng)建的,key的創(chuàng)建取決于 CreateOutputCachedItemKey方法。CreateOutputCachedItemKey方法的內(nèi)容還是比較復雜的,現(xiàn)在讓我們一 起來看看它的具體實現(xiàn)吧。 

CreateOutputCachedItemKey方法又是如何調(diào)用的了,創(chuàng)建緩存策略key的代碼:this.CreateOutputCachedItemKey(context, null);創(chuàng)建緩存key的代碼:this.CreateOutputCachedItemKey(context, cachedVary)區(qū)別就在于參數(shù)CachedVary 的值一個為null,一個是真正的CachedVary 實例。我這里的代碼是通過Reflector.exe反編譯得到,感覺和真實的代碼有點差別,不過邏輯上是一樣的。 
我還是以一個實際的例子來變解析邊說明,我這里是用asp.net mvc建立的一個demo。請求url:http://localhost:7503/Home/index 那么path就應該是:Home/index 
首先我們的可以需要區(qū)分我們的請求是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則繼續(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個字符,這個循環(huán)首先處理服務器的數(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里面的每個key,每個key對應的追加字符串為N+key+V+value,如果value是null則從新賦值為“+n+”,可以看見不同的請求這里的key及有所不同了。在QueryString和Form時這里的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有且緊有一個元素并且為*時,varyByAllParams才為true,varyByAllParams為false時這里的serverVarsWithoutDemand取值為GetServerVarsWithoutDemand方法,與我們的QueryString、Form3沒什么關(guān)系。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方法也是調(diào)用GetServerVars方法。現(xiàn)在serverVarsWithoutDemand的數(shù)據(jù)我們也搞清楚了。 
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是調(diào)用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)把請求的內(nèi)容(字節(jié))轉(zhuǎn)化為字符追加到key中,一般的http很少會發(fā)生此情況,典型的是HttpWebRequest發(fā)生的Post請求會觸發(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追加一個字符E,然后最佳ContentEncoding,ContentEncoding的取值為 context.Response.GetHttpHeaderContentEncoding()并且在緩存策略中的 _contentEncodings存在才追加。 
到現(xiàn)在為止我們的CreateOutputCachedItemKey方法講完了,緩存策略的key沒什么說的,與Http請求方式Get和Post、Request的Path屬性有關(guān)。但是緩存數(shù)據(jù)的key有關(guān)對象: 
(1)與我們的_headers有關(guān),即配置中的 VaryByHeader屬性有關(guān),VaryByHeader取值不同,key則不同 
(2)與_varyByAllParams有關(guān),當它為true時,實際上就是與 request.QueryString有關(guān),如果此請求是Post則還與request.Form有關(guān);_varyByAllParams默認為 false,為true的情況也很單一 varyByAllParams = (varyByParams.Length == 1) && (varyByParams[0] == "*") 
(3)與_varyByCustom有關(guān),它會把 context.ApplicationInstance.GetVaryByCustomString(context, cachedVary._varyByCustom)方法返回值追加到key中, 
(4)與_contentEncodings有關(guān),如果 context.Response.GetHttpHeaderContentEncoding()返回的值在_contentEncodings中則追加其返回值。 
注意:如果此Http處理是一個Post并且request.Form.Count ==0&& _varyByAllParams為rue的時候海域我們post過來的數(shù)據(jù)有關(guān)。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 草莓视频久久 | 欧美黄色片免费看 | 国产人成免费爽爽爽视频 | 羞羞网站在线看 | 国产亚洲精品久久久久久久久久 | 国产羞羞视频在线观看免费应用 | 成人在线观看免费视频 | 亚洲导航深夜福利涩涩屋 | 一区在线视频观看 | 国产亚洲精品久久久久久久 | 一级成人欧美一区在线观看 | 麻豆视频观看 | 国产91精品久久久久久久 | 国产精品久久久久久婷婷天堂 | 成人精品久久 | 99在线热视频 | 日本一级特级 | 日本一区视频在线播放 | 欧洲成人一区二区 | 免费观看9x视频网站在线观看 | 亚欧在线免费观看 | 欧美四级在线观看 | 撅高 自己扒开 调教 | 久久91精品国产91久久yfo | :国产精品成人一区二区三区 | 一级片免费在线 | 久久逼逼 | 在线观看视频亚洲 | 91网页 | av免费提供| 91看片成人 | 99久久久国产精品免费观看 | 91成人影库 | 免费观看高清视频网站 | 九九热精品视频在线播放 | 欧美一级黄色网 | 91精品国产九九九久久久亚洲 | www.91sese| 国产一区二区久久精品 | 视频一区二区中文字幕 | 久久亚洲精品久久国产一区二区 |