|
Caching:
Caching dramatically improve the performance of your web site. Instead of executing a page each time the page is requested, you can cache the output of the page and send the cached copy to satisfy the browser requests.
There are 3 type of caching:
- Paged Output caching
- Fragment caching
- Data caching
Page Output caching :
To improve the performance of the web site the page are cached and next time when the browser make the request of the same page the request is handled by the cache module and the page response is through the cached page.
The following code will enable output caching:
<%@ OutputCache Duration=”60” varyByParam=”none” VaryByHeader=”user-agent” VaryByCustom=”Browser” Location=”Client”%>
1. Varying cache contents by parameter:
None : indicates the different cached version of a page should not be created when the page is requested with different parameters.
* : indicates the different cached version of a page should be created whenever the page is requested with different parameters.
<%@ OutputCache Duration=”60” varyByParam=”*” %>
Eg:
<%@ OutputCache Duration=”60” varyByParam=”PID; PCatId” %>
This will creates different cached copies of the page depending on the values of the PID and PCatId parameters.
Note: Each cached copy of the page eats away at memory. So the more versions of the page that you cached, the more server resources are consumed.
2. Varying cache by Header:
To cache different version of a page depending on the browser.
<%@ OutputCache Duration=”20” varyByParam=”*” VaryByHeader=”User-Agent”%>
3. Varying cache contents by Custom string:
This attribute enables you to control the sensitivity of the page output caching.
<%@ OutputCache Duration=”60” varyByParam=”*” varyByCustom=”Browser”%>
4. Storage for cached page:
The following are the location values used to cache the pages on the desied location.
- Default: The cached page is stored on both places i.e server and client.The page is cached in the server's memory, and the proper headers are sent to the browser requesting the page to cause the browser to create a cached copy.
- Any: This is the default value. The page can be stored in any location i.e server, client location or any downstream server.
- Client: The output of the page is cached at the browser.
- Downstream: Page cached at any downstream server.
- None: No page caching should be performed.
- Server: The page cached at the server location.
<%@ OutputCache Duration=”20” varyByParam=”*” Location=”client” %>
Fragment Caching:
The fragment caching allows user to cache the part of the page rather than the whole part. The fragment caching is implemented with user controls. Here user can define different areas of a page by creating separate user controls for each area of the page. With in each user control, you can use the OutputCache directive to cache the page.
Eg. UserBanner.ascx
<%@ Outputcache duration=”60” varybyparams=”*” %>
<asp: DataGrid id=”dgrdproduct” runat=”server” />
Home.aspx
<%@Register Tag Prefix=”userCtl” tagname=”userbanner” src=”userBanner.ascx"%>
<userCtl:userbanner runat=”server”/>
Note: This user control can be used in any number of pages, making it cached will help us to improve the performance of the site.
Data Caching:
Caching the data displayed on a page makes more sense than caching the page itself. Caching database records can significantly improve the performance of the Web site. Instead of retrieving the employee information from a database table every time you need to display the information on a page, you can retrieve the information directly from memory.
Cache object is used to cache the items in memory.
Eg.
Cache.Item(“cacheItem”) = “Cache this value”
Cache(“cacheItem”) = “Cache this value”
Dim colState as Hashtable
colState.Add (“CA”,” california ”)
colState.Add (“WA”,”washington”)
Cache (“States”) =colState
Remove item from the cache:
Cache. Remove (“cacheItem”)
Note: Cache object can add any type of object i.e simple string object, Collection Object or DataSet object. DataSet object can improve the performance of the Web Site to a large extent but care should be taken while using this object. |