Application Cookie Session Cache and ViewState in ASP. NET

  • 2021-07-18 07:51:13
  • OfStack

We often use replies when we are doing ASP. NET development, and we store some data in the replies. Let's introduce Application, Cookie, Session, Cache and ViewState in 11, so that we can choose when to use them.

1. Application

Application provides access to application-wide methods and events for all sessions. It also provides access to an application-wide cache that can be used to store information. Application state is a data repository available to all classes in an ASP. NET application. It is stored in the memory of the server, so it executes 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. As a result, application state is ideal for storing small amounts of commonly used data that do not vary from user to user.

The key features of Application are: stored in the server memory, user-independent, that is, shared by multiple users, existing in the whole lifetime of the application, that is, it will not be actively discarded, serialized, and server-client data transmission will not occur.

2. Cookie

Cookie provides a way to store user-specific information in an Web application. For example, you can use Cookie to store user preferences or other information when users visit your site. When the user visits your Web site again, the application can retrieve the previously stored information. When developers programmatically set Cookie, they need to serialize the data they want to save into strings (and note that many browsers have a 4096-byte limit on Cookie) and then set it.

The key features of Cookie are: stored on the client hard disk, user-related, persistent storage within a certain time, sharing data across browsers, serialization and server-client data transmission.

3. Session

Session provides information for the current user session. It also provides access to a session-wide cache that can be used to store information, as well as methods to control how sessions are managed. Application state is a data repository available to all classes in an ASP. NET application. It is stored in the memory of the server, so it executes faster than storing and retrieving information in a database. Unlike application state that is not specific to a single user session, session state applies to a single user and session. As a result, application state is ideal for storing small amounts of commonly used data that vary from user to user. And because it does not generate server-client data transfers, the Session is also suitable for storing security data about the user, such as shopping cart information.

The key features of Session are: stored in server memory, session-dependent, existing throughout the lifetime of the session, i.e. not actively discarded, not serialized, and no server-client data transfer.

4. Cache

ASP. NET gives you a powerful, easy-to-use caching mechanism for storing objects in memory that require a lot of server resources to create. Caching these types of resources can greatly improve the performance of your application. It is stored in the memory of the server and allows you to customize how items are cached and how long they are cached. For example, when system memory is scarce, the cache automatically removes rarely used or low-priority items to free up memory. This technique, also known as cleanup, is one of the ways caching ensures that out-of-date data does not use valuable server resources. It is not session-related, so it is shared by multiple sessions, so using it can improve Web site performance, but it may reveal user security information, and because Cache may be automatically removed when the server lacks memory, it is necessary to check whether the Cache entry still exists every time the data is fetched.

The key features of Cache are: stored in server memory, session-independent, can be discarded at any time according to the status of server memory resources, not serialized, no server-client data transmission.
Here, I also want to make a description of ViewState by the way, so as to make a comparison with the above four ways of data persistence. Because ViewState can not share data across pages, but within the same page, it can be used to retain values between multiple requests for the same page.

5. ViewState

The ViewState property provides a dictionary object for preserving values between multiple requests for the same page. This is the default method used by pages to preserve the values of page and control properties between round trips. When the page is processed, the current state of the page and control is hashed to a string and saved in the page as one or more hidden fields (if the amount of data stored in the ViewState property exceeds the value specified in the MaxPageStateFieldLength property). When a page is posted back to the server, the page parses the view state string during page initialization and restores the property information in the page. You can also use view state to store values. By default, ViewState is not encrypted and server-client data transfers occur.

The key features of ViewState are: stored on a page, in session and related to the page, serialized, server-client transmission occurs by default, and not encrypted by default.
As for the circumstances under which ViewState does not have server-client transmission or is encrypted, we will explain it in a later chapter.

To sum up, we sum up one common and typical example:

Shopping carts for e-commerce sites: Use Session because shopping cart information is session relevant and security is important.
The "Remember Me" feature of a forum or other website: Use Cookie, because it is often saved with only one username, and the username needs to still exist when the user logs in next time.
Site counters: If you don't use a database, you are using Application, because counters are session independent. But even in the case of using a database, I recommend that you use Application to save the count values at the same time, and then save them to the database every once in a while, because this can reduce the number of accesses to the database and improve performance.
Product information: Cache is the preferred choice, because product information is usually session-independent, with low modification frequency and high access frequency. Using Cache to save can effectively improve the performance of the website.

Finally, we give a table that lists the characteristics comparison of the above data persistence methods, so that you can make a decision:

Application Cache Session Cookie ViewState
存储位置 服务器 服务器 服务器 客户端 客户端
是否会被主动丢弃 不会 不会 不会 不会
与会话相关
是否被序列化
是否发生服务器-客户端传输 是(默认情况)
是否被加密 否(默认情况)


Related articles: