c# – 每个用户输出缓存

前端之家收集整理的这篇文章主要介绍了c# – 每个用户输出缓存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你有一个记忆密集的仪表板,每个用户是不同的.如何根据当前登录的userID缓存响应,该userID不作为参数传递,但需要从当前登录用户派生.这是我的理解VaryByParam看的请求上下文

此外,数据库中还有一个值,当更改此值时,需要重置缓存

解决方法

在您的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将允许您编写一个自定义方法来确定是否会通过返回一个字符串来确定Cache命中/未命中,该字符串将被用作每个Cache副本的某种“哈希”.

原文链接:https://www.f2er.com/csharp/93010.html

猜你在找的C#相关文章