
In the above code, we have use Attribute OutputCache, it is used in ASP.NET MVC Application is used to cache the content returned by a controller action.
Asp.net core private cache code#
In your HomeController place the below code for Index ActionMethod Select MVC template to generate basic HomeController and other details. Let's create a project in Visual Studio and take a closer look at caching in MVC :Ĭreate a project in your Visual Studio(2017 in my example), by opening Visual Studio and clicking "File"-> "New"-> "Project".

In this case, the view will be retrieved from the cache instead of being regenerated from the controller action. Now, suppose we already know these records are not going to change for a certain interval of time, let's say 50 seconds, hence you can take advantage of the output cache to avoid executing a database query every time a user invokes the same controller action.

That way, the same content does not need to be generated each and every time the same controller action is invoked.įor example, you have an ASP.NET MVC application, which displays a list of employees, these set of database records must be retrieved from the database by executing a database query, each and every time a user invokes the controller action it returns the EmpList view.

Hence, any future request coming for the same action in that controller will be returned from the cached result. In ASP.NET MVC, Output caching basically allows you to store the output of a particular controller in the memory. Caching in ASP.NET MVC using OutputCache ActionFilter Subsequent requests for cached content can then be fulfilled from a cache closer to the user instead of sending the request all the way back to the web server. So, Web caching works by caching the HTTP responses for requests according to certain rules. The content, which includes HTML pages, images, files and Web objects, is stored on the local hard drive in order to make it faster for the user to access it, which helps improve the efficiency of the computer and its overall performance.Īs we are going to discuss caching related to asp.net MVC means a web application, here caching improves the performance of our web application by storing file, images etc on cache and not hitting the server for the same query again and again, for some duration of time(this duration is defined by us). Practically, this is an effective way for improving web application’s performance. Now before we proceed further, if you are new to web development you may like to know what is caching, so Caching provides a way of storing frequently accessed data and reusing that data. The output cache enables you to cache the content returned by a controller action, and it helps us to improve web-application performance.

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.
