Caching In ASP.NET

Caching In ASP.NET
Techiio-author
Written by Sagar RabidasJanuary 24, 2022
11 min read
ASP .Net
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Today start discussion Caching in ASP.NET.

Caching in ASP.NET

Caching in asp.net is the capability to keep a website web page or records within the reminiscence for rapid access. In asp.net, you need not hit the server for the equal response over and over. Shop it in the computer reminiscence and fetch it faster. Of course, this works handiest with static statistics because dynamic facts vary with each request made to the server.

How does Caching work in ASP.Net?

It's miles very vital to recognize the technique of asp.Net caching net pages or data. To apprehend this, we might need to understand the .Net compilation system so that we get a fair understanding of while and where to cache the pages for optimum performance. Asp.Net page code is compiled in two tiers the MSIL degree and the jit degree. At the MSIL level, the page code written in high-level languages is compiled into Microsoft intermediate language. This occurs on every occasion we build our project. The complete internet site or project is compiled into MSIL each time we build. Inside the jit stage, the MSIL code is then converted into native device code by the simplicity in the time compiler. This occurs at some point in the execution of the page. But, now not the complete project is transformed into local code all of the time. Only the pages asked via the person are converted from MSIL to native code during the execution. This protects a variety of network bandwidths and improves overall 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.

Types

Following are the different types of caching explained in detail:

  • Page Output Caching:-

Page output caching means caching the complete output of the requested web page. Every time a person requests an asp.Net page, the jit compiler compiles the applicable MSIL code and generates the native code output to be sent as a response to the patron. This means the jit compiler has to generate the native code whenever the page is asked. What if the web page is static? What if the web page output is the same after every compilation? We can shop a variety of compilation time and resources if we save the generated local code inside the page cache. The subsequent requests for the identical web page may be fetched from the cache rather. That is termed web page output caching. To obtain web page output caching, we need to specify the output cache directive within the asp.net code with length in seconds.

<%@ Page Language="C#" %>
<%@ OutputCache Duration='1000' VaryByParam='none' %>
<html>
<!-- Your Page Code Here -->
<html>
  • Page Fragment Caching

We've got visible how to cache a static page. What if the page is dynamic and varies with users? Right here comes web page fragment caching. It permits a developer to cache precise sections of the page. This facilitates while you need to cache the header and the footer, which is normally static for each consumer. To obtain web page fragment caching in asp.net, you must encapsulate the fragment code in a separate user manage. Then add the identical output cache directive in the consumer control. Whilst the person manage is loaded alongside the web page, a replica of it is maintained inside the cache. As a result, all the next references to the identical consumer managed at the same page or a special page would be fetched from the cache.

<%@ Control Language="C#" %>
<%@ OutputCache Duration='1000' VaryByParam='none' %>
<script runat="server" >
<!-- Your User Control Code Here -->
</script>
  • Data Caching

Data Caching is the mechanism of storing the required facts, which is regularly accessed, in a cache. This will dramatically improve the performance of the utility. This is due to the fact statistics caching saves the database round trip calls, which might be infamous for eating the maximum of the time. While frequently accessed and infrequently modified records are cached, the server fetches the statistics from the cache as opposed to making database calls. This may also prevent a little money as database calls to the cloud-hosted information servers rate you in step with request basis. Data caching in asp.net is a complete-fledged engine in itself. To reap information caching in our asp internet page, we want to use the cache item.

Cache["ProductName"]="My Product";
Label1.Text= Cache["ProductName"].ToString();

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.

  1. There is an informative page, which generates reports based on the data in the database. The database tables are refreshed every few hours.
  2. 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.
  3. 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.
  4. Page Fragment Caching can be used to cache the static legend and explanation data only.
  5. 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.
  6. Data Caching can be used to cache the data and dynamically generate user-requested charts and graphs.
asp net
Caching In ASP NET
Caching
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance