Gungho/Component/RobotRules/Storage/Cache.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 130 CC 8 SCAR 22.9
CC
8
Cov
0%
push_pending_robots_txt
20.0
get_pending_robots_txt
12.0
get_rule
6.0
CRAP 9.9
1 # $Id: /mirror/gungho/lib/Gungho/Component/RobotRules/Storage/Cache.pm 31308 2007-11-29T13:15:39.399180Z lestrrat $
2 #
3 # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4 # All rights reserved.
5  
6 package Gungho::Component::RobotRules::Storage::Cache;
7 use strict;
8 use warnings;
9 use base qw(Gungho::Component::RobotRules::Storage);
10  
11 __PACKAGE__->mk_accessors($_) for qw(expiration);
12  
13 sub setup
14 {
15 my $self = shift;
16 my $c = shift;
17 my %config = %{ $self->{config} };
18 my $module = delete $config{module} || 'Cache::Memcached';
19 my $expiration = delete $config{expiration} || 86400 * 7;
20  
21 Class::Inspector->loaded($module) or $module->require or die;
22  
23 $self->storage( $module->new(%config) );
24 $self->expiration( $expiration );
25 $self->next::method(@_);
26 }
27  
28 sub get_rule
29 {
30 my $self = shift;
31 my $c = shift;
32 my $request = shift;
33  
34 my $uri = $request->original_uri;
35 my @args;
36 my $storage = $self->storage;
37 if ($storage->isa('Cache::Memcached::Managed')) {
38 @args = (id => $uri->host_port, key => 'robot_rules.rule');
39 } else {
40 @args = ($uri->host_port);
41 }
42 my $rule = $self->storage->get( @args ) || '';
43 $c->log->debug("Fetch robot rules for $uri ($rule)");
44 return $rule || ();
45 }
46  
47 sub put_rule
48 {
49 my $self = shift;
50 my $c = shift;
51 my $request = shift;
52 my $rule = shift;
53  
54 my $uri = $request->original_uri;
55 $c->log->debug("Saving robot rules for $uri");
56  
57 # Cache:::Memcached::Managed is a bad boy and breaks API compatibility
58 # with the rest of the Cache::* modules
59 my @args;
60 my $storage = $self->storage;
61 if ($storage->isa('Cache::Memcached::Managed')) {
62 @args = (id => $uri->host_port, key => 'robot_rules.rule', value => $rule, expiration => $self->expiration);
63 } else {
64 @args = ($uri->host_port, $rule, $self->expiration);
65 }
66 $self->storage->set( @args );
67 }
68  
69 sub get_pending_robots_txt
70 {
71 my ($self, $c, $request) = @_;
72  
73 my $uri = $request->original_uri;
74 my $host_port = $uri->host_port;
75 my @args;
76 my $storage = $self->storage;
77 my $is_managed = $storage->isa('Cache::Memcached::Managed');
78  
79 if ($is_managed) {
80 @args = (id => $host_port, key => 'robot_rules.pending_robots_txt');
81 } else {
82 @args = ($host_port);
83 }
84  
85 $storage->can('delete') ?
86 $storage->delete(@args) :
87 $storage->remove(@args)
88 ;
89 return delete $c->pending_robots_txt->{ $host_port };
90 }
91  
92 sub push_pending_robots_txt
93 {
94 my ($self, $c, $request) = @_;
95  
96 my $uri = $request->original_uri;
97 my $host_port = $uri->host_port;
98 my @args;
99 my $storage = $self->storage;
100 my $is_managed = $storage->isa('Cache::Memcached::Managed');
101  
102 if ($is_managed) {
103 @args = (id => $host_port, key => 'robot_rules.pending_robots_txt');
104 } else {
105 @args = ($host_port);
106 }
107  
108 # If it already exists in the cache, just return
109 if (my $pending = $storage->get(@args)) {
110 $c->log->debug("storage->get(@args) returned $pending");
111 return 0;
112 }
113  
114 $c->pending_robots_txt->{ $host_port } ||= {};
115 my $h = $c->pending_robots_txt->{ $host_port };
116  
117 # pending requests are still stored in-memory
118 $c->log->debug("Pushing request $uri to pending list (robot rules)...");
119  
120 my $pending_robots_txt_timeout = 180;
121 if ($is_managed) {
122 push @args, (value => 1, expires => $pending_robots_txt_timeout);
123 } else {
124 push @args, 1, $pending_robots_txt_timeout;
125 }
126  
127 $self->storage->set( @args );
128 $h->{ $request->id } = $request ;
129 return 1;
130 }
131  
132 1;
133  
134 __END__