In ASP, the default Session expiration time is 20 minutes.

Source: Internet
Author: User

Anyone who has written a slightly larger ASP knows that the Session object is really useful. It can be used to record users' private data variables, which is safe and convenient. But do you really know how the Session works? Maybe you will never dare to use this loving and hateful object again. Although it is a little difficult to replace the method, it will have to be done in the long term.

First, let's talk about the benefits of Session. It can be used to record the private data variables of the client and will not disappear within the time range. This is really an important feature, especially for systems with members. Such as the member's login account, time, status, and real-time data records of this record. For example, the shopping system records the product inventory in the user's shopping basket. This information is required by each user, generally, developers use Session record processing.

However, sessions in ASP are made up of Cookies. The server transmits the information recorded in all sessions to the user's browser as Cookies. Generally, browsers store these Cookies. When users click the link and connect to the Server again, the browser will send these Cookies back to the Server for processing. This is the working principle of the Session. When the amount of data is large, it must be transferred out and retrieved, not only consuming the line bandwidth, but also reducing the efficiency, this is because the Server must spend more resources for online processing, memory reconfiguration, and other initial actions. Now, you may think, "I have to use this function, so I have to sacrifice some resources." However, in this article, Session is to teach you how to use it less. In addition, there are alternatives to play next to it, is the same Global. the Application object in the asa.

Application is also a good tool for recording and processing temporary data. Its capabilities and usage are the same as those of Session. However, compared with Session, the information recorded by Application is public, that is, the variable space that any user can share. Unlike the Session, the Application does not send data to the user. After the next online operation, the Application reads data back. The Application directly records the memory on the Server, which is more efficient than Session.

Because the Application object is public, the first thing that must be done is to plan a public region for each user so that each user has its own region to record data, to simulate the Session. There are two methods:

1. initialize the establishment and allocate user memory space before activating the Server. This method usually takes up a lot of resources when the Server is started, however, it also saves the trouble of allocating resources whenever users are online. However, there is a limit. To use this method, you must limit the maximum number of people. Since it is initialized Upon activation, we can only estimate the amount of memory space to be created, therefore, this method is usually used in small programs such as chat rooms.

2. This method should be appropriate for large applications. The dynamic allocation method is used to allocate resources to this user only when the user is online to the Server for the first time. These two Session simulation solutions aim to reduce the consumption of Session resources, but they cannot be completely replaced. We still need to use a little bit of Session, at least it can reduce the burden on the Server.

Solution 1

First, let's start the implementation of the first scheme. Because the Application is initialized during activation, we certainly need to start from Global. asa:

Initialization has been completed, but how can I use it? You only need to change the information stored in the Session, such as the account number and logon time, to the Application object we have created:Copy codeThe Code is as follows: 'Look for unused space
For I = 1 To Application ("ClientMax ")
If Application ("User_Status _" & I) = 0 Then
'User temporary ID
Session ("Index") = I
Application. Lock
'Set to used status
Application ("User_Status _" & I) = 1' put the variable data
Application ("User_Account _" & I) = Account
Application ("User_Logtime _" & I) = Now ()
Application. Unlock
End If

To obtain user-related variable data, follow these steps:

Response. Write (Application ("User_Account _" & Session ("Index "))

You may find that you do not want to use Session? Why is there a Session in the original code above? As mentioned above, this alternative solution cannot completely replace the Session. The browser is not always online with the Server and will be disconnected after reading the page, so how do we know that we will connect to the same person next time? At this time, we have to rely on the Session. We give users a set of real-time numbers, which are the number of the variable space on the user's Application. You can imagine that there are many safe deposit boxes in the bank, you have a key, and the key has a number. The number on the key allows the staff to lead you to your own safe. There are still some improvements to this method, but it is enough for small applications.

Solution 2

As for the previous scheme, you may also think that the custom number uses Session to record the number. Speaking of the number, the Session object provides a "SessionID" method. Yes, no matter whether we want to use it or not, the Server will automatically provide a number for each user, and this number will not be repeated. As for this number, it is obtained using Session. SessionID. This number is an action that the Session will do. We can use it to replace the number program we have written, saving effort and even providing greater scalability. However, basically, the first solution above is still applicable to small applications, such as chat rooms with limited numbers. The second alternative is to target larger systems.

For websites with hundreds or even tens of thousands of people on the website each second, using the previous solution will definitely not work. If you set the maximum number of users to 10000, Server will cut out 10 thousand regions for 10 thousand users as soon as it is activated. If there are 5 variables in one region, A variable occupies 32 bytes (Byte), and 10000 represents more than 320000 KB (320 MB). As soon as the Server is activated, so much garbage is inserted into the memory, performance is bound to be much lower before it reaches the battlefield. Although there are few such numbers, I think that my 512 MB will be enough. The above figure assumes a minimum number, in addition, it is unknown how many resources the Server will use When configuring the memory, so it will only be more and not lower. Therefore, the solution is to dynamically configure the user variable space. When a user is online with the Server, a region is switched out. Therefore, a large memory size does not need to be configured in advance.

The second solution is relatively simple. Please discard all the things in the first solution. We don't need to move to Global. asa, just need to change the user's login location and other useful places:Copy codeThe Code is as follows: 'lock ApplicationApplication. lock' put the variable data
Application ("User_Account _" & Session. SessionID) = Account
Application ("User_Logtime _" & Session. SessionID) = Now () 'Unlock Application. Unlock

To obtain user-related variable data, follow these steps:Copy codeThe Code is as follows: Response. Write (Application ("User_Account _" & Session. SessionID ))

In the past, I read a lot of books and wrote that sessions are very hard to eat resources and should not be used as much as possible. However, I still have to use them when necessary, and I have not taught a proper solution in the book. Now you know how to replace the Session and make good use of it! Maybe the performance problem that is always plagued can be improved a lot!

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: 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.