CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » Article » ASP.Net Ajax Article » Difference between caching and application – A Step Ahead Series   You are not logged in.
Search
 

Technologies
 

Sponsors
 

CodeDigest Navigation
 

Technology News
 

Community News
No News Feeds available at this time.
 
A Detailed look at Application and Caching Object in ASP.Net

By Gaurav Arora
Posted On Mar 01,2009
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 9
Print this article.
Category: ASP.Net

Subscribe to our feed!

A Detailed look at Application and Caching Object in ASP.Net

Cache  

It’s nothing but a thought kind of memory. In respect to asp.net it’s the memory of the machine/server from where the source-code is running. It is the one way which allows storing complex data for reusability.

Now think a criteria where clients access an ASP.NET page, there are basically two ways to provide them with the information they need:

Ø       the ASP.NET page can either obtain information from server resources, such as from data that has been persisted to a database, or

Ø       the ASP.NET page can obtain information from within the application.

Retrieving information from a resource outside the application will require more processing steps, and will therefore require more time and resources on the server than if the information can be obtained from within the application space.

Now, suppose the information’s which sent to browser’s have already been prepared then how faster the process of web-page.

The ASP.NET 3.5 Framework supports the following types of caching:

  1. Page Output Caching

Page Output Caching caches an entire page.       

  1. Partial Page Caching

Partial Page Caching enables you to get around this problem by enabling you to cache only particular regions of a page.

  1. DataSource Caching

You use DataSource Caching with the different ASP.NET DataSource controls such as the SqlDataSource and ObjectDataSource controls. When you enable caching with a DataSource control, the DataSource control caches the data that it represents.

  1. Data Caching

Finally, Data Caching is the fundamental caching mechanism. Behind the scenes, all the other types of caching use Data Caching. You can use Data Caching to cache arbitrary objects in memory. For example, you can use Data Caching to cache a DataSet across multiple pages in a web application.

 

Note:

The Cache object can also have an expiration which would allow us to reinstitute data into the memory in intervals. Using the same example as above, we can make the cache expire every two hours, and repopulate the data. It would do this every 2 hours throughout the day, allowing the most up to date data to be fetched. Below is an example of how something can be put into the cache: 

Referenced from msdn 

http://msdn.microsoft.com/en-us/library/system.web.caching.cache(VS.80).aspx

 

public void AddItemToCache(Object sender, EventArgs e)

{

    itemRemoved = false;

    onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

    if (Cache["Key1"] == null)

        Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60),TimeSpan.Zero, CacheItemPriority.High, onRemove);

}

 

public void RemoveItemFromCache(Object sender, EventArgs e)

{

    if(Cache["Key1"] != null)

        Cache.Remove("Key1");

}

 

 

Application

Its nothing but similar to Session with a bit difference that is Session objects have scope within a particular session while application objects having scope within entire application.

Creating an application variable is similar to session variables.

Application(“applictionObj”) = “This is an application object”

Referenced from msdn :

 http://msdn.microsoft.com/en-us/library/bf9xhdz4(VS.71).aspx

 



ASP.Net Hosting

Recent Articles

ASP.NET provides the following application-state support:

Ø       An easy-to-use state facility that is compatible with earlier versions of ASP, works with all .NET-supported languages, and is consistent with other .NET Framework APIs.

Ø       An application-state dictionary that is available to all request handlers invoked within an application. Unlike Internet Information Services (IIS) and earlier versions of ASP, where only pages can access application state, all IHttpHandler and IHttpModule instances can store and retrieve global variables within the dictionary.

Ø       A simple and intuitive synchronization mechanism that enables developers to easily coordinate concurrent access to variables stored in the application state.

Ø       Application-state values that are accessible only from code running within the context of the originating application. Other applications running on the system cannot access or modify the values.

Application-state variables are, in effect, global variables for a given ASP.NET application. Like client-side application developers, ASP.NET programmers should always consider the impact of storing anything as a global variable.


