Summary of the Differences between Session and Cache in ASP.NET

  • 2021-06-28 12:19:59
  • OfStack

Previously, there were many ways to cache data, including Cookie on the client side, Session on the server side, and Application on the server side.Cookie is a set of data stored in the client, mainly used to store personal information such as user name.Session saves the conversation information.Application is the information stored throughout the application and is equivalent to a global variable.Session is often used most frequently, so what is the difference between Session and Cache?

This section, combined with experience, details the differences between Session caches and Cache caches.

(1) The biggest difference is that Cache provides cache dependency to update data, while Session only relies on the defined cache time to determine whether the cached data is valid.

(2) Even if the application terminates, as long as the cache time defined in the Cache.Add method does not expire, the cached data will still exist the next time the application is opened.The Session cache only exists in one session, and when the session ends, the data is invalidated.

(3) Session is liable to be lost, leading to data uncertainty, which is not the case with Cache.

(4) Since Session is loaded every session, it is not appropriate to store a large amount of information, otherwise, the performance of the server will be degraded.Cache is mainly used to store large amounts of information, such as multiple tables in a database.

(5) The beta version of VS200 5 provides parameters to save the cache on the hard disk, but this feature has been removed from the official version and is expected to be reimplemented in a later version.Session can only be stored in memory at present, which has an impact on its performance.


Related articles: