Application, Cookie, session, cache, and viewstate usage Summary

Source: Internet
Author: User
Tags website performance

When we develop websites that involve data sharing between pages, we often need to use a certain method to persist our data. There are many ways to persist data, this requires us to make a choice. During development, we need to precisely select the data persistence method we use. The following descriptions of application, Cookie, session, and cache may help you make a decision:

Application: provides access to methods and events for the application scope of all sessions. It also provides access to the cache of the application scope that can be used to store information. The application status is a data repository that can be used by all classes in ASP. NET applications. It is stored in the memory of the server, so it is faster than storing and retrieving information in the database. Different from the session Status specific to a single user session, the application status is applied to all users and sessions. Therefore, the application status is suitable for storing frequently used data that is small and does not change with users. Application is stored in the server memory and is user-independent, that is, multi-user sharing. If an application exists throughout its life cycle, it will not be actively discarded or serialized, no server-client data transmission occurs.

COOKIE: cookie provides a method for storing user-specific information in Web applications. For example, when a user accesses your site, you can use cookies to store user preferences or other information. When the user visits your website again, the application can retrieve the previously stored information. When developers set cookies programmatically, they need to serialize the data they want to save into strings (and note that many browsers have 4096 bytes of cookie restrictions) and then set them. Key features of cookies include: they are stored on the client's hard disk and related to users. They are stored persistently for a certain period of time. data can be shared across browsers and need to be serialized for server-client data transmission.

Session: provides information for the current user session. It also provides access to the cache of sessions that can be used to store information, as well as methods to control how sessions are managed. The application status is a data repository that can be used by all classes in ASP. NET applications. It is stored in the memory of the server, so it is faster than storing and retrieving information in the database. Unlike application states that are not specific to individual user sessions, session states are applied to individual users and sessions. Therefore, the application state is suitable for storing frequently used data with a small amount of changes as users change. In addition, because it does not involve server-client data transmission, session is also suitable for storing user security data, such as shopping cart information. Session Key features include: stored in the server memory, which is related to sessions. If a session exists throughout its life cycle, it will not be discarded or serialized, no server-client data transmission occurs.

Cache: ASP. NET provides a powerful and easy-to-use cache mechanism for storing objects created by a large number of server resources in the memory. Caching these types of resources will greatly improve the application performance. It is stored in the server's memory, allowing you to customize how to cache items and how long they will be cached. For example, in the absence of system memory, the cache automatically removes rarely used or lower-priority items to release the memory. This technology, also known as cleanup, is one of the ways in which the cache ensures that expired data does not use valuable server resources. It is not related to sessions, so it is shared by multiple sessions. Therefore, it can improve website performance, but may leak user security information, because the cache may be automatically removed when the server lacks memory, you need to check whether the cache item exists each time you obtain data. The key features of cache include: it is stored in the server memory and has nothing to do with the session. It may be discarded at any time according to the server memory resource status, and will not be serialized. Server-client data transmission will not occur. Here, I also want to provide a description of viewstate to compare it with the above four data persistence methods. Although viewstate cannot share data across pages, it can be used to reserve values between multiple requests on the same page.

Viewstate: The viewstate attribute provides a dictionary object for retaining values between multiple requests on the same page. This is the default method for retaining page and control property values between a round trip. When processing a page, the current status of the page and control is hashed into a string, the page is saved as a hidden domain or multiple hidden domains (if the data volume stored in the viewstate attribute exceeds the specified value in the maxpagestatefieldlength attribute ). When a page is sent back to the server, the page analyzes the view status string during page initialization and restores the attribute information on the page. You can also use view status to store values. By default, viewstate is not encrypted and server-client data transmission occurs. Key features of viewstate include: stored on pages, session-related and page-related, serialized. Server-client transmission occurs by default and is not encrypted by default. In the following sections, we will describe the situations in which viewstate does not occur on the server-client transmission or is encrypted. In summary, we have summarized some common and typical examples: shopping cart for e-commerce websites: Using session, because the shopping cart information is session-related and important for security. The "remember me" function of a forum or other websites: use cookies, because this is often stored as a user name, and this user name is still required when the user logs on to the forum next time. Site counters: if you do not need a database, application is used because the counters are session-independent. However, even if you are using a database, we recommend that you use the application to save the Count value and then save it to the database at intervals, this can reduce the number of accesses to the database to improve performance. Product Information: cache is the preferred choice. Because product information is usually unrelated to sessions, data with low modification frequency and high access frequency, using cache to save can effectively improve the website performance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.