|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Improving Web application performance using caching techniques
By Sivakumar Kannaiyan Performance is one of the prime requirements in a web application. Due to their inherent nature web applications are comparatively slower than Windows applications. So when a web application is designed due care should be taken to improve the performance of the application. By performance I mean, the response time taken by the system to serve a request. When we talk about quick response from a web application the term caching comes as the first choice. Microsoft’s Definition for Caching “Caching is a technique widely used in computing to increase performance by keeping frequently accessed or expensive data in memory. In the context of a Web application, caching is used to retain pages or data across HTTP requests and reuse them without the expense of recreating them. “ This definition gives a crisp and clear idea as to what caching is and why it is used. There are different types of caching is used in a web application, ASP.Net supports the following,
Sometimes it is not practical to cache an entire page as portions of the page must be created or customized for each request. In this case, we can identify objects or data that are expensive to recreate and which can be cached. These items can be created once and cached for some duration for subsequent usage. This is achieved through Fragment Caching. While the above two caching techniques are done through configuration settings and setting attribute values of an ASP.Net directive, Data Caching is done programmatically. ASP.Net runs a separate Cache Engine for this purpose and provides an implicit object “Cache” to access the data in cache. The duration for which an item should be cached should be chosen carefully. There should be a balance between the performance and providing the user with relevant, non outdated data. For some items, the data might be refreshed at regular intervals or the data is valid for a certain amount of time. In that case, the cache items can be given an expiration policy that causes them to be removed from the cache when they have expired. Code that accesses the cache item simply checks for the absence of the item and recreates it, if necessary. The ASP.NET cache supports file and cache key dependencies, allowing developers to make a cache item dependent on an external file or another cache item. This technique can be used to invalidate cached items when their source is altered. Output Caching For the pages to be eligible for output caching they should have a valid expiration/validation policy and public cache visibility. This can be done through the thorough the OutputCache directive. Syntax: <%@ OutputCache Duration="#ofseconds" Location="Any | Client | Downstream | Server | None" Shared="True | False" VaryByControl="controlname" VaryByCustom="browser | customstring" VaryByHeader="headers" VaryByParam="parametername" %> Fragment Caching When only a part of the page is needed to be cached we can go for Fragment Caching. This is done by identifying the part of a page to be cached and creating it as a User Control and using the @ OutputCache directive to set the cache properties for that user control. In the above attribute list, except those which are specific to the page, all are applicable to the user control. Data Caching Data caching is done in a web application when recreation of an object is costly. The objects which do not change their state frequently are the suitable candidates for data caching. ASP.Net runs a separate cache engine for this purpose. ASP.Net provides many implicit objects for the developers to access directly without having to declare it first. Cache is one of those objects. Cache is nothing but a collection which holds objects which can be accessed in application level much similar to Application object. Cache vs. Application The difference between Cache and Application objects is, for the items added in the Cache we can set expiration and dependency policies. And we can map call back functions to the objects, which will be invoked when the item gets invalidated from the cache. Cached items are invalidated when its expiration time arrives or the items on which they depend get changed or it is removed explicitly by Cache.Remove method or if by the system itself when it goes out of memory. The reason why the cache item was removed will be sent to the call back method as an argument along with the key and value. Adding an item to Cache object To add an item to Cache, it provides two methods. a) Cache.Add(string key, stringvalue, System.Web.Caching.CacheDependency, absoluteExpirationTime, slidingExpirationTime, System.Web.Caching. CacheItemPriority, System.Web.Caching.CacheItemRemovedCallback ) Eg., Cache.Add(“UserID”,username, new System.Web.Caching. CacheDependency (filenames, cachekeynames,AnotherCacheDependency), DateTime.Now. AddSeconds(15), TimeSpan.Zero, new System.Web.Caching. CacheItemRemovedCallback(CallBackMethodName)); b) Cache.Insert( ). This method is same as Add except that it overrides the value if the key already exists in the Cache and also it has four overloads and convenient to use. (Add method will ignore the call if the key already exists in the Cache. You will have to remove the item explicitly using Cache.Remove method if you want to change the value associated with a key in the Cache. Insert method does this implicitly). Since Cache is just a collection, there is one more straight forward way to add an object into Cache as given below, Cache[“Key”]=Value; Retrieving an item from Cache Items can be retrieved using, a) Cache.Get(string key) It retrieves the value associated with the key. b) Cache.GetEnumerator() returns a Dictionary Enumerator which is used to iterate through the key-value pairs in the Cache. Like adding an item to the cache you can retrieve using collection style as follows, Value=(DataType)Cache[“Key”]; Type conversion is needed as the items are stored as object in the Cache. Caching is a technique which should be planned at the design stage of the application itself in order to utilize its power fully. Note: Examples in this article are given in C# syntax. |
![]() |
| Viewing: ASP Free Forums > Other > Development Articles > Improving Web application performance using caching techniques |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|