Wednesday, March 13, 2013

ASP.NET 4 Caching

 

Caching Overview

A cache is made up of a pool of entries. Each entry has data along with a tag, which specifies the identity of the data in the backing store of which the entry is a copy.

In real life, Cache is very simple. If you take the life cycle of an ant, it reserves the food during the summer days for the usage of winter. In a similar line, IT system reserves the frequent usage content for the future request/kit to avoid the redundant operation.

When the cache client (a CPU, web browser, operating system) needs to access the data presumed to exist in the backing store, it first checks the cache. If an entry can be found with a tag matching that of the desired data, the data in the entry is used instead. This situation is known as a cache hit.

CPUs and hard drives frequently use a cache, as do web browsers and web servers. Let's see the caching mechanism at Core level.

Caching at Core Level

A CPU cache is a cache used by the central processing unit of a computer to reduce the average time to access memory. The cache is a smaller, faster memory which stores copies of the data from the most frequently used main memory locations. As long as most memory accesses are cached memory locations, the average latency of memory accesses will be closer to the cache latency than to the latency of main memory. Core/Primary cache is the fastest form of storage. Because it's built in to the chip with a zero wait-state (delay) interface to the processor's execution unit, it is limited in size. With this concept, 3 levels of Core Caching is represented from the OS point of view.

There are few popular Caching algorithms based on key parameters (like hit rate, recent usage, frequent usage). The most popular ones are Belady optimal algorithm, Least recent used (LRU), Random Replacement, Most recently used (MRU), Multi Queue Caching, Segmented LRU, etc.

ASP.NET Caching

With these fundamentals, ASP.NET Framework enables programmers to take advantage of caching without requiring to write a lot of code to deal with the complexities of caching, such as cache expiration, updates, and memory management. There are two different types of caching in ASP.NET:

·         Application caching

·         Page output caching

Application Caching

Application caching is nothing but a collection of in memory objects. It's automatically deallocated based on memory limitation, time limit and few other vital dependencies. It's also termed as application data caching because the scope of these in memory objects are at the application level. In ASP.NET web based context, this cache object exists for the entire web application, can be shared across the user sessions and web requests.

In simple terms, Application caching can be called as Global Data Container (GDC). As the name stands, Global Data Container is in the memory storage box to hold the shared data across the application.

A similar concept is extended to the latest computing, i.e., Cloud Computing. Microsoft Azure platform executes the application caching to improve the system performance. The concept is drafted in the below diagram:

Storing into Application Cache

ASP.NET provides built in Cache object from System.Web.Caching namespace. This object contains 'Insert' method to store any collection into the application cache.

Let's see the key parameters for Insert method of Cache object.

·         Key: This is the primary name to access the cached object in the Cache collection. The key must be unique in the cache.

·         Value: This is the data as an Object that you want to cache.

·         Dependencies: It is the associated item for Cache. Framework will trigger the signal during the changes in this dependency object.

·         AbsoluteExpiration: This is the time as a DateTime object at which the object should be removed from the cache.

·         SlidingExpiration: This is the time as a TimeSpan object after which the object should be removed from the cache if it has not been accessed by a user.

·         Priority: This is a CacheItemPriority enumeration value that you can use to determine which objects are removed first during scavenging (i.e., when memory starts to run low). Lower priority objects are removed sooner.

·         onRemoveCallback: This is an event handler that is called when the object is removed from the cache. This can be null if you don’t want to specify a callback method.

Code Snippet

 Collapse | Copy Code

using System.Web.Caching;
public partial class _Default : System.Web.UI.Page
{
    protected override void OnPreInit(EventArgs e)
    {
        Cache["CompName"] = "ABCD Corporation";
        Cache["CompSymbol"] = "ABC";
        Cache["CEO"] = "First Last";
        
        // Programmatically Insert operation
        Cache.Insert("LastTrade", ReadFeed(LastTradePrice), 
                 null, DateTime.Now.AddHours(8), Cache.NoSlidingExpiration);
        
        //Cache Dependency operation
        System.Web.Caching.CacheDependency FeedDepend = 
            new System.Web.Caching.CacheDependency(Server.MapPath("Feed.xml"));
        Cache.Insert("MarketPrice", ReadFeed(CurrentPrice), 
                 FeedDepend, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
    }
}                

Code Summary

System.Web.Caching is the namespace supplied by ASP.NET Framework to manipulate the caching operations. It has been included in the first line. Cache content is getting loaded in PreInit state of the page. Cache storage functionality starts from the fourth line of the source code.

Based on the working knowledge with Session or similar objects, Cache object would be similar. Programmers can assign items directly to the cache by giving them a name (key) and assigning them an object (value).

In the fourth line of the above code, Company Name is stored in the cache with the key 'CompName'. Cache object is the global container object suppliant by Framework at application level. In the similar way, 'CompSymbol' and 'CEO' info are stored in global Cache object.

By using the Insert method, add an item to the cache and control how that item gets removed from the cache. This functionality includes automatic removal based on a specific period of time. Insert has a number of overloads based on the many parameters you can set when adding an item to the cache. LastTrade key insert method reserves the content from LastTradePrice, for the next 8 hours. Later, LastTrade cache automatically expires.

In ASP.NET Framework, a cache dependency links a cached item to something else such as a file or another item in the cache. ASP.NET monitors the dependency and invalidates the cache if the dependent item changes. In the above code sample, cache dependency is based on Feed.xml file. When this dependent feed file of the cache get updated, the object is removed from the cache. Apparently, we can use 'OnRemoveCallback' delegate to execute the operation on removal event.

Reading from Application Cache

It's a very easy job to retrieve the item from application cache. It's very similar to other collection objects. Programmer needs to pass the key to retrieve the value.

