sql-server - working - types of caching in mvc




ASP.NET MVC Caching vary by authentication (2)

I'm using ASP.NET Preview 5 (will upgrade soon to Beta) with LINQ2SQL on my recently launched tiny webapp.

I was so proud of my work until Silverlight.net featured it on the first page and it started receiving a more than humble number of visitors.

For some reason, users are sometimes getting "Specified cast invalid" thrown by the LINQ2SQL - this is because unauthenticated users are handled as a "Guest" user, and the Controller is loading information about the user "Guest" for each hit.

In the first page, there is a "Welcome [USERNAME]" on the top which can be easily cached using Donut Caching.

However, down in the middle, there is a "What is Dudlers.com" block which should only be shown to unauthenticated users, and another "About You" block which should only be shown to authenticated users. These blocks are too complicated to be cached using Donut Caching; they are way too different, it will be very messy.

I would like to cache the first by depending on the authentication status of the user. So unauthenticated users (needless to say, the majority) get the "Guest" cached page, and other authenticated users are shown a fresh non-cached page.

Is there an option to specify caching varied by authentication? something like: [OutputCache(VaryBy = Authentication)]

Or is there another non-ASP.NET MVC solution? (perhaps tuning MS SQL)

I hope my question is clear. I'm running Windows 2003 and MS SQL 2005 Express on the same dedicated server.

Thank you!



Caching partials based on logged in user

You can use OutputCacheAttribute to affect output caching on on a controller or action-by-action basis, and use VaryByCustom.

[OutputCache(Duration = 60, VaryByParam = "*", VaryByCustom="userName")]

Place that on the controllers, then go into your Global.asax.cs and override GetVaryByCustomString:

public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
  if(arg.ToLower() == username && context.User.Identity.IsAuthenticated) return context.User.Identity.Name;

  return base.GetVaryByCustomString(context, arg); 
}




caching