ASP.Net MVC 3.0 Attribute [OutputCache] - Is this global, or by session?
Asked Answered
C

3

7

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?

Chinkiang answered 16/3, 2011 at 16:21 Comment(0)
F
6

Depends. If your URL contains any user specific parts (e.g. ~/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.

Footie answered 16/3, 2011 at 16:24 Comment(1)
I have a caching question here. Appreciate your inputRosemarie
H
5

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

Henhouse answered 16/3, 2011 at 16:29 Comment(2)
I like your answer because you provided an example. Thank you. +1. Because Aliostad actually provided the same answer first, I am awarding @Footie the answer.Chinkiang
The only thing that VaryByParam can do is make unique cached copies based on query string or form values. The caching would be session-specific ONLY if the user's ID or some other identifying value is included in query or form parameters.Bunde
G
0

You can easily make it Session dependent using a VaryByCustom and returning the SessionId (very inexpensive operation).

Gooch answered 9/10, 2013 at 23:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.