- 论坛徽章:
- 0
|
新版带缓存的程序
- # cat /etc/pftable/red.pl
- #!/usr/bin/perl -w
- # author fengyong 2008-01-22
- use strict;
- use DB_File;
- use vars qw (%cache $uri $cachetime);
- $|=1;
- my $timeout=3600;#缓存超时时间
- while (<>){
- my ($client,$ident,$method)=();
- ($uri,$client,$ident,$method)=split;
- if (check($client)){
- next;
- }else{
- save($client);
- $uri="301:[url]http://www.xxxx.cn[/url]";
- }
- }continue{
- print $uri;
- }
- sub check {
- my $client=shift;
- # init cache time
- my $time=time();
- if (!$cachetime){
- $cachetime=$time+$timeout;
- }
- if ($time > $cachetime){
- %cache=();
- $cachetime=();
- }
- return 1 if $cache{$client};
-
- # reopen db
- my %ip=();
- tie %ip,"DB_File","/tmp/ip.db",O_CREAT|O_RDWR,0666;
- %cache=%ip; #hard copy?
- untie %ip;
- $cache{$client} ? 1:0;
- }
- sub save {
- my $client=shift;
- my %ip=();
- tie %ip,"DB_File","/tmp/ip.db",O_CREAT|O_RDWR,0666;
- $ip{$client}=1;
- untie %ip;
- }
复制代码
[ 本帖最后由 fengyong 于 2008-1-22 16:09 编辑 ] |
|