开发者

Application vs Session vs Cache

开发者 https://www.devze.com 2023-02-13 05:00 出处:网络
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. Application and Session State have

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.


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.


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.


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消