簡體   English   中英

使用VaryByCustom實現輸出緩存:自定義字符串在哪里?

[英]Implementing Output Caching using VaryByCustom: where does the custom string go?

我正在遵循此MSDN教程來實現自定義緩存機制。 在Global.asax中,我添加了以下內容:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (arg == "minorversion")
    {
        return "Version=" + context.Request.Browser.MinorVersion.ToString();
    }
    return base.GetVaryByCustomString(context, arg);
}

在aspx文件中,我添加了以下內容: <%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="minorversion" %>在頁面頂部。

我不明白的是如何使用Response.Cache.SetVaryByCustom("minorversion"); 以及在何處設置string參數,該參數在運行時基於字符串設置緩存。 例如,如果在代碼隱藏文件的Page_Load函數中設置了一個名為TheLanguage的字符串變量,那么如何以及在哪里指定我希望高速緩存隨該字符串的值而變化?

所有工作都在global.asax方法GetVaryByCustomString中完成。 您可以指定想要用來更改頁面的customstring的名稱,也可以為它命名任意名稱,但這只是用作GetVaryByCustomString方法的鍵。

如本教程所示,如果要基於“次要版本”進行更改,則可以為此名稱創建一個名稱(“ minorversion”),然后根據context.Request.Browser.MinorVersion.ToString()實現它。 對於GetVaryByCustomString和所提供的arg返回的字符串的每個變體,您將獲得不同的緩存輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM