Published: Friday, June 02, 2000
Saving Application Variables Across Web Restarts, Part 2
By Daniel Cooper
Read Part 1
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, readFromFile!
Firstly the 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
recordToFile and 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
FileSystemObject FAQ.
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
recordset.
With each line I use the split() function to break the it into two elements of an array. (For more
information on split() be sure to read: Parsing with split
and join.) It is
important to use isArray and 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.
You’ll find readFromFile() very similar to recordToFile(). The code follows:
Function readFromFile(strFileName, strKey)
dim objFileSys, objFile, arrLine
Set objFileSys = Server.CreateObject("Scripting.FileSystemObject")
if objFileSys.FileExists(strFileName) then
Set objFile = objFileSys.OpenTextFile(strFileName, 1)
do while not objFile.AtEndOfStream
arrLine = split(objFile.ReadLine, "|")
if isArray(arrLine) then
if Ubound(arrLine) = 1 then
if arrLine(0) = strKey then
readFromFile = arrLine(1)
objFile.close
Set objFile = nothing
Set objFileSys = nothing
Exit Function
end if
end if
end if
loop
end if
readFromFile = null
End Function
|
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
with 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...
Happy Programming!
Daniel Cooper
daniel_cooper@iname.com
Attachments:
Download the source code to readFromFile() and
recordToFile() in text format