|
It s probably occurred to you that rereading the quotations.txt file and loading it into the ArrayList object with every round trip is somewhat inefficient. When you run the page, you probably don t see any delay when you click the Show Another Quote button or at least no more delay than normal. So obviously not a huge amount of overhead is involved in rereading the file each time.
Nonetheless, rereading and loading the quotations text file with every round trip is inefficient from a coding standpoint. In our example, the inefficiency makes little difference, but if you had a file of 10,000 quotations, the delay between postbacks would be noticeable, especially if many people were hitting the Web site at the same time.
The solution is to cache (store) the quotations someplace in the server s memory. The process works like this: Whenever you need to read the quotations, you first check to see whether they are in the cache. If they are, you fetch them. If they aren t, you read the quotations file and store it in the cache for future use.
The question is where to cache the quotations. In our example, the information we want to cache the quotations file is the same for everyone who visits the Web site. In other words, individual users don t need to have a different view of the quotations or keep a user-specific version of the quotations.
A good candidate for caching the quotations, therefore, is a special object named Application. The Application object is a piece of memory in the server that can be shared by all the pages in the application that is, all the pages in the same directory. Unlike cookies, for example, the Application object is not user specific. It s not bound to any specific page, and it therefore persists between round trips of a page. In fact, once you ve loaded values into the Application object, the values stay there until no more users are using the application or until the Web server is stopped.
The Application object is similar to an array in that it can contain multiple objects. Instead of maintaining these objects as an ordered list that you access using an index value, however, the Application object stores values by name.
To clarify, the Application object is not suitable for every situation in which you need to store values between postbacks. Because values in the Application object take up server memory, you don t want to store values many megabytes in size in the Application object. And the Application object is not a good place to store information that needs to be customized for each user. We ll examine the options for storing customized information in Chapter 12.
Let me show you how to use the Application object with our quotations file. In the RandomQuotes.aspx page, switch to Code view if you re not in that view already. In the Page_Load event handler, change the initial block of code by adding in the boldfaced lines:
Dim quotesArrayList As ArrayLis If Application("quotations") Is Nothing Then quotesArrayList = New ArrayList() Dim srQuotes As System.IO.StreamReade Tr srQuotes = New System.IO.StreamRea der(path & "\quotations.txt" Catc labelQuote.Text = "Can t find any quotations, sorry! Exit Su End Tr Dim eof As Boolean = Fals Dim quoteLine As Strin While eof = Fals quoteLine = srQuotes.ReadLine( If quoteLine Is Nothing The eof = Tru Els Add the line to the arraylis t quotesArrayList.Add(quoteLine End I End Whil srQuotes.Close( Application("quotations") = quotesArrayList Else quotesArrayList = Application("quotations") End If
The changes to the Page_Load handler are not substantial. After declaring the quotesArrayList variable (the ArrayList object), the code tests whether the quotations element exists in the Application object. If the quotations element doesn t exist, the program reads the quotations.txt file and loads it into the quotesArrayList object that is, the program runs the same logic that it did in the original version of the page. The important difference is that this time, when the program finishes loading the quotations into the quotesArrayList object, it assigns the quotesArrayList object to the quotations element of the Application object. We could have chosen any name for this element; the important thing, of course, is that you store and retrieve items from the Application object using the same element name.
If the quotations element does exist in the Application object, we assign it to the quotesArrayList object and carry on. In other words, we retrieve the values for quotesArrayList out of the cache instead of reading it from the file.
To test the quotations page with the new caching logic, start by closing your browser if you still have it open, to ensure that the application is closed. Then run the page again. Click the Show Another Quote button several times to display different quotations. It s hard to say whether you ll be able to notice any difference in performance after these changes. However, your quotations will have been cached, and even if the difference is small, it s still there.
|