Sql – ASP.NET MVC Caching vary by authentication

asp.net-mvccachinglinq-to-sqlsql server

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!

Best Answer

You don't have to use the donut caching mechanism. ASP.NET has the VaryByCustom option, that allows you to determine how to vary the caching.

Check this out for more info: http://aspadvice.com/blogs/ssmith/archive/2007/10/29/VaryByCustom-Caching-By-User.aspx

Should work fine for ASP.NET MVC.