Difference Between Session Application and Cache

Hi, today's article is related to the Session, Application and Cache. So without wasting much time let's start our topic.

As we all know Session, Application and Cache are State Management Techniques. We have two types of State Management Techniques:

  1. Client Side
  2. Server Side

So Session and Application are server-side techniques and Cache works on the client side as well as the server side, in other words thereby all these three stores the information on the server (since browsers are stateless) and these are widely used in ASP.Net applications. The question now arises of when and where do we need to use all these. So before using them we should know what the uses of these techniques are. Let's start with sessions.

1. Session: A Session is basically used to store user-specific information. Typically you save username, email ID, Department and so on and this information cannot be shared by another user. Like in this form.

Snapshot: 1

Snapshot: 2

I have created a web form with a text box and a button. Now I will enter a name and will click on the “Click Me” button. Then we will redirect to our next web form (Welcome.aspx). Then we will see what happens. The destination form looks like:

Snapshot- 3

Now we will click on the button of our first web form.

Snapshot- 4

Snapshot- 5

I used here my name and as I click on the “Click Me” button then:

Snapshot- 6

Snapshot- 7

See, now the Session, Application and cache contain the same value, “Yogendra”. Why? Because the session contains user information and the Application and Cache contain the value for the complete web application.

Now copy the URL and paste it into another browser as in the following:

Snapshot- 8

Snapshot- 9

Here we can see the Session value become null here, so we didn't find a Session Key, whereas the Application and Cache hold the value for the entire web application. That's why it's state is still maintained. Now if both the Application and Cache are doing the same job then what is the difference between them. So the following describes the differences among them.

2. Application: As in the preceding snapshots you see that the Application and Cache values are the same all over the application. Basically we use a static value in the Application Object that we want to show somewhere in the application that will be visible for every form in the Application.

We can use the Application Object in the global.asax file.

The Application Object maintains its data until the web application is shut down or we release the data manually by assigning null or call the Clear() method. The Application Object does not have any timeout.

Basically we use The Application Object to hit a counter on the Application, or read a readonly file or table to display in various web pages.

3. Cache: A Cache is very useful in web applications for performance optimization. Since application data can only be stored on the server, Cache data is stored on both the client side as well as the server side. We can define the time period for the cache using the Cache.Insert() or Cache.Add() method and the time period can be assigned from seconds to years. We even can maintain an absolute time expiration for the Cache.

We can assign value in the Cache only from web forms, not from the global.asax (we only can pass in the case of the Application and Session) file.

We retrieve the Cache data from the Cache without repeating the full cycle, which doesn't happen in the case of the Application.

And we can use the Cache if we want to calculate the time the user was logged in. Or if we are giving an online examination, then we can maintain the questions in the cache and retrieve them from there for better performance.

Since there are three types of caching, here we did not explain all the types.

Thanks for reading the article, suggestions are heartily welcome.