We all are aware about MVC. Today, in this blog post we will see how the performance of MVC can be improved with Caching. MVC is all about actions. Let’s say whenever any request is generated, a call is initiated to the controller.
Then we get the result and bind the view. Let’s say if the user is requesting same view 50 times. Well, this is a situation that occurs hardly. In certain cases, it is necessary to use the most useful thing known as Caching. This ensures that there are no actions that require what will happen within the same time frame.
So, what is Caching?
Caching is basically a technique that is used to enhance performance. It helps to avoid frequent actions that already occurred with the output Caching. The view that is available won’t be seen again if it is available in limited time period.
It will show the one that is rendered already. It will cache those contents that are returned by controller action. Let’s assume that you have a controller which will return the debit credit transaction from the database.
Generally, when the user hits a controller then it will reduce the DB and collect the data. However, this is not needed to hit the DB again as the data will be same for the week. Hence, we can set the output caching so that within the time interval the controller contents are available in cache which will help viewers to render cached data.
How to activate Output Caching?
First of all, a simple application is developed in Visual Studio.
Then, control is created in MVC application. Once the controller is ready then we need to develop a simple view and then we will change the index action in the controller in the following code snippet.
public ActionResult Index()
Now, returning back from the current code time from the controller, we will refresh the view constantly. For example; 2 times in 5 minutes. This means within five minutes the controller is hit twice.
So, where are the cached Contents?
Here, we will see all the locations that can save our cache data. Generally, the contents are cached in these three locations:
- Web Server
- Proxy server
- Web Browser
One can always decide where one wants to save the cached data. Let’s say whether it is in the server, client or both server and client with the use of location property. Here, we can keep any location.
There are certain situations that one must not cache in the server. For example; we have a controller which returns the country name of the user that is logged in the application. It is not necessary to cache any personal data in the server.
It is advisable to cache the data into client. With the use of Output Cache Location; one must include namespace using System Web UI.
Another way of achieving the caching is to use Cache profiles. It is nothing but creating a tag in web configuration file and then apply it for various controllers. In this way, one can have the same cache profile for various controls.
Then it becomes easy to make changes in the cache profile just like the tag in the web configuration file. All the changes in cache profile will get easily applied.
So, this is all about caching. In our next post; we will discuss how caching can be done in ASP.net 5 and MVC 6 very soon.
Try caching now. Let us know how this blog post was useful to you. Do share your experience with us. Stay connected to know more about asp.net from Softqube Technologies; a well known ASP.net application development company in India.