你好有一個相當內存密集的儀表板,每個用戶都不同。如何根據當前登錄的用戶標識緩存響應,該用戶標識不作爲參數傳遞,但需要從當前登錄的用戶派生。這是我的理解的VaryByParam着眼於請求上下文每個用戶輸出緩存
也有是,當這個改變緩存需要重置
你好有一個相當內存密集的儀表板,每個用戶都不同。如何根據當前登錄的用戶標識緩存響應,該用戶標識不作爲參數傳遞,但需要從當前登錄的用戶派生。這是我的理解的VaryByParam着眼於請求上下文每個用戶輸出緩存
也有是,當這個改變緩存需要重置
在你Web.config
:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Dashboard" duration="86400" varyByParam="*" varyByCustom="User" location="Server" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
在你Controller
/Action
:
[OutputCache(CacheProfile="Dashboard")]
public class DashboardController : Controller { ...}
然後在您的Global.asax
:
//string arg filled with the value of "varyByCustom" in your web.config
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "User")
{
// depends on your authentication mechanism
return "User=" + context.User.Identity.Name;
//?return "User=" + context.Session.SessionID;
}
return base.GetVaryByCustomString(context, arg);
}
從本質上說,GetVaryByCustomString
將讓你寫一個自定義的方法來確定是否會有一個緩存hit
/miss
通過返回將被用來作爲某種每個緩存拷貝的「哈希」的字符串。
修改代碼了一下,在你的表單中添加一個隱藏字段在數據庫中的值當你在發佈時,它有userId散列。
或者更好的方法是使用VaryByCustom方法,並根據用戶的cookie值而有所不同。
這裏是一個很好的參考:
http://codebetter.com/darrellnorton/2004/05/04/asp-net-varybycustom-page-output-caching/