Differences between application, session, Cookie, viewstate, and Cache

Source: Internet
Author: User
1. Application: used to save data shared by all users. Similar configuration data in ASP. NET is best stored in the web. config file. If the application object is used, a problem to be considered is that any write operation must be completed in application_onstart event (Global. asax. Although the application. Lock and application. Unlock methods are used to avoid synchronization of write operations, the method serializes Application Object Requests. When a website has a large traffic volume, a severe performance bottleneck occurs. Therefore, it is best not to use this object to save large datasets.
Use:
// Store information
Application ["nameid"] = "0001 ";
// Read information
String nameid = application ["nameid"]. tostring ();
2. Session: used to save the private information of each user. The session information is stored in the memory of the Web server, and the stored data volume can be large or small. The stored data is automatically released when the session times out or is disabled. It is a good choice to save a small amount of data session objects.
Use:
// Store information
Session ["nameid"] = "0001 ";
// Read information
String nameid = session ["nameid"]. tostring ();
3. COOKIE: the cookie is used to save the request information sent from the client browser to the server page. The validity period can be set manually, and the stored data volume is limited. Therefore, do not save datasets and other large amounts of data. Moreover, cookies store data in plain text on the client's computer, so it is best not to store sensitive unencrypted data.
Use:
// Store information
Response. Cookie ["nameid"]. value = "0001 ";
// Read information
String nameid = response. Cookie ["nameid"]. value;
4. viewstate Program Performance. All Web server controls use viewstat to save their own status information during page sending. Each control has its own viewstate. It is best to disable it when not in use to save resources. You can disable the viewstate of the entire page by adding the "enableviewstate = false" attribute to the @ page command.
Use:
// Access information
Viewstate ["nameid"] = "0001 ";
// Read information
String nameid = viewstate ["nameid"]. tostring ();
5. cache: used to save pages and data between HTTP requests. It allows you to store a large number of frequently accessed server resources in the memory. When a user sends the same request, the server returns the information stored in the cache to the user instead of processing it again, this reduces the server's request processing time.
Use:
// Store information
Cache ["nameid"] = "0001 ";
// Store information
Cache. insert ("nameid", "0001 ");
// Read information
String nameid = cache ["nameid"]. tostring ();
Note: you must use the cache. insert method or cache. Add method to add information if you use the functions provided by the cache, such as cleanup, expiration time, and dependency.
6. hidden domain: the hidden control is an HTML-type server control that can hide a domain. It is no different from other spaces except that it is not displayed in the browser and is always hidden.
Use:
// Store information
Hidden. value = "0001 ";
// Read information
String nameid = hidden. value;
7. query string: connect the passed value to the end of the URL and use the response. Redirect method to redirect the client.
The transfer method is as follows:
Response. Redirect ("list. aspx? Nameid = 0001 & gradeid = 002 ");
After executing the preceding statement Code As follows:
Http://www.QQview.com/List.aspx? Nameid = 0001 & gradeid = 002
After you jump to list. aspx, you can use the following code to obtain the transmitted information:
String nameid, gradeid;
Nameid = request. Params ["nameid"];
Gradeid = request ["gradeid"];
Compare and summarize the specific environment of each object application:
Method information size storage time application range storage location
All user server segments of an application of any size throughout the life cycle of the application
A small number of sessions, simple data user activity time + general delay time (generally 20 minutes) for a single user server segment
A small amount of cookies. You can set a single user client as needed for simple data.
Small viewstate, simple data a Web page life cycle a single user Client
You can set all user server segments as needed.
Hiding a small amount of fields, simple data, a web page, lifecycle, a single user Client
Query a small amount of string, simple data until the next page Jump request a single user Client
A small amount of data that remains unchanged or rarely changes in the web. config file until the configuration file is updated to all users

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.