 Collapse | Copy Code

Cache[key]

Code Snippet

 Collapse | Copy Code

lblName.Text = Cache["CompName"];
lblSymbol.Text = Cache["CompSymbol"];
lblCeo.Text = Cache["CEO"];                 

Code Summary

Objects can be retrieved from the cache by checking for the key. In the above example, Company Name is retrieved from Cache object CompName key and lblName is refreshed with the loaded data. In the similar way, Company Symbol and CEO info are read from System.Web.Caching.Cache global application object and the retrieved info are assigned in the appropriate label controls.

It is good practice to always verify that an item is not null. If a value is null, that value either hasn’t been cached or it has expired from the cache. If an item is null, you should retrieve it from the original source and, in most cases, place it into the cache.

Page Output Caching

In general, web browser often keeps a copy of the page on the local computer. On next time user requests, the browser simply verifies that the cached version is still valid, and then displays the cached page to the user. This methodology improves the responsiveness using the client side efficiency.

With page output caching concept, ASP.NET can keep a copy of a rendered ASP.NET webpage in memory on the server. If web requests come from the same user or different user, ASP.NET can return the page almost instantly. Let's assume the same page takes a long time to render due to the process intensive like heavy business logic, multiple database queries, etc. ASP.NET page output caching improves significant performance and reduces rendering time. If you have a lot of activity on your server, it can also increase your scalability, because resources used to retrieve data can be freed.

Execution of page output caching is drawn as:

Declarative Page Output Caching

To enable Page Output Caching, ASP .NET provides declarative approach at each page in your site. Using this approach, the programmer will get the granular control over which pages get cached and how they get cached. It's possible by adding the @ OutputCache directive to the top of a page’s markup. Programmers can configure this directive by using the below attributes.

·         Duration: The number of seconds to cache the page. It's the only mandatory parameter.

·         Location: One of the OutputCacheLocation enumeration values, such as Any (Default), Client,Downstream, Server, None, or ServerAndClient.

·         CacheProfile: The name of the cache settings to associate with the page.

·         NoStore: Flag to determine whether to prevent secondary storage of sensitive information.

·         Shared: Flag to determine whether user control output can be shared with multiple pages. The default isfalse.

·         VaryByParam: A semicolon-separated list of strings used to vary the output cache.

·         VaryByControl: A semicolon-separated list of strings used to vary a user control’s output cache.

·         SqlDependency: It identifies a set of database and table name pairs on which a page or control’s output cache depends.

Code Snippet

 Collapse | Copy Code

< %@ OutputCache Duration="20" VaryByParam="search;none" %>           

Code Summary

The above code example demonstrates how to cache a page for 20 minutes, regardless of the parameters passed to the page. At the end of this time span, Cache get expired automatically.

 Collapse | Copy Code

< %@ OutputCache Duration="15" VaryByParam="marketprice" %>           

Code Summary

If the page might display differently based on parameters, provide the names of those query string parameters in the VaryByParam attribute. The above code example caches a different copy of the page for different values provided in the marketprice query string parameters.

In a similar line, other attributes of OutputCache directive performs the declarative page output caching process in ASP.NET Framework.

Advantages of Caching

·         In terms of advantage, Cache concept can make or break the performance of your computer system. Smart buffer algorithm will make the site loading faster than traditional approach.

·         Cache takes the heavy load / execution from the server for the repeated operations. By doing so, Servers can be efficiently used.

Disadvantages of Caching

·         In terms of Caching approach, it will not be coherent with the actual data content in the delayed writes.

·         Apparently, Cache isn't always the freshest information.

Points of Interest

Hope it will be interesting to also know the core cache concepts and ASP.NET caching features, instead of just the regular abstracted code level implementation.

 

No comments:

Post a Comment