Advantages of Cache VS Session

Advantages of Cache vs Session

One important difference is, that items in the cache can expire (will be removed from cache) after a specified amount of time. Items put into a session will stay there, until the session ends.

ASP.NET can also remove items from cache when the amount of available memory gets small.

Another difference: the session state can be kept external (state server, SQL server) and shared between several instances of your web app (for load balancing). This is not the case with the cache.

Besides of these differences (as others have noted): session is per user/session while cache is per application.

Application vs Session vs Cache

Application and Session State have a very important difference:

Application state is a data repository available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another

Application State Overview

Session State Overview

Caching, on the other hand, allows you to store objects in memory that require extensive server resources to create - it offers powerful features that allow you to customize how items are cached and how long they are cached - you can set extensive properties like priority and expiration.

Caching Application Data Overview

Although they might appear similar, they are distinctly separate and have different roles to play in an ASP.NET application in its broadest sense.

Data cache vs session object in ASP.Net

If the objects are shareable between user sessions, then use the cache. If the objects are unique to each session -- perhaps because they are governed by permissions -- then store it in the session. The in-process session itself is stored in the cache so the deciding factor really should be the scope of the data.

Difference between cache and session in real time for Asp.Net

Session:
Session is used to store data,It is user specific. It can be accessible through out the site. Session has 4 modes:

  1. In Proc ( Data is stored in memory )
  2. State Service ( Data is stored in a service, the benefit is if your application restarts session still exits )
  3. Sql Server ( same benefit as state server has )
  4. In Proc ( without cookies ) a session_id is attached to URL. THis is used when user has disabled cookies.

Example:

Session["key"] = "value"; // You can store any object data type.

Cache:
There are two types of cache in asp.net.

  1. Page Output Caching: You can cache the whole page by specifying the PageOutput direcitve at the top of the page.It store's the rendered page in the Cache. It is used when your page is accessed by thousands of users, so to increase the response time, we cache that page.
  2. Application Caching: It allows to store any object in memory and automatically removed the object based on some limitations e.g time or some other dependencies .
    Example:

    Cache["key"] = "value"; // it also supports any object data type.

Remember Cache and Session are different things.

cache Vs Session

Use Cache:

  • To cache commonly used contents between users.
  • To cache frequently used data.
  • When you wants to speed up your site.

Use Session:

  • To store user specific data
  • Don't save high amount of data in session, it requires server resources and at end it speeds down the site.

Cache or Session?

Actually, session variable on server was designed to cache user-specific data. Storing user-specific data in cache like reinventing the wheel.



Related Topics



Leave a reply



Submit