Application vs Session vs Cache

asp.net

asp.net Problem Overview


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.

asp.net Solutions


Solution 1 - asp.net

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.

Solution 2 - asp.net

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.

Solution 3 - asp.net

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.

Solution 4 - asp.net

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

Solution 5 - asp.net

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:

https://stackoverflow.com/questions/3629709/i-just-discovered-why-all-asp-net-websites-are-slow-and-i-am-trying-to-work-out

Solution 6 - asp.net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchoboView Question on Stackoverflow
Solution 1 - asp.netTa01View Answer on Stackoverflow
Solution 2 - asp.netAdeelView Answer on Stackoverflow
Solution 3 - asp.netH20riderView Answer on Stackoverflow
Solution 4 - asp.netCogitatorView Answer on Stackoverflow
Solution 5 - asp.netIan IppolitoView Answer on Stackoverflow
Solution 6 - asp.netuser1540857View Answer on Stackoverflow