|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Download Caching_Visio Diagram.zip - 1.38 MB Table of Contents
IntroductionIn our last project we have developed a sites for a large number of user, Larger number of client means large number of request to your web server, heavy load on network, cause performance issue. For solving this problem I have worked on using of caching on our web application. Now I think why should not I write one article on Code project on it. I am writing this article that what ever I have learned from my Practical Experience, Net Surfing and Different Books for completing my assignment . Most of the thinks are very common to maximum of reader, but I have tried to write some different way that it can be understandable by all beginners also. Main interest that I have found while writing the article that setting up the different location for caching, I have also given the corresponding Visio Diagram also. Hope You will definitely like it. What is Caching ?Web applications are accessed by multiple users. A web site can have an extremely low load for minimum number of client access which provide faster access of the sites or may be heavy load on the site can increase exponentially which can slow down the server as well as access of Sites . Slow access is the most common problem for any web site when it is accessed by a large number of client simultaneously. For resolve this problem we can have used high level of hardware configuration, Load Balancer , High bandwidth but load is not the only reason that make a web site is slow , so we need to provide such kind of mechanism which also provide fast data access and provide performance improvement of web site. Caching provide the solution. Caching is a technique where we can store frequently used data and Web pages are stored temporarily on local hard disks for later retrieval. This technique improves the access time when multiple users access a Web site simultaneously or a single user accesses a Web site multiple times. Caching for Web applications can occur on the client (browser caching), on a server between the client and the Web server (proxy caching / Reverse Proxy Caching), and on the Web server itself (page caching or data caching). Maximum time we chose web server to store cached data though it improved the performance but it does not solve our purpose every time. If we consider about load on Web Server we have to consider about the location that when the cached data should store. Following section will describe on different location of storing cached data. Different Caching LocationCaching in a web application can be done either on client side (Client Browser), In between Client and Server (Proxy & Reverse Proxy Caching ) and on Server Side( Data Caching/ Page Output Caching) . So we can classified caching location in 4 way
1. Client Caching : In Client Caching Client Browser perform caching by storing cached data on local disk as temporary file or browser internal memory. This provide quick access of some information by client which reduce the network load and server load also. This information can't be shared by other clients so it is client specific.
Fig 1.0 : Client Caching Advantages
Disadvantages
2. Proxy Caching : Main disadvantage of Client caching was data that are store on client browser are client specific. Proxy Caching technique used a dedicated server that store caching information in between client and web server in a shared location, that all client can used same shared data. The proxy server (e.g. Microsoft Proxy Server ) fulfills all the requests for the Web page without sending out the request to the actual Web server over the Internet, resulting in faster access.
Fig 1.0 : Proxy Caching Proxy caches are often located near network gateways to reduce the bandwidth . Some times Multiple Proxy Cache server is used for larger number of client. This is called Cache Array.
Fig 1.1 : Cache Array Advantages
Disadvantages
3. Reverse Proxy Caching : Some Proxy Cache server can placed in front of web server to reduce the number of requests that they receive. This allows the proxy server to respond to the frequently received requests and pass the other requests to the Web server. This is called a reverse proxy.
Fig 1.2 : Reverse Proxy Caching Advantages
Disadvantages
4. Web Server Caching : In Web server caching cached data stored inside the web server, Data caching and page caching used web sever caching mechanism.
Fig 1.3 : Web Server Caching Advantages
Disadvantages
Advantages and Disadvantages Of Caching Advantages
Caching Opportunity in ASP.NETASP.NET provides support for page, partial page (Fragment), and data caching. Caching a page that is dynamically generated, called page output caching . In page caching when a pages is cached that dynamically generated only the first time it is accessed. Any subsequent access to the same page will be returned from the cache.. ASP.NET also allow to Cached a portion of a page called Partial page caching or Fragment Caching . Other server data are cached (e.g. SQL Server data, XML Data ) that can be easily accessed with out re-retrieving that data using data Caching . caching reduce number of round trip of database and other data source. ASP.NET provides a full-featured data cache engine, complete with support for scavenging (based on cache priority) , expiration, and file and key , Time dependencies . There are two locations where caching can be used to improve performance in ASP.NET applications.
Fig 1.4 : Caching Opportunity in ASP.NET In above picture (1) is used for return caching of page that means its used in output caching and (2) save the round trip by storing the data using data caching. ASP.NET supports two types of expiration policies, which determine when an object will be expired or removed from the cache. These two policies are described as follows: Absolute expiration: Determines that the expirations occur at a specified time. Absolute expirations are specified in full-time format (hh:mm:ss). The object will be expired from the cache at the specified time. ASP.NET Supports Three Type of Caching
Different Types of Caching1 . Page Output Caching : Before starting Page Output caching we need to know the compilation process of a page, because based on the generation of page we should able to understand why should we used caching . ASPX Page compiled in two stage process. First, the code is compiled into the Microsoft Intermediate Language (MSIL). Then, the MSIL is compiled into native code (by JIT Compiler ) during execution. and entire code in an ASP.NET Web page is compiled into MSIL when we built the sites , but at the time of execution only the portion of MSIL Converted to native code which is need by user or user request, which also improve performance.
Fig 1.5 : ASP.NET Page Execution Process Now what ever we are getting , if there is some page which change frequently JIT need compile every time. So, We can use Page output for those page which content are relatively static. So rather than generating the page on each user request we can cached the page using Page output caching so that it can be access from cache itself. so, Instead of pages can be generated once and then cached for subsequent fetches.Page output caching allows the entire content of a given page to be stored in the cache.
Fig 1.5 : Page Output Caching As from given picture, when the first request is generated page is been cached and for same page request page should be retrieve from cache itself rather that regenerating the page. For Output caching , OutputCache directive can be added to any ASP.NET page, specifying the duration (in seconds) that the page should be cached. The code. Example<%@ Page Language="C#" %> <%@ OutputCache Duration='300' VaryByParam='none' %> <html> <script runat="server"> protected void Page_Load(Object sender, EventArgs e) { lbl_msg.Text = DateTime.Now.ToString(); } </script> <body> <h3>Output Cache example</h3> <p>Page generated on: <asp:label id="lbl_msg" runat="server"/></p> </body> </html>We also set Caching property from Codebehind also void Page_Load(Object sender, EventArgs e) { Response.Cache.SetExpires(DateTime.Now.AddSeconds(360)); Response.Cache.SetCacheability( HttpCacheability.Public); Response.Cache.SetSlidingExpiration(true); _msg.Text = DateTime.Now.ToString(); } We have to have mention the Duration and
Fig 1.6 : Caching multiple page based on parameters As above picture shown , if we are using query string for page and we need to cached all the page based on the query string, we have to use Example: <%@ OutputCache Duration="60" VaryByParam="*" %> page would cached for 60 seconds, and would create a separate cache entry for every variation of querystring Following table shown you the most commonly used and most important attributes of outputcache:
All the attributes that we specify in an Output Caching LocationAs I have already mention We can store cached data in different location like client, server or in between client and server , Now I am going to discuss how this is feasible to set location of cached data. If we store the cached data it save the page rendering time by fetching the data from catch. There is another way that we can save cached data on client browser , which reduce the network traffic. Following Table shown you the Location details . It show the location of cached and what should be the effects on Cache-Control and Expires Header.
For example, if you specified a value of ' Example<%@ OutputCache Duration='120' Location='Client' VaryByParam='none' %> This would save the cached for 120 second and cached data should not be saved on Server it should store only on client browser. 2 . Page Fragment Caching : ASP.NET provides a mechanism for caching portions of pages, called page fragment caching. To cache a portion of a page, you must first encapsulate the portion of the page you want to cache into a user control. In the user control source file, add an
Fig 1.7 : Fragment Caching Following Example shown you details of Fragment caching Example<!— UserControl.ascx —> <%@ OutputCache Duration='60' VaryByParam='none' %> <%@ Control Language="'C#'" %> <script runat="server"> protected void Page_Load(Object src, EventArgs e) { _date.Text = "User control generated at " + DateTime.Now.ToString(); } </script> <asp:Label id='_date' runat="'server'" /> Here I have user caching on user control, so when ever we used in a page , only partial page will be cached. 3. Data Caching : Caching of data can dramatically improve the performance of an application by reducing database contention and round-trips. Simply data caching store the required data in cache so that web server did not send request to DB server every time for each and every request which increase the web site performance. For data caching we need to cached those data which is accessible to all or which is very common data. The data cache is a full-featured cache engine that enables you to store and retrieve data between multiple HTTP requests and multiple sessions within the same application .
Fig 1.8 : Data Caching Above image showing how data can be accessed directly from data base server and how data retrieving using cache. Data caching is not only related with SQL Server, we can store other data source data (as shown on Fig 1.4). Now let see, how we can implement data caching in our web application. There are Three Different way to add data or object into cache. But based upon the situation we have to access it. These methods are
As we are getting, Now we should look into details of
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Situation | Suggested Caching Type |
|
The generated page generally stays the same, but there are several tables shown within the output that change regularly. |
Use Fragment Caching in this situation. |
|
The generated page constantly changes, but there are a few objects that don’t change very often. |
Use Data Caching for the objects. |
|
The generated page changes every few hours as information is loaded into a database through automated processes. |
Use Output Caching and set the duration to match the frequency of the data changes. |