Fragment caching in asp net Part 12204:33

  • 0
Published on October 27, 2017

Text version of the video

Slides

All ASP .NET Text Articles

All ASP .NET Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video we will discuss about fragment caching. We discussed the basics of caching in Parts 119, 120, and 121. Please watch Parts 119, 120, and 121 from asp.net video tutorial before proceeding with this video. Link for asp.net video tutorial

Caching parts of webform is called as partial caching or fragment caching. In a web application development, there might be scenarios where most parts of the web page changes, but only a specific section of the page is static. Let’s say that specific section also takes a long time to load. This is an ideal scenario, where fragment caching can be used.

We will be using tblProducts table to understand fragment caching. Please use the script from by blog from the link below to create and populate table tblProducts

Steps to fragment cache a webform
1. Encapsulate that sepcific sections of the page that does not constantly change into a user control.
2. Use the OutputCache directive on the user control to specify the cache settings.
3. Drag and drop the user control on the webform.

Code samples used in the demo, can be found on my blog at the following link

Notice that, when you run the web application, the user control is cached. The server time of the user control is not changed on refreshing the page but the other times on the user control and page changes. This proves that, the user control is cached, but not the rest of the webform. Fragment caching is that easy.

“Shared” attribute of the “OutputCache” directive:
“Shared” attribute can be used with “OutputCache” directive, to cache a single response from a user control for use on multiple Web forms. By default, ASP.NET caches a separate response for each Web form that uses a cached user control. Let us understand this with an example.

Add another webform with name WebForm2.aspx to the asp.net web application. Copy and paste the relevant HTML and code from WebForm1.aspx onto WebForm2.aspx.

Run the application and navigate to WebForm1.aspx. It takes around 5 seconds to load. Now navigate to WebForm2.aspx, and notice that WebForm2.aspx also takes 5 minutes. Since by default, ASP.NET caches a separate response for each Web form that uses a cached user control, both WebForm1.aspx and WebForm2.aspx, are taking 5 seconds.

Now, let’s set the Shared=”true” for “OutputCache” directive on “UCProductsControl” user control. This should cache a single response from the user control for use on WebForm1.aspx and WebForm2.aspx.

Run the application and navigate to WebForm1.aspx. It takes around 5 seconds to load. Now navigate to WebForm2.aspx, and notice that WebForm2.aspx loads instantly. Also notice that, the server time of the user control is same on both the webforms. This proves that both WebForm1.aspx and WebForm2.aspx are using the single cached response of the user control.

Enjoyed this video?
"No Thanks. Please Close This Box!"