Introduction to Caching in ASP.NET
Caching in ASP.NET is the ability to store a website page or data in the memory for rapid access. In ASP.NET, you need not hit the server for the same response again and again. Store it in the computer memory and fetch it faster. Of course, this works only with static data because dynamic data varies with every request made to the server.
What is Caching in ASP.Net?
The .Net framework also provides the caching facility so that the performance of your application can be improved. Now you ask, ASP .Net is used for creating user-dependent dynamic web pages, then how does it cache content?
Let us understand this with two scenarios – first, where the page is the user’s dashboard and second, where it has product listings. Scenario two is independent of the user, the products are the same with every request to the server, and thus the whole page can be cached. Again, the price and availability of products are variable, which can be handled by timely updating the cache. Scenario one depends on the user; the dashboard for one user may not be similar to another user. But there still are a few components, such as images, legends, headers and footers, which can be cached to improve performance.ASP.Net enables the developers to handle both kinds of scenarios efficiently in their application.
How Caching works in ASP.Net?
It is very important to understand the process of ASP.Net caches web pages or data. To understand this, we would need to understand the .Net compilation process so that we get a fair understanding of when and where to cache the pages for optimum performance. ASP.Net page code is compiled in two stages the MSIL stage and the JIT stage. In the MSIL stage, the page code written in high-level languages is compiled into Microsoft Intermediate Language. This happens whenever we build our project. The whole website or project is compiled into MSIL every time we build. In the JIT stage, the MSIL code is then converted into native machine code by the Just In Time compiler. This occurs during the execution of the page. However, not the whole project is converted into native code all the time. Only the pages requested by the user are converted from MSIL to native code during the execution. This saves a lot of network bandwidth and improves performance.
Now, which code should we cache, when should we cache, and where?
ASP.Net has a full-featured engine dedicated to caching. It has features such as time dependency, file and key dependency, expiration, data scavenging, etc. We would not go into these details in this article. We need to understand that we can cache our pages and data in two locations to improve the performance of our ASP.Net application. The first location is the Page Cache section residing in the ASP.Net server. This store page output caches and page fragment caches, basically ASPX pages. Whenever a mostly static page is requested, a copy of the generated native code is stored in the Page Cache section. This saves the JIT compilation time during subsequent page requests. The second location is the Data Cache. This stores the data fetched from the data servers or other servers. Storing a copy of this data helps in saving future network calls to the database servers or other third-party servers. A few examples of the data cached are SQL Server data, XML data, JSON data, third-party API responses, etc.
Following are the different types of caching explained in details:
1. Page Output Caching
Page Output Caching means to cache the complete output of the requested page. Whenever a user requests an ASP.Net page, the JIT compiler compiles the relevant MSIL code and generates the native code output to be sent as a response to the client. This means that the JIT compiler has to generate the native code every time the page is requested. What if the page is static in nature? What if the page output is the same after every compilation? We can save a lot of compilation time and resources if we store the generated native code inside the Page Cache. The subsequent requests for the same page can be fetched from the cache instead. This is termed as Page Output Caching. To achieve Page Output Caching, we need to specify the OuputCache directive in the ASP.Net code with duration in seconds.
<%@ Page Language="C#" %>
<%@ OutputCache Duration='1000' VaryByParam='none' %>
<!-- Your Page Code Here -->
2. Page Fragment Caching
We have seen how to cache a static page. What if the page is dynamic and varies with users? Here comes Page Fragment Caching. It enables a developer to cache specific sections of the page. This helps when you want to cache the header and the footer, which is mostly static for every user. To achieve Page Fragment Caching in ASP.Net, you must encapsulate the fragment code in a separate user control. Then add the same OuputCache directive in the user control. When the user control is loaded along with the page, a copy of it is maintained in the cache. Thus, all the subsequent references to the same user control on the same page or a different page would be fetched from the cache.
<%@ Control Language="C#" %>
<%@ OutputCache Duration='1000' VaryByParam='none' %>
<script runat="server" >
<!-- Your User Control Code Here -->
3. Data Caching
Data Caching is the mechanism of storing the required data, which is frequently accessed, in a cache. This can dramatically improve the performance of the application. This is because data caching saves the database round trip calls, which are notorious for consuming most of the time. When frequently accessed and seldom changed data is cached, the server fetches the data from the cache instead of making database calls. This can also save you some money as database calls to the cloud-hosted data servers charge you per request basis. Data Caching in ASP.Net is a full-fledged engine in itself. To achieve data caching in our ASP web page, we need to use the Cache object.
Why do we need Caching in ASP.Net?
Having understood the caching process in ASP.Net, let us look at some practical examples where caching is implemented in real-time scenarios.
- There is an informative page, which generates reports based on the data in the database. The database tables are refreshed every few hours.
Page Output Caching can be used in such a scenario with a duration of the cache set to match the frequency of the data refresh job.
- There is a page that shows several tables and data which constantly change. However, the legends and the explanation of the data remain the same.
Page Fragment Caching can be used to cache the static legend and explanation data only.
- There is a user dashboard that is user-customized and generates graphs and charts on user requests. The data used to generate the graphs and charts changes infrequently.
Data Caching can be used to cache the data and dynamically generate user-requested charts and graphs.
Thus, we have learned that caching comes a long way in improving the performance of our ASP.Net application. This was an introductory level article on caching in ASP.Net. There is more to it to explore. It is recommended to learn more about caching to improve the application’s performance further.
This has been a guide to Caching in ASP.NET. Here we discuss the basic concept; it’s working along with types of Caching, respectively. You can also go through our other suggested articles to learn more –