CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

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:
Be first to rate
this article.
No of Comments: 5
Category: ASP.Net
Print this article.

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 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 CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
I read your post and
I read your post and wihsed I was good enough to write it http://pzftzhltf.com [url=http://yibofyeagqj.com]yibofyeagqj[/url] [link=http://ywdpxm.com]ywdpxm[/link]
You get a lot of res
You get a lot of respect from me for writing these helpful <a href="http://lewbwzqrlq.com">arlcites.</a>
The voice of rayaoni
The voice of rayaonilitt! Good to hear from you. http://dcbvxdwief.com [url=http://gibuveoigmg.com]gibuveoigmg[/url] [link=http://nlqtnda.com]nlqtnda[/link]
Check that off the l
Check that off the list of things I was <a href="http://hnjzbs.com">coefsund</a> about.
You make thgnis so c
You make thgnis so clear. Thanks for taking the time!