Application State Synchronization

Multiple threads within an application can simultaneously access values stored in application state. Consequently, when you create something that needs to access application-state values, you must always ensure that the application-state object is free-threaded and performs its own internal synchronization or else performs manual synchronization steps to protect against race conditions, deadlocks, and access violations.

The HttpApplicationState class provides two methods, Lock and Unlock, that allow only one thread at a time to access application-state variables.

Application.Lock();

Application["SomeGlobalCounter"] =(int)Application["SomeGlobalCounter"] + 1;

Application.UnLock();

 

Calling Lock on the Application object causes ASP.NET to block attempts by code running on other worker threads to access anything in application state. These threads are unblocked only when the thread that called Lock calls the corresponding Unlock method on the Application object.

Conclusion

 

Caching is one of the vital things in a web application in terms improving performance and in same way; application object will help us set some application wide settings that can reflect in every user’s sessions. In this article, we have learnt the ASP.Net Application and Caching object in a detailed way.  After reading this article one can understand the difference between both this object.

 

Similar Articles
  • You can contribute to CodeDigest.Com:
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments
    l8jsbq <a href="http://hejmqaeahwip.com/">hejmqaeahwip</a>
    l8jsbq <a href="http://hejmqaeahwip.com/">hejmqaeahwip</a>
    GOYbpf <a href="http://cyydtdzgjftd.com/">cyydtdzgjftd</a>
    GOYbpf <a href="http://cyydtdzgjftd.com/">cyydtdzgjftd</a>
    Im impressed, I ought to say. Very raerly do I come across a weblog thats both informative and entertaining, and let me let you know, youve hit the nail on the head. Your weblog is critical; the issue
    Im impressed, I ought to say. Very raerly do I come across a weblog thats both informative and entertaining, and let me let you know, youve hit the nail on the head. Your weblog is critical; the issue is some thing that not sufficient individuals are talking intelligently about. Im seriously happy that I stumbled across this in my search for something relating to this concern.
    stdfcujyfh
    ghgd dchmgc vngchg hgcfghbn c cghcdhfdxgrfsdxytdgh vhtgdctg mhytdc mvhmfh yfhgvhhbv jgdfhmgccn vhgfhmgcdgf jhvjhfgcgvf vghfchgvn bvhgcgcgh gmjhgvjhvbjhv, hjvbhjbv mmnmnm,nvn ,nbvhjfvghfgc fchgcfghcjhbvjhvhnv,.jhb.j/kbhjvhgvb,j..j.....j/j///j.hvgjhv jhghjvbchv
    sxCbUy , [url=http://skfvzsflcede.com/]skfvzsflcede[/url], [link=http://kjwsptgqitsl.com/]kjwsptgqitsl[/link], http://ajuwnenbigoc.com/
    sxCbUy , [url=http://skfvzsflcede.com/]skfvzsflcede[/url], [link=http://kjwsptgqitsl.com/]kjwsptgqitsl[/link], http://ajuwnenbigoc.com/
    cfGYu4 <a href="http://llxnofvblcrz.com/">llxnofvblcrz</a>
    cfGYu4 <a href="http://llxnofvblcrz.com/">llxnofvblcrz</a>
    xX2UBN , [url=http://ocbjtrbbmyqi.com/]ocbjtrbbmyqi[/url], [link=http://nepaytljrsci.com/]nepaytljrsci[/link], http://pxyfcaginobi.com/
    xX2UBN , [url=http://ocbjtrbbmyqi.com/]ocbjtrbbmyqi[/url], [link=http://nepaytljrsci.com/]nepaytljrsci[/link], http://pxyfcaginobi.com/
    XPUZx8 <a href="http://qhmunhusbsal.com/">qhmunhusbsal</a>
    XPUZx8 <a href="http://qhmunhusbsal.com/">qhmunhusbsal</a>
    Kudos! What a neat way of thinnkig about it.
    Kudos! What a neat way of thinnkig about it.