Thursday, 12 September 2019

difference between caching and session

  1. Session data is stored at the user level but caching data is stored at the application level and shared by all the users.
  2. Sessions may not improve performance whereas Cache will improve site performance.
  3. Items in cache can expire after given time to cache while items in session will stay till session expires.
  4. Sessions may change from user to user whereas a single Cache will be maintained for the entire application.
  5. Cache wont maintain any state, whereas Sessions will maintain separate state for every user.
OR,

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.
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.
Or,

1. In Session after storing the session, when you copy the URL and paste it into another browser then you can't access the URL but when you do that for caching, in other words after storing into the cache variable, it can be accessed by a different user also.
So now we can say that a session has user-level storage on the server but caching has application-level storage on the server.

2. A session has a relative or sliding expiration, in other words by default a session has an expiration time of 20 minutes, but an application that is not being used will be relatively increased, in other words if the user is not using that application for more than 20 minutes then the session will be automatically expired and if the user is not using it then the time will be relatively increased, well we can also provide a custom time of session expiration. But caching has 2 types of expiration as in the following:
  1. Absolute Expiration
  2. Relative Expiration or Sliding Expiration 
Absolute expiration means in caching the cache memory will be deleted after a fixed amount of time, the same as for cookies
Sliding expiration means in caching the cache memory will be deleted after a fixed amount of time if that memory is not in use, if it is used in that specific time then it can be relatively increased. the same as for a session.

1. How to provide Absolute Expiration in caching
  1. Cache.Insert("Uname", txtUser.Text, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero); 

2. How to provide Sliding Expiration in caching
  1. Cache.Insert("Uname", txtUser.Text, null, DateTime.MaxValue, TimeSpan.FromSeconds(20));

No comments:

Post a Comment