70

What is an appropriate use case for all of the above? It seems session and cache are quite similar, and I can't think of much use for application.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
chobo
  • 31,561
  • 38
  • 123
  • 191

6 Answers6

110

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.

Ta01
  • 31,040
  • 13
  • 70
  • 99
  • What do we mean by `objects in memory that require extensive server resources to create`? If I've to store a string key-value pair (to be accessed by all users) then I will store it in Application state and if I've to store the contents of a frequently used file then I'll store it in Application cache. Is that a fair example? If everything is getting stored in main memory of the application only then why saving it in cache will do any good apart from having features like expiration. At the end of the day both Application state and Cache are hogging the main memory of the application. Isn't it? – RBT Jan 19 '18 at 00:42
  • 1
    @RBT though they both use server-side memory, the cache can be invalidated by multiple factors, such as priority, time or a dependency (such as another item in the cache or file or directory). I would read up the link provided in the answer for Caching Application Data Overview to get a better idea of the capabilities. – Filipe De Sousa Apr 19 '18 at 11:31
  • https://stackoverflow.com/questions/10960695/using-static-variables-instead-of-application-state-in-asp-net#answer-10964038 – bresleveloper Jun 26 '19 at 22:31
  • Is there any store difference of Cache and Session. Or they are kept inside the worker process memory, unless not implemented explicitly. Because there is a limit of cache in MB. – शेखर Sep 24 '19 at 02:07
  • System.Web.Caching.Cache also has extensive automatic expiration and dependency features. HttpApplicationState is for simple name value look up. – cskwg Dec 07 '21 at 05:48
22

Session is per user. It is not shared among users.

Application and Cache scope are application wide. Cache can be expired. If you have data that can be changed let's say 5 minutes, you can place that in cache, while if you have data that is not updated regularly, than it is the candidate of placing in application variable.

Adeel
  • 19,075
  • 4
  • 46
  • 60
  • It seems that session is something you would be using 95% of the time. I guess cache could be used for components on a page that use queries. I see little use for application state – chobo Feb 23 '11 at 20:27
  • 1
    It depends on the application. If your content is very time consuming to gather and aggregate for a page veiw, it makes sense to cache it on your own server, so every user gets the same cached content. However, things specific to a process a user goes through, is typically put in the Session. ApplicationState could be used for things like configuration or access tokens to external services. – Arve Systad Jan 09 '15 at 15:43
  • Great use of application object is to restrict multisession for same user. Whenever user logs in, make his entry in application and remove it when Session_ends. Now when application already holds entry to userid which means one active session is already in progress – Sangram Nandkhile Jun 24 '15 at 09:46
7

Session is used for user specific information. Typically you would save username, user preferences like screen name, cart id ( if you are selling anything), email etc

Cache is generally used to when you have information that is shared among all people. It is usually to reduce long processes or hits to the DB. IE you want to display the top n articles. You can set a time limit on this, so it will refresh the date after a time peroid

Application variable is good for static information you want to save on the server. This could be a location of where media files are located.

H20rider
  • 2,162
  • 5
  • 29
  • 47
4

None of these answers makes clear enough a very important property of the Cache - it has application scope and is shared by all users! Any data you store in cache is available to all users. You can still store data in the cache that you want to be available only to a specific user, but you must use a cache key value that is unique to that user e.g. Cache.Add("UserData" + userID, data...

Cogitator
  • 154
  • 1
  • 1
2

There is a very important limitation of the built-in inproc session object that none of the other answers have pointed out, that limits its use in high concurrency websites. Specifically, if you change any session item in your code, the request will stall and wait until all read requests to the session object are completed. In this case, the cache is a much better choice:

I just discovered why all ASP.Net websites are slow, and I am trying to work out what to do about it

Community
  • 1
  • 1
Ian Ippolito
  • 486
  • 4
  • 7
0

Another thing about the session vs cache. If data stored in a session needs to be invalidated or reloaded when a depending object is changed then it might be difficult to do this. Them cache might be a better option.

user1540857
  • 194
  • 1
  • 10