Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 custom outputcache

I'd like to use caching in my application but the data I'm returning is specific to the logged in user. I can't use any of the out of the box caching rules when I need to vary by user.

Can someone point me in the right direction on creating a custom caching attribute. From the controller I can access the user from Thread.CurrentPrincipal.Identity; or a private controller member that I initialize in the controller constructor _user

Thank you.

like image 971
BZink Avatar asked Apr 08 '11 03:04

BZink


2 Answers

You could use the VaryByCustom. In Global.asax override the GetVaryByCustomString method:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (arg == "IsLoggedIn")
    {
         if (context.Request.Cookies["anon"] != null)
         {
              if (context.Request.Cookies["anon"].Value == "false")
              {
                   return "auth";
              }
              else
              {
                   return "anon";
              }
          }
          else
          {
             return "anon";
          }
    }
    else
    {
        return base.GetVaryByCustomString(context, arg);
    }
}

and then use the OutputCache attribute:

[OutputCache(CacheProfile = "MyProfile")]
public ActionResult Index()
{
   return View();
}

and in web.config:

<caching> 
    <outputcachesettings>             
        <outputcacheprofiles> 
            <clear /> 
            <add varybycustom="IsLoggedIn" varybyparam="*" duration="86400" name="MyProfile" /> 
        </outputcacheprofiles> 
    </outputcachesettings> 
</caching>
like image 164
Darin Dimitrov Avatar answered Nov 12 '22 04:11

Darin Dimitrov


The Authorize Attribute has some interesting things going on regarding caching for authorized vs unauthorized users. You may be able to extract it's logic and modify it to cache per authorized user, instead of just per-"the user is authorized".

Check out this post:

Can someone explain this block of ASP.NET MVC code to me, please?

like image 20
cwharris Avatar answered Nov 12 '22 04:11

cwharris