I have created a new mod based on the 'Display # of Records' Mod. This new mod will count the number of records after a specified time ($update_time). This will reduce the load on your server and the time it takes to load the page with the &num_records on it. It reads the count from a file instead of counting every record in your DB again and again, (oh yeah - and again).
$update_time should depend on how busy your DB is and how exactly accurate you want your count. It only counts when someone goes to the page that calls the &num_records AND if time is greater than $update_time. I have mine set to 60 minutes. I know I have about 50 visitors an hour that will go to my database and search, but maybe only 2-4 an hour add an entry. This means after the first visitor activates the NEWCOUNT, the next 49 people won't have to wait for a count because it's already in count.txt, and I have over 25,000 records in my DB!
To make this work, create 2 new files called time.txt (put 955955955 in it) and count.txt (put 0 in it). Upload them in ASCII where your db.cgi is in. You DON'T have to chmod them.
In default.cfg add:
# Number of seconds between update of count.txt
# 1800 = 30 minutes, 3600 = 1 hour, 14400 = 4 hours
In db.cgi add new subroutines (REPLACE 'sub num_records' if you have the 'Display # of Records' Mod installed):
#-----------------------------------------------
$current_time = time();
#open last time records were counted
open (LASTTIME,"<time.txt") or &cgierr("error in num_records. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(LASTTIME, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!"); }
$last = <LASTTIME>;
close (LASTTIME);
$doit = $current_time - $last;
#check if it's time to count again
if ($doit > $update_time) {
#if so, overwrite time.txt with new time
open (NEWTIME,">time.txt") or &cgierr("error in num_records. unable to open database: time.txt.\nReason: $!");
if ($db_use_flock) { flock(NEWTIME, 2) or &cgierr("unable to get exclusive lock on time.txt.\nReason: $!"); }
print NEWTIME "$current_time";
close (NEWTIME);
#run sub to count records
&thecount
}
#opens count file and retrieves count
open (COUNT,"<count.txt") or &cgierr("error in num_records. unable to open file: count.txt.\nReason: $!");
if ($db_use_flock) { flock(COUNT, 2) or &cgierr("unable to get exclusive lock on count.txt.\nReason: $!"); }
$count = <COUNT>;
close (COUNT);
print $count; #prints count from count.txt
}
sub thecount {
#-----------------------------------------------
my $count = 0;
#opens db and counts records
open (DB, "<$db_file_name") or &cgierr("error in thecount. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!"); }
LINE: while (<DB> ) {
foreach (LINE) {
++$count;
}
}
close DB;
#opens count file and replaces old count with new count
open (NEWCOUNT,">count.txt") or &cgierr("error in thecount. unable to open file: count.txt.\nReason: $!");
if ($db_use_flock) { flock(NEWCOUNT, 2) or &cgierr("unable to get exclusive lock on count.txt.\nReason: $!"); }
print NEWCOUNT "$count";
close (NEWCOUNT);
}
In html.pl add where you want to show the number of records:
Anything to take the stress off of a server helps!
Have a day!
AJ
[This message has been edited by TheFew (edited May 02, 2000).]
$update_time should depend on how busy your DB is and how exactly accurate you want your count. It only counts when someone goes to the page that calls the &num_records AND if time is greater than $update_time. I have mine set to 60 minutes. I know I have about 50 visitors an hour that will go to my database and search, but maybe only 2-4 an hour add an entry. This means after the first visitor activates the NEWCOUNT, the next 49 people won't have to wait for a count because it's already in count.txt, and I have over 25,000 records in my DB!
To make this work, create 2 new files called time.txt (put 955955955 in it) and count.txt (put 0 in it). Upload them in ASCII where your db.cgi is in. You DON'T have to chmod them.
In default.cfg add:
Code:
$update_time = "1800"; # Number of seconds between update of count.txt
# 1800 = 30 minutes, 3600 = 1 hour, 14400 = 4 hours
In db.cgi add new subroutines (REPLACE 'sub num_records' if you have the 'Display # of Records' Mod installed):
Code:
sub num_records { #-----------------------------------------------
$current_time = time();
#open last time records were counted
open (LASTTIME,"<time.txt") or &cgierr("error in num_records. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(LASTTIME, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!"); }
$last = <LASTTIME>;
close (LASTTIME);
$doit = $current_time - $last;
#check if it's time to count again
if ($doit > $update_time) {
#if so, overwrite time.txt with new time
open (NEWTIME,">time.txt") or &cgierr("error in num_records. unable to open database: time.txt.\nReason: $!");
if ($db_use_flock) { flock(NEWTIME, 2) or &cgierr("unable to get exclusive lock on time.txt.\nReason: $!"); }
print NEWTIME "$current_time";
close (NEWTIME);
#run sub to count records
&thecount
}
#opens count file and retrieves count
open (COUNT,"<count.txt") or &cgierr("error in num_records. unable to open file: count.txt.\nReason: $!");
if ($db_use_flock) { flock(COUNT, 2) or &cgierr("unable to get exclusive lock on count.txt.\nReason: $!"); }
$count = <COUNT>;
close (COUNT);
print $count; #prints count from count.txt
}
sub thecount {
#-----------------------------------------------
my $count = 0;
#opens db and counts records
open (DB, "<$db_file_name") or &cgierr("error in thecount. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!"); }
LINE: while (<DB> ) {
foreach (LINE) {
++$count;
}
}
close DB;
#opens count file and replaces old count with new count
open (NEWCOUNT,">count.txt") or &cgierr("error in thecount. unable to open file: count.txt.\nReason: $!");
if ($db_use_flock) { flock(NEWCOUNT, 2) or &cgierr("unable to get exclusive lock on count.txt.\nReason: $!"); }
print NEWCOUNT "$count";
close (NEWCOUNT);
}
In html.pl add where you want to show the number of records:
Code:
|; &num_records print qq|Anything to take the stress off of a server helps!
Have a day!
AJ
[This message has been edited by TheFew (edited May 02, 2000).]