【问题标题】:Is it possible to cache by membership role in asp.net mvc?是否可以通过 asp.net mvc 中的成员角色进行缓存?
【发布时间】:2011-03-14 11:02:51
【问题描述】:

我们有很多页面流量非常高,因此我们在 web.config 中有以下内容:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="defaultCache" duration="900" varyByParam="*" location="Any"/>
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

以及必要的控制器方法上的以下属性:

[OutputCache(CacheProfile = "defaultCache")]

这对我们很有帮助,因为提供给普通用户和管理员角色的缓存页面之间没有交叉。然而,现在我们已经实现了一个 CMS,如果用户以管理员角色登录,则 CMS 的界面会呈现到大多数页面中。但是,我们发现当前的缓存策略现在对我们不起作用,因为管理内容正在被缓存并提供给普通用户。

那么,有没有办法按角色缓存?如果页面的 url 保持不变但内容根据登录的角色发生变化,这是否可能?通过向所有相关页面添加类似 ?admin=true 的内容来更改 URL 是否会更好,这样我们的缓存配置文件上的 varyByParam="*" 属性就可以完成它的工作?

谢谢。

【问题讨论】:

    标签: c# asp.net-mvc caching roles


    【解决方案1】:
    <add name="defaultCache" duration="900" varyByParam="*" varyByCustom="membership" location="Any"/>
    

    全球.asax:

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (custom == "membership")
        {
            string membership = "";//Get membership.
            return membership;
        }
    
        return string.Empty;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 2010-12-27
      • 2011-12-11
      • 2010-12-12
      • 1970-01-01
      • 2011-03-29
      • 2016-12-04
      • 2022-06-10
      • 2015-08-02
      相关资源
      最近更新 更多