How to optimize counters using ASP

Source: Internet
Author: User

Many websites have a stenographer to record website visits, which provides administrators with a great deal of convenience to know the operation and access of the website. I have studied a lot of counter programs written in ASP, and found that most of them are text files or databases when a visitor accesses the site, reading the previous Count value and adding 1, then write the file. If the website has a large access volume, it may cause a great burden on the system. Is there any optimization method? After research and testing, the answer is: yes.

Anyone familiar with ASP knows that ASP provides an application attribute to save some public variables of the server. We can use this variable to save the information of the calculator.

The idea is to first set two Application variables, one application ("totalcount"), to save the values; the other application ("lastwritetime "), used to save the time when the last record value was saved to the file. We can define the time interval for saving the Count value to the file, for example, 1 hour, 1 day, or 1 month. When a visitor visits a website, add 1 to the application ("totalcount"). If the time difference between the last storage count and the current count is greater than the preset storage interval value, then, the current Count value is written to the file for storage, which reduces the IO operations of the program and saves the burden on the system.

In order to avoid accidents, such as power failure or server stop response, which need to be restarted, we can set the storage interval to 2 hours, so that even in case of accidents, the loss will not be too great.

The routine is as follows:

<%

Dim ofso' defines the FSO Component Object

Dim ofile' defines the object for reading and writing files

Dim ncount 'defines the number of records read from the file

Dim sfilepath 'defines the path of the counter to save the file

Const iinterval = 2' defines the storage interval as 2 hours

Sfilepath = server. mappath ("count/count.txt") 'hypothetical designer file in the countdirectory under the root directory. The file name is count.txt

If application ("totalcount") = 0 or application ("totalcount") = "" then

'If the website is running for the first time, for example, after restarting, We need to read the previous Count value from the file

Set ofso = server. Createobject ("scripting. FileSystemObject") 'instantiate the object ofso

If not ofso. fileexists (sfilepath) then

Ofile = ofso. createtextfile (sfilepath, true) 'create a file if the file does not exist

Parts

Ofile. Write ("1") write 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 time and the last saved Count value is greater than the set time interval, the Count value is re-written to the file

Set ofso = server. Createobject ("scripting. FileSystemObject") 'instantiate the object ofso

Ofile = ofso. opentextfile (sfilepath, true) 'open the 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 website. You are the first visitor to visit this Website" & Application ("totalcount! ")

%>

This routine is passed under Windows2000 iis5.0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.