每个用户的输出缓存 [英] Output cache per User
问题描述
喜有相当内存密集型的仪表板是每个用户不同。我如何缓存基于登录用户ID当前未作为参数传递,但需要从登录的用户目前得到的响应。这是我的理解的VaryByParam着眼于请求上下文
Hi have quite a memory intensive dashboard which is different per user. How do I cache the response based on the current logged in userID which is not passed as a parameter but needs to be derived from the current logged in user. It is my understanding VaryByParam looks at the request context
也有是,当这个被改变缓存需要被重置在数据库中的值
Also there is a value in the database that when this is changed the cache needs to be reset
推荐答案
在你的的Web.config
:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Dashboard" duration="86400" varyByParam="*" varyByCustom="User" location="Server" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
在你的控制器
/ 动作
:
[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
将让你写一个自定义的方法来确定是否会有一个缓存按
/ 通过返回将被用来作为某种每个缓存副本的哈希的字符串错过
。
In essence, GetVaryByCustomString
will let you write a custom method to determine whether there will be a Cache hit
/ miss
by returning a string that will be used as some sort of a 'hash' per Cache copy.
这篇关于每个用户的输出缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!