I cache different versions on server of some pages using cache profile, for differing versions I am using VaryByCustom parameter which is overloaded and returns one version for registered user and another for common one. As it turns out my registered users will soon have very different information on page. My question is it possible to say if user is registered than do not cache anything?
You could try this:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if ("User".Equals(custom, StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrWhiteSpace(Thread.CurrentPrincipal.Identity.Name))
return Guid.NewGuid().ToString(); // do not cache anything
return string.Empty; // cache for non-signed-in users
}
return base.GetVaryByCustomString(context, custom);
}
Reply to comments
GUID is Globally Unique IDentifier. Each time you invoke Guid.NewGuid().ToString()
, you will get a pretty darn unique string. Technically, the result will still be cached, however it will be cached using the unique string. So, chances are infintesimally low that 2 signed-on users would ever see the same cached result. In fact, chances are equally low that the same signed-on user would ever see a cached result for the page.
Another approach, if you do not want to cache anything at all, would be to implement caching manually. However, I don't think you can tell an action method to "cache for non-signed in users, but don't cache for signed-on users" using the OutputCacheAttribute
.