Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按用户配置输出缓存mvc_C#_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

C# 按用户配置输出缓存mvc

C# 按用户配置输出缓存mvc,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我有一个特定于用户的仪表板。仪表板只会每天更改,我想使用MVC的OutputCache。是否有任何方法可以配置每个用户的缓存,并在请求是新的一天时过期 我对此进行了研究,发现您可以扩展OutputCache属性来动态设置持续时间,但是如何配置每个用户的持续时间 提前感谢在web.config中尝试此功能 <system.web> ........... ........... <caching> <outputCacheSettings> &

我有一个特定于用户的仪表板。仪表板只会每天更改,我想使用
MVC的
OutputCache
。是否有任何方法可以配置每个用户的缓存,并在请求是新的一天时过期

我对此进行了研究,发现您可以扩展
OutputCache
属性来动态设置持续时间,但是如何配置每个用户的持续时间


提前感谢

在web.config中尝试此功能

<system.web>
 ...........
 ...........
 <caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="UserCache" duration="1440" varyByParam="UserID" enabled="true" location="Client"/>
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

...........
...........

Web.config
中:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="Dashboard" duration="86400" varyByParam="*" varyByCustom="User" location="Server" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>
然后在您的
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

相同的概念是否适用于DevTrends DonutCaching?没有尝试过,但因为它们也提供了
VaryByCustom
机制。我想答案是肯定的。
//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);
}