File Coverage

lib/Cache/AgainstFile/Base.pm
Criterion Covered Total %
statement 41 41 100.0
branch 12 12 100.0
condition n/a
subroutine 7 7 100.0
pod 1 5 20.0
total 61 65 93.8


line stmt bran cond sub pod time code
1             ###############################################################################
2             # Purpose : Base class for default Cache::AgainstFile implementations
3             # Author : John Alden
4             # Created : 24th April 2004
5             # CVS : $Id: Base.pm,v 1.8 2005/06/03 14:29:55 johna Exp $
6             ###############################################################################
7              
8             package Cache::AgainstFile::Base;
9              
10 3     3   15 use strict;
  3         7  
  3         93  
11 3     3   12 use vars qw($VERSION);
  3         7  
  3         1491  
12             $VERSION = sprintf"%d.%03d", q$Revision: 1.8 $ =~ /: (\d+)\.(\d+)/;
13              
14             sub new {
15 13     13 1 27 my ($class, $loader, $options) = @_;
16            
17 13         48 my $self = {
18             'loader' => $loader,
19             'options' => $options,
20             };
21              
22 13 100       71 TRACE("Cache: Stat disabled") if($self->{options}->{NoStat});
23 13         60 return bless $self, $class;
24             }
25              
26             sub purge {
27 21     21 0 47 my($self, $all) = @_;
28 21         35 my @keys;
29 21         83 my $accessed = $self->_accessed();
30 21 100       54 if($all)
31             {
32 14         36 TRACE("Purging all keys in cache");
33 14         47 @keys = keys %$accessed;
34             }
35             else
36             {
37             #Identify items to delete
38 7 100       39 if($self->{options}->{NoStat})
39             {
40 2         7 TRACE("purging stale items");
41 2         8 push @keys, $self->_stale(); #Stale items
42             }
43 7 100       37 if(defined $self->{options}->{MaxATime})
44             {
45 3         9 my $max = $self->{options}->{MaxATime}; #seconds
46 3         42 TRACE("purging files older than $max");
47 3         20 DUMP($accessed);
48 3         14 push @keys, grep {time - $accessed->{$_} > $max} keys %$accessed; #Inactive items
  11         36  
49             }
50 7 100       33 if($self->{options}->{MaxItems})
51             {
52 2         6 my $max = $self->{options}->{MaxItems};
53 2         16 TRACE("keeping $max youngest files");
54 2         24 my @agelist = sort {$accessed->{$a} <=> $accessed->{$b}} keys %$accessed; #sort by age
  31         60  
55 2         16 while(scalar(@agelist) > $max)
56             {
57 10         39 push @keys, shift @agelist; #keys of the oldest ones
58             }
59             }
60            
61             #Remove duplicates
62 7         15 my %unique = map {$_ => undef} @keys;
  17         54  
63 7         35 @keys = keys %unique;
64             }
65 21         64 DUMP("keys to purge", \@keys);
66 21 100       115 $self->_remove(\@keys) if(@keys);
67             }
68              
69             sub clear {
70 14     14 0 18 my $self = shift;
71 14         51 return $self->purge(1);
72             }
73              
74 23     23 0 40 sub TRACE {}
75 24     24 0 31 sub DUMP {}
76              
77             =head1 NAME
78              
79             Cache::AgainstFile::Base - base class for default backends
80              
81             =head1 SYNOPSIS
82              
83             package Cache::AgainstFile::MyBackend;
84              
85             use Cache::AgainstFile::Base;
86             @ISA = qw(Cache::AgainstFile::Base);
87            
88             ...implement methods...
89              
90             1;
91              
92              
93             =head1 DESCRIPTION
94              
95             This provides a default implementation for purging the cache, based on a list of stale files
96             and a hashref of access times.
97              
98             Classes inheriting from this base class should provide the following public methods:
99              
100             =over 4
101              
102             =item $b = new Cache::AgainstFile::MyBackend(\&loader, \%options)
103              
104             This should call the base class constructor.
105              
106             =item $data = $b->get($filename, @opts)
107              
108             Fetch an item from the cache.
109             @opts should be passed after the filename to the loader coderef.
110              
111             =item $n = $b->count()
112              
113             Number of items in the cache
114              
115             =item $bytes = $b->size()
116              
117             Total size of the cache in bytes
118              
119             =back
120              
121             They should also provide the following protected methods which are used to support purge():
122              
123             =over 4
124              
125             =item $b->_remove(\@filenames)
126              
127             Remove a number of items from the cache
128              
129             =item $hashref = $b->_accessed()
130              
131             A hashref of filename => access time
132              
133             =item @filenames = $b->_stale()
134              
135             A list of cache items which are stale with respect to their original files
136              
137             =back
138              
139             =head1 OPTIONS
140              
141             The implementation of purge() supports the options:
142              
143             =over 4
144              
145             =item NoStat
146              
147             =item MaxItems
148              
149             =item MaxATime
150              
151             =back
152              
153             =head1 VERSION
154              
155             $Revision: 1.8 $ on $Date: 2005/06/03 14:29:55 $ by $Author: johna $
156              
157             =head1 AUTHOR
158              
159             John Alden
160              
161             =head1 COPYRIGHT
162              
163             (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
164              
165             See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
166              
167             =cut