File Coverage

blib/lib/WWW/RobotRules/Memcache.pm
Criterion Covered Total %
statement 15 72 20.8
branch 0 14 0.0
condition 0 3 0.0
subroutine 5 13 38.4
pod 1 8 12.5
total 21 110 19.0


line stmt bran cond sub pod time code
1             package WWW::RobotRules::Memcache;
2              
3 1     1   21724 use strict;
  1         2  
  1         36  
4 1     1   5 use warnings;
  1         2  
  1         35  
5              
6 1     1   7 use base 'WWW::RobotRules';
  1         6  
  1         977  
7              
8 1     1   11824 use Cache::Memcached;
  1         192598  
  1         33  
9 1     1   9 use Carp;
  1         2  
  1         749  
10              
11             our $VERSION = '0.1';
12              
13             sub new {
14 0     0 1   my ($class, @mem_nodes) = @_;
15 0 0         if (! @mem_nodes) {
16 0           Carp::croak('WWW::RobotRules::Memcache servers required')
17             }
18 0           my $self = bless { }, $class;
19 0           $self->{'memd'} = Cache::Memcached->new({
20             'servers' => [ @mem_nodes ],
21             });
22 0           return $self;
23             }
24              
25             sub no_visits {
26 0     0 0   my ($self, $netloc) = @_;
27 0           my $t = $self->{'memd'}->get("$netloc|vis");
28 0 0         if (! $t) { return 0; }
  0            
29 0           return ( split( /;\s*/, $t ) )[0];
30             }
31              
32             sub last_visit {
33 0     0 0   my ($self, $netloc) = @_;
34 0           my $t = $self->{'memd'}->get("$netloc|vis");
35 0 0         if (! $t) { return 0; }
  0            
36 0           return ( split( /;\s*/, $t ) )[1];
37             }
38              
39             sub fresh_until {
40 0     0 0   my ($self, $netloc, $fresh) = @_;
41 0           my $old = $self->{'memd'}->get("$netloc|exp");
42 0 0         if ($old) {
43 0           $old =~ s/;.*//;
44             }
45 0 0         if (defined $fresh) {
46 0           $fresh .= "; " . localtime($fresh);
47 0           $self->{'memd'}->set("$netloc|exp", $fresh);
48             }
49 0           return $old;
50             }
51              
52             sub visit {
53 0     0 0   my($self, $netloc, $time) = @_;
54 0   0       $time ||= time;
55              
56 0           my $count = 0;
57 0           my $old = $self->{'memd'}->get("$netloc|vis");
58 0 0         if ($old) {
59 0           my $last;
60 0           ($count,$last) = split(/;\s*/, $old);
61 0 0         if ($last > $time) { $time = $last; }
  0            
62             }
63 0           $count++;
64 0           $self->{'memd'}->set("$netloc|vis", "$count; $time; " . localtime($time));
65 0           return 1;
66             }
67              
68             sub push_rules {
69 0     0 0   my($self, $netloc, @rules) = @_;
70 0           my $cnt = 1;
71 0           while ($self->{'memd'}->get("$netloc|r$cnt")) {
72 0           $cnt++;
73             }
74 0           foreach my $rule (@rules) {
75 0           $self->{'memd'}->set("$netloc|r$cnt", $rule);
76 0           $cnt++;
77             }
78 0           return 1;
79             }
80              
81             sub clear_rules {
82 0     0 0   my ($self, $netloc) = @_;
83 0           my $cnt = 1;
84 0           while ($self->{'memd'}->get("$netloc|r$cnt")) {
85 0           $self->{'memd'}->delete("$netloc|r$cnt");
86 0           $cnt++;
87             }
88 0           return 1;
89             }
90              
91             sub rules {
92 0     0 0   my($self, $netloc) = @_;
93 0           my @rules = ();
94 0           my $cnt = 1;
95 0           while (my $rule = $self->{'memd'}->get("$netloc|r$cnt")) {
96 0           push @rules, $rule;
97 0           $cnt++;
98             }
99 0           return @rules;
100             }
101              
102             1;
103             __END__