When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs



















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Product Manager, Testing (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume

Published: Friday, June 02, 2000

Saving Application Variables Across Web Restarts
By Daniel Cooper


A while ago I had a problem with a site that I was working on. One of the requirements called for a featured product to appear in the footer of the site, which would be changed monthly by the client through a back-end interface.

- continued -

Usually when one has to dynamic information for a site a database is the answer. In this case I thought "This is stupid. I'm not going to do a database call to retrieve a single value and I'm not going to have an orphan table in the database to only hold that value!"

The obvious solution is to use an application variable. After all, it’s global, can be set easily and have little overhead. The problem is that the webserver gets rebooted every week, meaning that any settings in the application variables would be lost. My problem was: "How do I make application wide values persistent?"

What I did was write two functions. One writes key/value pairs to a text file in a format that the second function can read back. I set up the site so that when the site's operator set a value, it was written both to the text file and to an application variable. Then when the application restarts, the Global.asa file contains code to initialize those variables to the previous values set. Note that this approach will only persist textual application variables since there is no way to represent a binary object through a text file.

So the code to record application variable values looks a little like this:

Application("featured_product_id")  =  someVariable
Call recordToFile(someFileName, "featured_product", someVariable)

And in the Application_OnStart event in Global.asa we have a line like:

Application("featured_product_id") = readFromFile(fileName, "featured_product")

So once you set a value in both an application variable and in the file you can access it throughout the site with little overhead and the confidence of knowing that if your web application is restarted the value will persist.

Here's how its done:

  • recordToFile() - writes a key/value pair to a text file you specify, I use data.txt but it can be any filename. It creates the file if it doesn’t already exist and will create a key/value pair or set the value if it exists.
  • readFromFile() - opens your data file and retrieves the value for a given key, or null if it can’t find it.

Following is the code to recordToFile. In Part 2 we'll look at the code for readFromFile and dissect both functions.

  • Part 2


    function recordToFile(strFileName, strKey, strValue)
      dim objFileSys, objFile, arrLine, objDict, thing
    
      Set objFileSys = Server.CreateObject("Scripting.FileSystemObject")
      Set objDict = Server.CreateObject("Scripting.Dictionary")
    
      if objFileSys.FileExists(strFileName) then
        'case where file already exists and presumably holds values
        Set objFile = objFileSys.OpenTextFile(strFileName, 1)
    
        do while not objFile.AtEndOfStream
          arrLine = split(objFile.ReadLine, "|")
    
          if isArray(arrLine) then
            if Ubound(arrLine) = 1 then
              objDict.Add arrLine(0), arrLine(1)
            end if
          end if
        loop
        
        objFile.close
        objFileSys.DeleteFile(strFileName)
      end if
    		
      'insert/update new item
      objDict.Item(strKey) = strValue
    
      Set objFile = objFileSys.OpenTextFile(strFileName, 2, true)
      for each thing in objDict
        objFile.writeLine thing & "|" & objDict.Item(thing)
      next
    
      objFile.Close
      Set objDict = Nothing
      Set objFile = Nothing
      Set objFileSys = Nothing
    End Function
    


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article