Asp. NET application, Cookie, session, cache and Viewstate_ base application

Source: Internet
Author: User
Tags sessions server memory

We often use the answer when we do asp.net development, we store some data in the reply, let's introduce the application, cookies, session, cache and viewstate, so we can choose when to use them.

First, application

Application provides access to application-scoped methods and events for all sessions. Also provides access to application-scoped caching that can be used to store information. Application state is a repository of data that can be used by all classes in the ASP.net application. It is stored in the server's memory, so it performs faster than storing and retrieving information in the database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is ideal for storing commonly used data that is small in number and does not change with the user.

The key features of application are: stored in server memory, user-independent, multi-user sharing, in the entire lifetime of the application will not be actively discarded, not serialized, no server-client data transfer.

Second, cookies

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

The key features of cookies are: stored on the client's hard disk, related to users, persistent storage in a certain period of time, can share data across browsers, need to be serialized, server-client data transfer occurs.

Third, session

Sessions provide information for the current user session. It also provides access to the session-scoped cache that can be used to store information, as well as ways to control how the session is managed. Application state is a repository of data that can be used by all classes in the ASP.net application. It is stored in the server's memory, so it performs faster than storing and retrieving information in the database. Unlike application states that are not specific to individual user sessions, session state applies to individual users and sessions. As a result, application state is ideal for storing frequently used data that is small in number and varies with user changes. And because it does not occur server-client data transfer, the session is also suitable for storing security data about the user, such as shopping cart information.

The key features of the session are: stored in the server memory, the sessions are related, in the entire lifetime of the conversation will not be actively discarded, not serialized, no server-client data transfer.

Four, Cache

ASP.net provides you with a powerful, easy-to-use caching mechanism for storing objects that require a large amount of server resources to be created in memory. Caching these types of resources can greatly improve the performance of your application. It is stored in the server's memory, allowing you to customize how items are cached and how long they are cached. For example, when system memory is scarce, caching automatically removes less-used or lower-priority items to free memory. This technology is also known as cleanup, which is one of the ways caching ensures that outdated data does not use valuable server resources. It is not session-related, so it is shared by multiple sessions, so using it can improve the performance of the Web site, but it may reveal the user's security information and may automatically remove the cache when the server lacks memory, so it is necessary to detect whether the cache entry still exists each time the data is fetched.

Cache key features are: stored in the server memory, the session is irrelevant, depending on the state of the server memory resources can be discarded at any time, not serialized, no server-client data transfer.
Here, I would also like to make a description of ViewState, in order to make a comparison with the four data persistence methods above. Because, although viewstate cannot share data across pages, it can be used on the same page to preserve values between multiple requests on the same page.

Five, ViewState

The ViewState property provides a Dictionary object that preserves values between multiple requests on the same page. This is the default method that pages use to preserve page and control property values between round trips. When the page is processed, the current state of the page and control is hashed into a string and saved as a hidden field or multiple hidden fields in the page (if the amount of data stored in the ViewState property exceeds the specified value in the Maxpagestatefieldlength property). When the page is sent back to the server, the page analyzes the view-state string at the page initialization stage 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 transfer occurs.

The key features of viewstate are: stored on the page, session and page-related, serialized, server-client transfer by default, and not encrypted by default.
As to what the case ViewState does not occur server-client transmission, or is encrypted, we will be described in later chapters.

In summary, we have summed up some common and typical examples:

E-commerce Web site Shopping Cart: Use session, because shopping cart information is conversation-related and security is important.
The "Remember Me" feature of a forum or other Web site: Use cookies because this is often saved only by a username, and the user name still exists when the user logs in next time.
Site counter: If you do not use the database, it is using application, because the counter is session-independent. But even when using a database I recommend that you use application to save the count, and then save it to the database every other time, because it reduces the number of times you access the database to improve performance.
Product Information: Cache is a priority choice, because the product information is usually not a session-independent, modified frequency and access to high data, the use of cache to save can effectively improve the performance of the site.

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

Application Cache Session Cookies ViewState
Storage location Server Server Server Client Client
Whether it will be actively discarded No Yes No No No
Session related Whether Whether Is Is Is
is serialized Whether Whether Whether Is Is
Whether server-client transfer occurs Whether Whether Whether Is Yes (default)
Whether or not to be encrypted Whether Whether Whether Is No (default)

Related Article

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.