Skip to content

Instantly share code, notes, and snippets.

@brainded
Last active August 29, 2015 14:06
Show Gist options
  • Save brainded/20b623ddbcee3542afe5 to your computer and use it in GitHub Desktop.
Save brainded/20b623ddbcee3542afe5 to your computer and use it in GitHub Desktop.
Utilize Output Cache with User Identity. Found on StackOverflow: http://stackoverflow.com/questions/17187736/output-cache-per-user
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TwitterPoc.Services;
namespace BobsBurgers.Controllers
{
public class BobsBurgersController : Controller
{
//To use the user cache, the cache needs to be server side sadly
[OutputCache(VaryByCustom="User", Location = OutputCacheLocation.Server)]
public ActionResult Index()
{
return View();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace BobsBurgers
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
...
}
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom.Equals("User", StringComparison.InvariantCultureIgnoreCase))
{
return string.Format("User:{0}", context.User.Identity.Name);
}
return base.GetVaryByCustomString(context, custom);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment