Caching ASP.NET MVC

-> Caching provides a way of storing frequently accessed data and reusing that data. This is an effective way of improving web application’s performance.

Advantages of caching
-> Reduce hosting server round-trips. When content is cached at the client or in proxies, it cause minimum request to server.
-> Reduce database server round-trips. When content is cached at the web server, it can eliminate the database request.
-> Reduce network traffic. When content is cached at the client side, it also reduces the network traffic.
-> Avoid time-consumption for regenerating reusable content. When reusable content is cached, it avoids the time consumption for regenerating reusable content.
-> Improve performance. Since cached content reduce round-trips, network traffic and avoid time consumption for regenerating reusable content which cause a boost in the performance.

OutputCache filter

-> You can enable output cache by adding an [OuputCache] attribute to either an individual controller action or an entire controller class.

-> By default, when you use the [OutputCache] attribute, content is cached in three locations: the web server, any proxy servers, and the web browser. You can control exactly where content is cached by modifying the Location property of the [OutputCache] attribute.

Varying the Output cache
-> In some situations, you might want different cached versions of the very same content. Imagine, for example, that you are creating a master/detail page. The master page displays a list of movie titles. When you click a title, you get details for the selected movie.
->If you cache the details page, then the details for the same movie will be displayed no matter which movie you click. The first movie selected by the first user will be displayed to all future users.
->You can fix this problem by taking advantage of the VaryByParam property of the [OutputCache] attribute. This property enables you to create different cached versions of the very same content when a form parameter or query string parameter varies.

Creating a Cache Profile
->As an alternative to configuring output cache properties by modifying properties of the [OutputCache] attribute, you can create a cache profile in the web configuration (web.config) file. Creating a cache profile in the web configuration file offers a couple of important advantages.
->First, by configuring output caching in the web configuration file, you can control how controller actions cache content in one central location. You can create one cache profile and apply the profile to several controllers or controller actions.

Leave a Reply

Your email address will not be published. Required fields are marked *