Saving Application Variables Across Web Restarts, Part 2By Daniel Cooper
In Part 1 we discussed the problem - persisting Application variables over
Web server restarts. We also looked at the code for the
recordToFile function. In this part we'll
first dissect the
recordToFile function and then look at the second function,
recordToFile function expects the full file and pathname to your data file. You can
use the one data file for all your values a group of files. Either way, your web server's got to have write
access to wherever you put your file(s). The second and third parameters specify your key and value. In the
readFromFile functions the
FileSystemObject object is used
to read and write the application variables to a text file; for
more information on the
FileSystemObject be sure to check out the
When you call the function it checks to se whether the file already exists using the
objFileSys.FileExists method. If true it will open the file, read all the values into a
dictionary object and delete the old file.
Keys and values are delimited by pipe (
|) characters and occupy one line. To read them I use a
Do ... Loop (specifically,
do while not objFile.AtEndOfStream) and the
objFile.ReadLine method to advance through the file, in much the same manner as iterating a
With each line I use the
split() function to break the it into two elements of an array. (For more
split() be sure to read: Parsing with
join.) It is
important to use
Ubound to make sure the array has the dimensions
expected, otherwise we are going to get runtime errors!
I use a Dictionary object to hold the key/value pairs, which is basically an associative array, which is basically what the file is doing- which makes it very handy for this purpose. If you haven't worked with Dictionary objects before I recommend looking at them as theyíre a very handy way to contain data. For starters read: Utilizing the Dictionary Object in VBScript.
Back to the function. If the file existed it was deleted after its data was moved to the Dictionary object.
Otherwise there was no file to begin with and we have an empty Dictionary object. Either way, we now use use
objDict.Item(strKey) = strValue to add the key/value pair into the Dictionary. If the key
already exists in the Dictionary its value is overwritten, otherwise it is implicitly added.
Now we write the file back to the server by using a
for ... each to move through the Dictionary
and write the key/value pairs delimited by pipes. The last few lines clean up after the objects.
readFromFile() very similar to
recordToFile(). The code follows:
Again we check if the file exists, in order to avoid runtime errors in this case. We start to loop through
the file using a
Do ... Loop and
objFile.ReadLine as before, splitting up strings
split() and looking for the key passed to the function as
strKey. Once the key
is found in the file the function closes the text file, explicitly destroys all the objects that were created
and returns the value. If the function fails to find the key or canít find the file it will get to the end of
its loop and return null.
That's it. With these two functions you basically have an associate array that is disk based and hence persistent.
It does have a few weak points,
recordToFile() is very heavy so you wouldn't want to have it in
the middle of a long loop or anything.
readFromFile() is lighter, unless your data files get
very large. Also, there is no error trapping and if a user tries to write a key or value with a pipe
character in it youíre going to get all sorts of problems.
That said, Iíve found this method to be very handy for storing the dozen or so values that are required to keep my site running. Note that this method only allows storage of simple Application variables - it cannot store objects. Currently it is not setup to store arrays, although that could be added in...
recordToFile()in text format