Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 927
barrypicker Avatar asked Mar 16 '11 16:03

barrypicker


People also ask

What is the use of OutputCache attribute in MVC?

The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.

What is Varybyparam OutputCache?

It allows varying the cached output by GET query string or form POST parameters. For instance, you can vary the user-control output to the cache by specifying the user-control name along with either a query string or a form POST parameter. For more information, see Caching Multiple Versions of User Control Output.

Where is output cache stored?

The output cache is located on the Web server where the request was processed. This value corresponds to the Server enumeration value. The output cache can be stored only at the origin server or at the requesting client. Proxy servers are not allowed to cache the response.

How cache is implemented in ASP.NET MVC?

In ASP.NET MVC, there is an OutputCache filter attribute that you can apply and this is the same concept as output caching in web forms. The output cache enables you to cache the content returned by a controller action. Output caching basically allows you to store the output of a particular controller in the memory.


2 Answers

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.

like image 67
Aliostad Avatar answered Oct 21 '22 01:10

Aliostad


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

like image 35
BlackICE Avatar answered Oct 21 '22 01:10

BlackICE