Caching Data in the Web Servers Session

Because of the connectionless nature of Web communications, every request received by the Web server is treated as a request from a brand new user. This makes it difficult to know where users have been and what information has already been provided to them. Take an ecommerce Web site with a shopping cart feature for instance. Without some way to persist which items a visitor has placed into his shopping cart as the user navigates from one page to another, the shopping cart will always remain empty. In order to remember which items a user selected as he travels from page to page, the Web site needs to save this information using a client-side cookie, a database, a text file on the server, or a user's session on the Web server.

A user's Web server session is a special area in memory that is directly tied to an individual visitor to the Web site. If the Web server currently has 10 visitors, there will be 10 sessions being stored in memory. Each Web session has a timeout period. If a visitor does not return to the Web server and request another Web page within the designated timeout period, the session and all of its data is destroyed by the Web server to make room for new sessions. By default, the session's timeout length is 20 minutes, although Web server administrators can increase or decrease this value as needed.

For Web site developers, the Web server's session provides a powerful tool to persist data associated with a particular user. Imagine that you are developing an online bookstore, and you want to store the name of the book a user is ordering while you present him with a Web page asking for his mailing address and credit card information. The following example shows a Button's Click event that would write this data to the user's session:

Session("BookName") = strBookName

As long as the user stays active on this Web site, the session will remain active and will remember the name of the book the user selected. To access this information in the Web server's session, all you need to do is assign to it to a variable like so:

StrBookToOrder = Session("BookName")

A developer could also opt to store the book's title in a client-side cookie, but if the user has cookies disabled in his browser, this plan would fail. You could also use a database in which to create a permanent or temporary user account, and then add books to that account to persist the data. Unfortunately, database calls come at a high price in terms of application performance, so this may not be the best choice for a visitor that may or may not actually purchase all of the books he is looking at. Storing data in the Web server's session is the fastest and most efficient way to persist this data. Access to data stored within a session is almost instantaneous because the session is directly connected to the Web server that is responsible for processing your Web Form. If the user abandons his shopping spree and logs off, the Web server will delete the session data after the timeout period expires. This feature eliminates the problem of cleaning up unneeded cookies or data stored in a database.

0 0

Post a comment