There are many ways to implement data caching in the past, with client cookies, server-side sessions, and application.
A cookie is a set of data that is stored on the client and is used primarily to hold personal information such as user names.
Session saves the dialog information. Application is the information stored throughout the application, which is equivalent to a global variable.
Session
Session is used to save proprietary information for each user
The lifetime of the session is the duration of the user's request plus a period of time (usually about 20 minutes).
Session information is stored in the Web server memory, save the amount of data can be large and small
This method is less efficient because the user remains in memory for a period of time after the application has stopped using it
Code:
session["UserID"]= "test";
String username=session["UserID"]. ToString ();
Cookies
Cookies are used to hold request information from the client browser requesting the server page
We can store sensitive user information, save time can be set according to need
If the cookie expiration date is not set, its lifecycle is saved until the browser is closed
The Expires property of the cookie object is set to MinValue that never expires
The amount of data stored by cookies is limited, and most browsers are 4K so do not store large data
Since not all browsers support cookies, the data will be stored in clear text on the client
Code:
resopnse.cookies["UserID"]= "test";
String username= resopnse.cookies ["UserID"]. ToString ();
Cache
Cache is used to save pages or data during an HTTP request
The use of cache can greatly improve the efficiency of the whole application
It allows frequently accessed server resources to be stored in memory, and when the user makes the same request, the server does not process it again but returns the data stored in the cache directly to the user
You can see that the cache saves time-server processing time
The cache instance is proprietary to each application, its lifecycle = = the application cycle
Application restart will re-create its instance
Note: If you want to use features such as cache cleanup, expiration management, dependencies, and so on, you must use the Insert or Add method method to add information
Code:
cache["id"]= "CC" or Cache.Insert ("id", "test");
String ID =cache["id"]. ToString ();
Usually the most frequently used is the session, then the session and cache what is the difference.
The difference between session caching and cache caching.
(1) The biggest difference is that the cache provides caching dependencies to update the data, and session can only rely on the defined cache time to determine whether the cached data is valid.
(2) Even if the application terminates, the cached data still exists the next time the application is opened, as long as the cache time defined in the Cache.Add method does not expire. The session cache only exists in one session, and the data is invalidated after the conversation has ended.
(3) session is easy to lose, resulting in the uncertainty of the data, and cache does not appear this situation.
(4) because the session is loaded each time, it is not appropriate to store a large amount of information, otherwise it will result in lower performance of the server. And cache is mainly used to save large capacity information, such as multiple tables in the database.
(5) The session can only be saved in memory at present and has an effect on its performance.