Counter | Optimization Many of the sites have registers, used to record the number of visits to the site, which gives webmasters instant understanding of the operation of the site and access to provide a lot of convenience. The author has studied many counter programs written by ASP, found that most of the time when a visitor to the site to play a text file or database, read the previous count to add 1, and then write to the file, if the site traffic is very large, may cause a great burden on the system, then there is no optimization method, The author after the research test, the answer is: have.
Familiar with the ASP's friends know that ASP provides a application property to save some of the server's public variables, we can use this variable to save the Register information.
The idea is to set two application variables, a application ("TotalCount") to hold the value, and another application ("LastWriteTime") to save the last time the value was saved to the file. We can define our own time intervals for saving the counted values to a file, such as 1 hours, 1 days, or one months. When visitors visit the site, let application ("TotalCount") to add 1, if the last save count time and now the difference is more than we set the save time interval value, then write the current count value to save the file, which reduces the program IO operation, Save the burden of the system.
In order to avoid accidents, such as power outages or server stop response needs to be restarted and so on, we can set the save time interval of 2 hours, so that even if an accident, the loss will not be too large.
The routines are as follows:
<%
Dim oFSO ' defines the FSO Component object
Dim ofile ' defines a read-write file object
Dim ncount ' defines the values read from a file
Dim Sfilepath ' defines the path of the counter save file
Const iinterval=2 ' defines save time interval of 2 hours
Sfilepath=server.mappath ("Count/count.txt") ' assumes that the counter file is in the Count directory under the root directory and the file name is Count.txt
IF application ("TotalCount") =0 or Application ("totalcount") = "" THEN
"If you run the site for the first time, such as a reboot, we need to read out the previous count from the file
Set ofso=server.createobject ("Scripting.FileSystemObject") ' Instantiate a File action object oFSO
IF not ofso.fileexists (Sfilepath) THEN
Ofile=ofso.createtextfile (sfilepath,true) ' If the file does not exist, create a text
Thing
Ofile.write ("1") writes the current count value "1"
Ofile.close
Application ("TotalCount") =1
ELSE
Set ofile = Ofsot.opentextfile (Sfilepath)
Ncount=ofile.readline
Application ("TotalCount") =clng (ncount) +1
Ofile.close
End IF
Application ("lastwritetime") = Now ' Sets the last access time to the current time
ELSE
Application ("totalcount") = Application ("TotalCount") +1
IF DateDiff ("H", Application ("LastWriteTime"), now >iinterval THEN
' If the time difference between the current and last saved count is greater than the set time interval, the value is rewritten to the file
Set ofso=server.createobject ("Scripting.FileSystemObject") ' Instantiate a File action object oFSO
Ofile=ofso.opentextfile (sfilepath,true) ' Open file '
Ofile.write (Application ("TotalCount")) ' writes the current count value
Ofile.close
Application ("lastwritetime") = Now ' Sets the last access time to the current time
End IF
End IF
Response.Write ("Welcome to this site, you are visiting the site of the first" & Application ("TotalCount") & "Visitors!" ")
%>
This routine is passed under Windows2000 IIS5.0.