blib/lib/Cache/Simple/TimedExpiry.pm

filter: Statementstmt 96.1% 50 / 52 Branchbran 90.0% 18 / 20 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 90.9% 10 / 11 Podpod 62.5% 5 / 8 totaltotal 92.0% 93 / 101 CC 13 SCAR 10.8
CC
13
Cov
95%
expire
5.0
has_key
4.0
store
3.0
CRAP 3.0
↑ Summary
1 package Cache::Simple::TimedExpiry;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '0.27';
8  
9 =head1 NAME
10  
11 Cache::Simple::TimedExpiry
12  
13 =head2 EXAMPLE
14  
15 package main;
16  
17 use strict;
18 use warnings;
19 $,=' '; $|++;
20  
21 use Cache::Simple::TimedExpiry;
22 my $h = Cache::Simple::TimedExpiry->new;
23  
24 $h->set( DieQuick => "No duration!", 0);
25 print $h->elements;
26 do { $h->set($_,"Value of $_", 1); sleep 2;}
27 for qw(Have a nice day you little monkey);
28  
29  
30 print $h->elements; $h->dump; sleep 4; print $h->elements; $h->dump;
31  
32 print time;
33  
34  
35 =cut
36  
37  
38 # 0 - expiration delay
39 # 1 - hash
40 # 2 - expiration queue
41 # 3 - last expiration
42  
43 =head2 new
44  
45 Set up a new cache object
46  
47 =cut
48  
49  
50 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
51 1 bless [2,{},[],0], "Cache::Simple::TimedExpiry";
52 }
53  
54  
55 =head2 expire_after SECONDS
56  
57 Set the cache's expiry policy to expire entries after SECONDS seconds. Setting this changes the expiry policy for pre-existing cache entries and for new ones.
58  
59  
60 =cut
61  
62 4 ▶sub expire_after {
Subroutine expire_after: called CC=2 SCAR=6.9
Pod documented
63 4 my $self = shift;
64 4 ▶ $self->[0] = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 2 4
65 4 return ($self->[0]);
66  
67 }
68  
69  
70 =head2 has_key KEY
71  
72 Return true if the cache has an entry with the key KEY
73  
74 =cut
75  
76 19 ▶sub has_key ($$) { # exists
Subroutine has_key: called CC=4 SCAR=13.9
Pod documented
77 19 my ($self, $key) = @_;
78
79 19 my $time = time;
80 19 ▶ $self->expire($time) if ($time > $self->[3]);
Branch: 1 branch
BranchTrueFalseTotal
if $time > $self->[3] 3 16 19
81 19 ▶ return 1 if defined $key && exists $self->[1]->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and exists $self->[1]{$key} 10 9 19
Condition100%3/3
defined $key and exists $self->[1]{$key}
!ll&&!rl&&r
2710
MC/DC100%2/2
defined $key and exists $self->[1]{$key}
defined $key exists $self->[1]{$key}
Truth table100%3/3
A and B
A= defined $keyB= exists $self->[1]{$key}
ABresult
0X0
100
111
82 9 return 0;
83 }
84  
85 =head2 fetch KEY
86  
87 Return the cache entry with key KEY.
88 Returns undef if there is no such entry
89  
90 (Can also be called as L<get>)
91  
92 =cut
93  
94 1 *get = \&fetch;
95  
96 7 ▶sub fetch ($$) {
Subroutine fetch: called CC=2 SCAR=6.9
Pod documented
97 7 my ($self,$key) = @_;
98  
99 # Only expire
100 7 ▶ unless ( $self->has_key($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_key($key)) 2 5 7
101 2 return undef;
102 }
103  
104 5 return $self->[1]->{$key};
105  
106 }
107  
108 =head2 store KEY VALUE
109  
110 Store VALUE in the cache with accessor KEY. Expire it from the cache
111 at or after EXPIRYTIME.
112  
113 (Can also be called as L<set>)
114  
115 =cut
116  
117 1 *set = \&store;
118  
119 4 ▶sub store ($$$) {
Subroutine store: called CC=3 SCAR=11.0
Pod documented
120 4 my ($self,$key,$value) = @_;
121 4 my $time = time;
122 # Only expire
123 4 ▶ $self->expire($time) if ($time > $self->[3]);
Branch: 1 branch
BranchTrueFalseTotal
if $time > $self->[3] 1 3 4
124  
125 4 ▶ return undef unless defined ($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 4 4
126 4 $self->[1]->{$key} = $value;
127  
128 4 push @{$self->[2]}, [ time, $key ];
129 }
130  
131 4 ▶sub expire ($$) {
Subroutine expire: called CC=5 SCAR=16.1
Pod undocumented
132 4 my $self = shift;
133 4 my $time = shift;
134
135 4 $self->[3] = $time;
136  
137 4 my $oldest_nonexpired_entry = ($time - $self->[0]);
138
139  
140 4 ▶ return unless defined $self->[2]->[0]; # do we have an element in the array?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[2][0] 1 3 4
141  
142  
143 3 ▶ return unless $self->[2]->[0]->[0] < $oldest_nonexpired_entry; # is it expired?
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[2][0][0] < $oldest_nonexpired_entry 1 2 3
144  
145 2 ▶ while ( @{$self->[2]} && $self->[2]->[0]->[0] <$oldest_nonexpired_entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->[2];} and $self->[2][0][0] < $oldest_nonexpired_entry) 2 2 4
Condition100%3/3
@{$self->[2];} and $self->[2][0][0] < $oldest_nonexpired_entry
!ll&&!rl&&r
112
MC/DC100%2/2
@{$self->[2];} and $self->[2][0][0] < $oldest_nonexpired_entry
@{$self->[2];} $self->[2][0][0] < $oldest_nonexpired_entry
Truth table100%3/3
A and B
A= @{$self->[2];}B= $self->[2][0][0] < $oldest_nonexpired_entry
ABresult
0X0
100
111
146 2 my $key = $self->[2]->[0]->[1];
147 2 delete $self->[1]->{ $key };
148 2 shift @{$self->[2]};
149 }
150  
151 }
152  
153 4 ▶sub elements ($) { # keys
Subroutine elements: called CC=2 SCAR=7.0
Pod undocumented
154 4 my $self = shift;
155 4 my $time = time;
156 # Only expire
157 4 ▶ $self->expire($time) if ($time > $self->[3]);
Branch: 1 branch
BranchTrueFalseTotal
if $time > $self->[3] 0 4 4
158  
159 4 return keys %{$self->[1]};
160  
161 }
162  
163 0 ▶sub dump ($) {
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
164 0 require Data::Dumper;
165 0 print Data::Dumper::Dumper($_[0]);
166 }
167  
168  
169  
170 =head1 AUTHOR
171  
172 Jesse Vincent <jesse@bestpractical.com>
173 Some of the heavy lifting was designed by Robert Spier <rspier@pobox.com>
174  
175 Copyright 2004 Jesse Vincent <jesse@bestpractical.com>
176  
177 =cut
178  
179 1 1;
↑ Summary