blib/lib/Cache/AgainstFile/CacheModule.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 123 CC 14 SCAR 27.4
CC
14
Cov
0%
get
42.0
new
20.0
count
12.0
CRAP 15.6
1 ###############################################################################
2 # Purpose : Cache::AgainstFile wrapper for CPAN caching modules
3 # Author : John Alden
4 # Created : 25 Apr 2005
5 # CVS : $Id: CacheModule.pm,v 1.9 2005/10/31 21:10:41 johna Exp $
6 ###############################################################################
7  
8 package Cache::AgainstFile::CacheModule;
9  
10 use strict;
11 use vars qw($VERSION %StatHistory);
12 $VERSION = sprintf"%d.%03d", q$Revision: 1.9 $ =~ /: (\d+)\.(\d+)/;
13  
14 sub new {
15 my ($class, $loader, $options) = @_;
16
17 #Load appropriate backend class on demand
18 my $backend_name = $options->{CacheModule};
19 die("Package name '$backend_name' doesn't look valid") unless($backend_name =~ /^[\w:]+$/);
20 eval "require $backend_name";
21 die("Unable to load $backend_name - $@") if($@);
22  
23 #Wire up tracing stubs
24 foreach my $stub qw(TRACE DUMP) {
25 no strict 'refs';
26 *{$backend_name."::".$stub} = \&{$stub} if(defined &{$backend_name."::".$stub});
27 }
28  
29 my $backend = $backend_name->new($options->{CacheModuleOptions});
30 my $self = {
31 'loader' => $loader,
32 'options' => $options,
33 'backend' => $backend,
34 'serialize' => _explicitly_serialize($backend),
35 'stat' => \%StatHistory,
36 };
37 return bless $self, $class;
38 }
39  
40 #File statting magic happens here
41 sub get {
42 my ($self, $filename, @opts) = @_;
43 my $record = $self->_get($filename);
44 my ($last_modified, $data);
45 if(defined $record)
46 {
47 ($last_modified, $data) = @$record;
48 unless($self->{options}->{NoStat})
49 {
50 my $grace = $self->{options}->{Grace} || 0;
51 my $last_checked = $self->{'stat'}{$filename} || 0;
52
53 #Are we within the grace period since our last stat?
54 unless($grace > time - $last_checked)
55 {
56 TRACE("stat: $filename");
57 $data = undef if((stat($filename))[9] > $last_modified); #stat and maybe mark as stale
58 $self->{'stat'}{$filename} = time;
59 }
60 }
61 }
62 unless(defined $data)
63 {
64 TRACE("stale: $filename");
65 $data = $self->{loader}->($filename, @opts);
66 $last_modified = (stat($filename))[9];
67 $self->_set($filename, [$last_modified, $data]);
68 }
69 else
70 {
71 TRACE("not stale: $filename");
72 }
73 return $data;
74 }
75  
76 #Forward all other methods to backend
77 sub purge {
78 shift()->{backend}->purge();
79 }
80  
81 sub clear {
82 shift()->{backend}->clear();
83 }
84  
85 sub size {
86 shift()->{backend}->size();
87 }
88  
89 # Allow for slight difference in count functionality between Cache::Cache and Cache APIs
90 sub count {
91 my $backend = shift()->{backend};
92
93 #Cache:: module have a count() method
94 return $backend->count() if($backend->can('count'));
95
96 #Cache::Cache modules currently don't - but they do have a get_keys method
97 if($backend->can('get_keys')) {
98 my @keys = $backend->get_keys();
99 return scalar @keys;
100 }
101
102 return undef;
103 }
104  
105 # Allow for slight difference in serialization between Cache::Cache and Cache APIs
106 sub _set {
107 my $self = shift;
108 if($self->{serialize}) {
109 my ($k, $v, @args) = @_;
110 return $self->{backend}->freeze(@_);
111 } else {
112 return $self->{backend}->set(@_);
113 }
114 }
115  
116 sub _get {
117 my $self = shift;
118 if($self->{serialize}) {
119 return $self->{backend}->thaw(@_);
120 } else {
121 return $self->{backend}->get(@_);
122 }
123 }
124  
125 sub _explicitly_serialize {
126 my ($backend) = @_;
127 return 1 if($backend->isa('Cache'));
128 return 0;
129 }
130  
131 #Log::Trace stubs
132 sub TRACE {}
133 sub DUMP{}
134  
135 1;
136  
137 =head1 NAME
138  
139 Cache::AgainstFile::CacheModule - use Cache or Cache::Cache modules for Cache::AgainstFile
140  
141 =head1 SYNOPSIS
142  
143 use Cache::AgainstFile;
144 my $cache = new Cache::AgainstFile(
145 \&loader,
146 {
147 Method => 'CacheModule',
148 CacheModule => 'Cache::MemoryCache', #This will be loaded on demand
149 CacheModuleOptions => {'default_expires_in' => 300},
150 # ...
151 }
152 );
153  
154 $data = $cache->get($filename);
155  
156  
157 =head1 DESCRIPTION
158  
159 This backend allows you to apply Cache::AgainstFile to any of the modules on CPAN that conform to the Cache::Cache or Cache interfaces. Data structures are automatically serialised into the cache.
160  
161 =head1 OPTIONS
162  
163 =over 4
164  
165 =item CacheModule
166  
167 The module to use for the cache implementation (e.g. Cache::MemoryCache)
168  
169 =item CacheModuleOptions
170  
171 A hashref of options to pass to the cache module's constructor
172  
173 =item Grace
174  
175 How long to defer statting the file (in seconds).
176 This option is only any use with memory caches for two reasons:
177  
178 First, for filesystem caches, the cost of fetching an item from the cache far exceeds the cost of statting a file.
179  
180 Second, unlike the file modification time which is stored within the cache itself, the history of
181 when files were last statted is held in memory, so that the cache does not need to be reserialised on
182 each stat. Therefore the stat history is not shared between processes.
183  
184 Be careful if you use this in modperl environments as it will result in some children having a
185 new version of the cached item, and some still having the old version.
186  
187 =item NoStat
188  
189 Don't stat files to validate the cache - items are served from the cache until they are purged.
190 Valid values are 0|1 (default=0, i.e. files are statted)
191  
192 Setting this to 1 is equivalent to setting Grace to an infinite value.
193  
194 =back
195  
196 =head1 VERSION
197  
198 $Revision: 1.9 $ on $Date: 2005/10/31 21:10:41 $ by $Author: johna $
199  
200 =head1 AUTHOR
201  
202 John Alden <cpan _at_ bbc _dot_ co _dot_ uk>
203  
204 =head1 COPYRIGHT
205  
206 (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
207  
208 See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
209  
210 =cut