ASP.Net MVC 3.0 Attribute [OutputCache]
When creating an ASP.Net MVC 3.0 application, I can include an OutputCache attribute on an action, and define the duration the cache will persist. After this duration, any activity causing the action to fire will cause this cached result to be discarded and refreshed.
My question is... Is this cached output global for all user sessions or is this cache option specific to each session. In otherwords, if user1 makes a request where an action with a OutputCache attribute is set to 1 hour starts their request at say noon (12:00 PM). User2 makes a request to the same action, but at say noon-thirty (12:30 PM). Will the cached result be the same for both users, and if so, will the cached output for user2 be refreshed at 1:00 PM?
Depends. If your URL contains any user specific parts (eg ~/blogs/userid/1
), then yes, otherwise it is not session specific. You can fine tune it based on vary by param, ...
Caching in ASP NET MVC is not different to Web Forms, it is just the same infrastructure which is URL-based .
You can use varybyparam like Aliostad said, the duration would then be session specific, otherwise it is not.
[OutputCache(Duration=10, VaryByParam="none")]
http://www.asp.net/mvc/tutorials/improving-performance-with-output-caching-cs
您可以使用VaryByCustom
轻松使其与Session
相关并返回SessionId
(非常便宜的操作)。