.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Application and Caching objects in Asp.net - at a spark

Posted By:Gaurav Arora       Posted Date: April 27, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

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.       

2.      Partial Page Caching

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

3.      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.

4.      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.



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 



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)






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 :



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["SomeGlobalCounter"] =(int)Application["SomeGlobalCounter"] + 1;



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.




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.


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend