ASP.NET MVC 缓存因身份验证而异

我在我最近 推出的微型网络应用程序上使用 ASP.NET Preview 5(即将升级到 Beta)和 LINQ2SQL一>.

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

我为自己的作品感到非常自豪,直到 Silverlight.net 在第一页上展示了它,而且它开始吸引了越来越多的访问者.

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.

出于某种原因,用户有时会收到 LINQ2SQL 抛出的Specified cast invalid" - 这是因为未经身份验证的用户被视为Guest"用户,并且控制器正在为每次点击加载有关用户Guest"的信息.

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.

在第一页,顶部有一个欢迎 [USERNAME]",可以使用 甜甜圈缓存.

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

然而,在中间,有一个What is Dudlers.com"块只应向未经身份验证的用户显示,另一个关于你"块只应向经过身份验证的用户显示.这些块太复杂,无法使用 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.

是否可以选择根据身份验证指定缓存?就像是:[OutputCache(VaryBy = Authentication)]

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

或者是否有其他非 ASP.NET MVC 解决方案?(可能会调整 MS SQL)

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

我希望我的问题很清楚.我在同一台专用服务器上运行 Windows 2003 和 MS SQL 2005 Express.

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

谢谢!

推荐答案

您不必使用甜甜圈缓存机制.ASP.NET 具有 VaryByCustom 选项,允许您确定如何改变缓存.

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.

查看更多信息:http://aspadvice.com/blogs/ssmith/archive/2007/10/29/VaryByCustom-Caching-By-User.aspx

应该适用于 ASP.NET MVC.

Should work fine for ASP.NET MVC.

相关文章