lib/Cache/AgainstFile/Base.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 20.0% 1 / 5 totaltotal 93.9% 62 / 66 CC 8 SCAR 14.2
CC
8
Cov
100%
purge
7.0
new
2.0
BEGIN
1.0
CRAP 4.1
« lib/Cache/AgainstFile.pm ↑ Summary lib/Cache/AgainstFile/Null.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $VERSION = sprintf"%d.%03d", q$Revision: 1.8 $ =~ /: (\d+)\.(\d+)/;
13  
14 13 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
15 13 my ($class, $loader, $options) = @_;
16
17 13 my $self = {
18 'loader' => $loader,
19 'options' => $options,
20 };
21  
22 13 ▶ TRACE("Cache: Stat disabled") if($self->{options}->{NoStat});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"NoStat"} 2 11 13
23 13 return bless $self, $class;
24 }
25  
26 21 ▶sub purge {
Subroutine purge: called CC=7 SCAR=19.5
Pod undocumented
27 21 my($self, $all) = @_;
28 21 my @keys;
29 21 my $accessed = $self->_accessed();
30 21 ▶ if($all)
Branch: 1 branch
BranchTrueFalseTotal
if ($all) { } 14 7 21
31 {
32 14 TRACE("Purging all keys in cache");
33 14 @keys = keys %$accessed;
34 }
35 else
36 {
37 #Identify items to delete
38 7 ▶ if($self->{options}->{NoStat})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"NoStat"}) 2 5 7
39 {
40 2 TRACE("purging stale items");
41 2 push @keys, $self->_stale(); #Stale items
42 }
43 7 ▶ if(defined $self->{options}->{MaxATime})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"MaxATime"}) 3 4 7
44 {
45 3 my $max = $self->{options}->{MaxATime}; #seconds
46 3 TRACE("purging files older than $max");
47 3 DUMP($accessed);
48 3 push @keys, grep {time - $accessed->{$_} > $max} keys %$accessed; #Inactive items
49 }
50 7 ▶ if($self->{options}->{MaxItems})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"MaxItems"}) 2 5 7
51 {
52 2 my $max = $self->{options}->{MaxItems};
53 2 TRACE("keeping $max youngest files");
54 2 my @agelist = sort {$accessed->{$a} <=> $accessed->{$b}} keys %$accessed; #sort by age
55 2 ▶ while(scalar(@agelist) > $max)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @agelist > $max) 10 2 12
56 {
57 10 push @keys, shift @agelist; #keys of the oldest ones
58 }
59 }
60
61 #Remove duplicates
62 7 my %unique = map {$_ => undef} @keys;
63 7 @keys = keys %unique;
64 }
65 21 DUMP("keys to purge", \@keys);
66 21 ▶ $self->_remove(\@keys) if(@keys);
Branch: 1 branch
BranchTrueFalseTotal
if @keys 13 8 21
67 }
68  
69 14 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
70 14 my $self = shift;
71 14 return $self->purge(1);
72 }
73  
74 23 ▶sub TRACE {}
Subroutine TRACE: called CC=1 SCAR=0.0
Pod undocumented
75 24 ▶sub DUMP {}
Subroutine DUMP: called CC=1 SCAR=0.0
Pod undocumented
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 <cpan _at_ bbc _dot_ co _dot_ uk>
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
« lib/Cache/AgainstFile.pm ↑ Summary lib/Cache/AgainstFile/Null.pm »