views:

42

answers:

1

We have a bunch of pages that get really high traffic, and as such we have the following in web.config:

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

and the following attribute on the necessary controller methods:

[OutputCache(CacheProfile = "defaultCache")]

This has served us well because there is no intersection between cached pages served to normal users and those in an admin role. However now we have implemented a CMS where the interface to the CMS is rendered into most pages if a user is logged in under an admin role. However, we have found that the current caching strategy is not working for us now as admin content is getting cached and served to normal users.

So, is there a way to cache by role? Is this even possible where the url of a page remains the same but the content changes according to the logged in role? Would it be better to alter the URL by adding something like ?admin=true to all relevant pages such that the varyByParam="*" attribute on our cache profile can do its job?

Thanks.

+3  A: 
<add name="defaultCache" duration="900" varyByParam="*" varyByCustom="membership" location="Any"/>

Global.asax:

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

    return string.Empty;
}
Jaroslav Jandek