Optimize your counter

zhaozj2021-02-16  46

Write the optimization method of the counter with an ASP

There are many websites that have a record that records the number of websites, which provides a lot of convenience to the website administrator instantly understanding of this website. The author studied a lot of counter programs written in ASP, found that most of them play a text file or database when a visitor access site, read the previous count value to add 1, then write files, if the website is accessible The amount is very large, may have a big burden on the system, then there is no optimization method, the author has been study and testing, the answer is:

Friends who are familiar with ASP know that ASP provides an Application property to save some public variables of the server, we can use this variable to save the information.

The idea is to set two Application variables, an application ("totalcount") to save the value; another Application ("LastWrittime"), is used to save the last time the value is saved to the file. We can define your own interval for saving the count value to the file, such as 1 hour, 1 day or one month. When there is a visitor website, let Application ("totalcount") plus 1, if the time of the last saved count is greater than the current time difference, the current count value is written to the file. Save, so that the IO operation of the program is reduced, saving the burden of the system.

To avoid accidents, such as power failure or server stopping reactions need to be restarted, we can set the save time interval to 2 hours, so that even if there is an accident, the loss will not be too large.

The routines are as follows:

<%

DIM OFSO 'Defines FSO Components Objects

DIM Ofile 'Defines Read and write file objects

DIM NCOUNT 'Defines the count value read from the file

DIM SFILEPATH 'Defines the path to save the file

Const Iinterval = 2 'Define Save Time Interval is 2 hours

SfilePath = server.mappath ("count / count.txt") 'assumes that counter files in the count directory in the root directory, file named count.txt

IF Application ("Totalcount") = 0 or Application ("Totalcount") = "" ""

'If you run the site for the first time, after the restart, we need to read the previous count value from the file.

Set ofso = server.createObject ("scripting.filesystemObject") 'Instantiate File Operation Objects OFSO

IF not offsfileexists (sfilepath) THEN

Ofile = OFSO.CREATEXTFILE (SFILEPATH, TRUE) 'If the file does not exist, create a text.

Piece

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 ("LastWrittime" = now 'Set the last time for the current time ELSE

Application ("Totalcount") = Application ("Totalcount") 1

IF Datediff ("H", Application ("Lastwritetime"), NOW> IINTERVAL THEN

'If the current time is greater than the time difference of the last saved count value, the count value is rewritten to the file.

Set ofso = server.createObject ("scripting.filesystemObject") 'Instantiate File Operation Objects OFSO

Ofile = OFSO.OpenTextFile (sfilepath, true) 'Opens the file

Ofile.Write ("TotalCount") 'Write the current count value

Ofile.close

Application ("LastWrittime" = now 'Set the last time for the last time for the current time

END IF

END IF

Response.write ("Welcome to this website, you are the" Totalcount ") &" Totalcount ") &" Totalcount ") &" Totalcount ") &" Totalcount ") &" Totalcount ") &" Totalcount ") &" Totalcount ") &" Totalcount ")

%>

This routine is passed under Windows2000 IIS5.0.

转载请注明原文地址:https://www.9cbs.com/read-26096.html

New Post(0)