具有动态片段的 ASP.NET MVC 输出缓存
如果用户已登录等等,我如何缓存整个页面,除了顶部的一点,上面写着“欢迎回来,Matt!|注销”
?
我正在使用 ASP.NET MVC 2。
How could I cache an entire page except a bit at the top which says something along the lines of "Welcome back, Matt! | Log Out"
if the user is logged in and so-on?
I'm using ASP.NET MVC 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您想要实现的目标称为甜甜圈缓存或缓存替换。从 ASP.NET MVC 2 开始,没有内置的帮助程序来支持这种情况。据我所知,这是 MVC v.1 中计划的功能,但在发布过程中被删除了。有关详细信息,请检查此链接 http: //haacked.com/archive/2008/11/05/donut-caching-in-asp.net-mvc.aspx,甜甜圈缓存是否可以与 ASP.NET MVC 一起正常工作?。
Oleg 在这里提到的 VaryByParam 选项对于您的情况来说不是一个好主意。如果您有 VaryByParam,则针对参数的每个不同值(在您的情况下针对每个用户名),页面的不同版本将被放入缓存中。
就我个人而言,我会考虑缓存数据,而不是页面的整个输出。
What you are trying to achieve is called donut-caching or cache substitution. As of ASP.NET MVC 2 there is no built in helper to support this scenario. As much as I know it was a planned feature in MVC v.1 but it was dropped somewhere in the way to the release. For more info check this links http://haacked.com/archive/2008/11/05/donut-caching-in-asp.net-mvc.aspx, Is Donut Caching working properly with ASP.NET MVC?.
VaryByParam option that is mentioned by Oleg here is not a good idea in your case. If you have VaryByParam a different version of the page will be put in the cache for every different value of the parameter (in your case for every user-name).
Personally I would think of caching the data, not the whole output of the page.
可能有帮助
或为
VaryByParam
提供一些其他值。请参阅 http://msdn.microsoft.com/en-us/library/hdxfb6cy .aspx,http ://blog.maartenballiauw.be/post/2008/06/Creating-an-ASPNET-MVC-OutputCache-ActionFilterAttribute.aspx 和 http://blogs.microsoft.co.il/blogs/gilf/archive/2010 /07/18/asp-net-output-cache-provider.aspx。此外,如果您的起始页不依赖于用户,则可以将起始页替换为带有空字段(隐藏 div)的非常静态的欢迎页面,而不是“欢迎回来,马特! | 注销”。之后就可以在客户端发起一个填写用户名的ajax请求。这种欢迎页页面可以很好的被缓存。
Probably helps
or with some other value for the
VaryByParam
. See http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx, http://blog.maartenballiauw.be/post/2008/06/Creating-an-ASPNET-MVC-OutputCache-ActionFilterAttribute.aspx and http://blogs.microsoft.co.il/blogs/gilf/archive/2010/07/18/asp-net-output-cache-provider.aspx.Moreover, if you have the start page which is not user depended, it is possible to replace the start page with a very static welcome page with the empty field (hidden div) instead of "Welcome back, Matt! | Log Out". After that an ajax request for filling of the user name can be started at the client side. This kind of the welcome page page can be very good cached.
不支持!= 不可能
http://blog.maartenballiauw.be/post/2008/07/01/Extending-ASPNET-MVC-OutputCache-ActionFilterAttribute-Adding-substitution.aspx
http://www.klopfenstein.net/lorenz.aspx/output-donut -caching-attribute-asp-net-mvc-partial-requests
http://haacked.com/archive/2009/05/12/donut-hole-caching.aspx
Not Supported != Not Possible
http://blog.maartenballiauw.be/post/2008/07/01/Extending-ASPNET-MVC-OutputCache-ActionFilterAttribute-Adding-substitution.aspx
http://www.klopfenstein.net/lorenz.aspx/output-donut-caching-attribute-asp-net-mvc-partial-requests
http://haacked.com/archive/2009/05/12/donut-hole-caching.aspx
这里有一个解决方案:
*将 OuptutCache 属性添加到通常管理整个视图的控制器中:
*对于您不想进行缓存的部分,使用 jquery + ajax 请求(使用自己的控制器)加载它并且没有 OutputCache 属性):
将从输出缓存中检索视图,加载后,将执行获取登录信息的请求。希望这将是一个非常快的请求,并且用户不会注意到延迟。
Here you have a workaround solution:
*Add the OuptutCache attribute to the Controller that manages the whole view as usually:
*For the part that you don't want to do caching, load it using jquery + an ajax request (with its own controller and without the OutputCache attribute):
The view will be retrieved from the Output Cache and, once it is loaded, a request to obtain the login info will be performed. Hopefully, it will be a very quick request and the user will not notice the delay.
通过 nuget 获取:
http://mvcdonutcaching.codeplex.com/
为 LogOnPARTIAL 添加操作,以便您可以从 Html 更改它。 _Layout.cshtml 中 Html.Action 的部分
true
是一个排除参数,表示从缓存中排除此参数。即使该操作所在的页面已被缓存,该操作也会被调用。这是甜甜圈中未缓存的“洞”。在您想要缓存的页面(例如 About.cshtml)上,应用 DonutOutputCache 属性。这允许新框架在缓存页面时检查页面,并在您排除操作的位置添加标志。
好处是 _LogOnPartial 不会被缓存,并且会为不同的用户刷新,而页面的其余部分会被缓存,并且 About() 操作不会运行。您甚至可以在使用 DonutOutputCache 属性创建的 _LogOnPartial 操作上配置缓存,但间隔频率更高或更低,或者因其他参数而异。这允许您组成部分页面,并且为每个部分独立配置缓存刷新逻辑。
在我看来,这个工具正是我想象的 MVC 中缓存的实现方式。
Get this via nuget:
http://mvcdonutcaching.codeplex.com/
Add an action for LogOnPArtial, so you can change it from Html.Partial to Html.Action in the _Layout.cshtml
The
true
is a exclude parameter that says, exclude this from caching. The action will be called even if the page it is in is cached. This is the "hole" in the donut that is not cached.On your page, such as About.cshtml that you want cached, apply DonutOutputCache attribute. This allows the new framework to inspect the page as it's caching it, and add flags where you've excluded actions.
The nice thing is the _LogOnPartial is not cached and will refresh for different users while the rest of the page is cached and the About() action will not be run. You could even configure caching on the _LogOnPartial action you created using the DonutOutputCache attribute, but a more frequent or less frequent interval, or vary by some other param. This allows you to compose pages of partials, and the cache refreshing logic is independently configured for each partial.
IMO this tool is exactly how I imagined caching in MVC should have been implemented.