In this blog post, I will document the caching mechanism provided in ASP.NET MVC3. MVC allows us to cache results from the whole controller or just some selected actions. It also provides parameters which help in configuring caching according to our needs.
The attribute used to enable caching is OutputCache. It can be applied to controllers or actions. Within OutputCache attribute we can specify the following arguments -
The attribute used to enable caching is OutputCache. It can be applied to controllers or actions. Within OutputCache attribute we can specify the following arguments -
- Duration - in seconds to keep the cache.
- Location - specifies where the content should be cached. Possible values -
- Any - default
- Client - stored on client browser. It should be used in case you have personalized content per user.
- Downstream - any proxy servers or client browser.
- Server - stored on web server. It should be used in case the content is not user dependent.
- None - no caching
- ServerAndClient - caching on both locations
- Nostore - informs proxy servers and browsers to not store a permanent copy of cached content
- VaryByParam - allows to cache based on different parameters. It can increase the size of you cache dramatically, so it should be carefully used. Possible values -
- * - Create a different cache whenever any parameters change
- none - never create different cached versions
- parameters separated by commas - create a different cache whenever any parameters in this list change.
- CacheProfile - takes a string name for a profile named in config. The advantage of this is that its easier to manage all the cache settings in one place. Also, they can be changed without the need to recompile the application.
MVC3 CacheProfile: In MVC3, CacheProfile doesn't work for child actions. In that scenario, we have to use the other arguments such as Duration.