Differences among Session, Cookie, Application, ViewState, and Cache

Source: Internet
Author: User

Directory

Only by clarifying these objects can they be well applied to Asp. Net development and better optimize and store data.

Through the above comparison, we should understand almost the same. Next we will introduce the usage of each object in detail.

Viewstate 

The value of viewstate is stored in the html code of the browser. When the browser is closed, the value disappears. That is, viewstate is passed between functions on this page, as to why this method is used, the page may be refreshed after an event occurs. If global variables are defined, viewstate is used.

Store client information on the page

All Web server controls use ViewState to save their own information status during page sending back. If a control does not need to save information during sending back, it is best to disable its ViewState attribute, (check the property Panel) to avoid unnecessary resource waste. You can disable the ViewState of the entire page by setting "EnableViewState = false" on the page.

The ViewState container can store a large amount of data, but it must be used with caution because excessive use may affect the performance. The most obvious difference is that the webpage is slow to open.

Session

Session uses key-value pairs, that is, the ID stores the client, and the value is placed on the server. The user ID is used to find the corresponding value on the server. In this way, the value is placed on the server, there is a time limit. When the time is reached, the server will be automatically released.

The session between the client and the server is stored.

It is used to save the dedicated information of each user. Its lifetime is the user's sustained request time plus a period of time (which can be set in web. config, which is 20 minutes by default ).

The Session information is stored in the server's memory. Of course, you can set the method for saving the Session information (such as the information stored in the SQL database ). Since the user stops using the program and it remains in the memory for a period of time, the efficiency of using the Session object to save user data is very low. For a small amount of data. Session is a good choice.

Cookie

Cookies can be stored in a browser variable. One method is to save the value in the browser variable. When the browser is closed, the cookie ends. The other method is to save the cookie to the hard disk, as long as the time does not expire, you can use it again next time.

To save the request information of the client browser request server page, the programmer can also use it to save non-sensitive content

If the Cookie is set to Min Value, it never expires.

The Cookie storage capacity is limited. Generally, the maximum size of a browser is 4096 bytes. Therefore, it cannot be used to store large amounts of data.

Since not all browsers support cookies and they are saved in plain text, it is best not to save sensitive content. Otherwise, network security is affected.

Application

It is used to save the data information shared by all users. If the stored data does not change or rarely change during the lifetime of the application, use it. However, there is a web. config in asp.net, which may be better.

To use the application, you must consider that any write operation must be completed in the application_onstart event (Global. asax.

Although the application. lock and application. unlock methods are used to avoid Operation synchronization, They serialize application requests, which may cause performance bottlenecks when the Website access volume is large.

Therefore, it is best not to use it to access large datasets.

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.

 

Note: ViewState is maintained.Page StatusThe Application is MaintenanceWeb Application StatusThe entire Web application (site/virtual directory) has only one Session to maintain.Session Status, Each customer has

 

Reference resources:

Http://topic.csdn.net/t/20041028/08/3497971.html

Http://www.cnblogs.com/3stones/archive/2007/03/16/676833.html

Http://zhidao.baidu.com/question/40138645

Http://blog.sina.com.cn/s/blog_5d96a10d0100bikp.html

Http://hi.bccn.net/space-301836-do-blog-id-14009.html

Http://www.lslnet.com/linux/docs/linux-4141.htm

Http://www.360doc.com/content/09/0403/17/32573_3011834.shtml

 

Viewstate 

The value of viewstate is stored in the html code of the browser. When the browser is closed, the value disappears. That is, viewstate is passed between functions on this page, as to why this method is used, the page may be refreshed after an event occurs. If global variables are defined, viewstate is used.

Store client information on the page

All Web server controls use ViewState to save their own information status during page sending back. If a control does not need to save information during sending back, it is best to disable its ViewState attribute, (check the property Panel) to avoid unnecessary resource waste. You can disable the ViewState of the entire page by setting "EnableViewState = false" on the page.

The ViewState container can store a large amount of data, but it must be used with caution because excessive use may affect the performance. The most obvious difference is that the webpage is slow to open.

Session

Session uses key-value pairs, that is, the ID stores the client, and the value is placed on the server. The user ID is used to find the corresponding value on the server. In this way, the value is placed on the server, there is a time limit. When the time is reached, the server will be automatically released.

The session between the client and the server is stored.

It is used to save the dedicated information of each user. Its lifetime is the user's sustained request time plus a period of time (which can be set in web. config, which is 20 minutes by default ).

The Session information is stored in the server's memory. Of course, you can set the method for saving the Session information (such as the information stored in the SQL database ). Since the user stops using the program and it remains in the memory for a period of time, the efficiency of using the Session object to save user data is very low. For a small amount of data. Session is a good choice.

Cookie

Cookies can be stored in a browser variable. One method is to save the value in the browser variable. When the browser is closed, the cookie ends. The other method is to save the cookie to the hard disk, as long as the time does not expire, you can use it again next time.

To save the request information of the client browser request server page, the programmer can also use it to save non-sensitive content

If the Cookie is set to Min Value, it never expires.

The Cookie storage capacity is limited. Generally, the maximum size of a browser is 4096 bytes. Therefore, it cannot be used to store large amounts of data.

Since not all browsers support cookies and they are saved in plain text, it is best not to save sensitive content. Otherwise, network security is affected.

Application

It is used to save the data information shared by all users. If the stored data does not change or rarely change during the lifetime of the application, use it. However, there is a web. config in asp.net, which may be better.

To use the application, you must consider that any write operation must be completed in the application_onstart event (Global. asax.

Although the application. lock and application. unlock methods are used to avoid Operation synchronization, They serialize application requests, which may cause performance bottlenecks when the Website access volume is large.

Therefore, it is best not to use it to access large datasets.

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.

 

Note: ViewState is maintained.Page StatusThe Application is MaintenanceWeb Application StatusThe entire Web application (site/virtual directory) has only one Session to maintain.Session Status, Each customer has

 

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.