Suffit/Cache.pm

filter: Statementstmt 82.8% 63 / 76 Branchbran 67.6% 23 / 34 Conditioncond 69.2% 18 / 26 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 69.2% 9 / 13 Podpod 100% 9 / 9 totaltotal 74.4% 128 / 172 CC 32 SCAR 22.6
CC
32
Cov
76%
_dequeue
30.0
set
10.0
purge
8.7
CRAP 9.6
« Suffit/Util.pm ↑ Summary Suffit/Const.pm »
1 package WWW::Suffit::Cache;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf8
6  
7 =head1 NAME
8  
9 WWW::Suffit::Cache - Simple in-memory cache for WWW::Suffit web-servers
10  
11 =head1 SYNOPSIS
12  
13 use WWW::Suffit::Cache;
14  
15 my $cache = WWW::Suffit::Cache->new(
16 max_keys => 100,
17 expiration => 60,
18 );
19  
20 $cache->set(foo => 'bar');
21 $cache->set(foo => 'bar', 60);
22  
23 my $foo = $cache->get('foo');
24  
25 =head1 DESCRIPTION
26  
27 Simple in-memory cache for WWW::Suffit web-servers with size limits and expirations
28  
29 This module based on L<Mojo::Cache> and L<Cache::Memory::Simple>
30  
31 =head1 ATTRIBUTES
32  
33 This class implements the following attributes
34  
35 =head2 max_keys
36  
37 my $max = $cache->max_keys;
38 $cache = $cache->max_keys(100);
39  
40 Maximum number of cache keys. Setting the value to 0 or undef will disable caching by number of cache keys
41  
42 =head2 expiration
43  
44 my $exp = $cache->expiration;
45 $cache = $cache->expiration(60);
46  
47 This attribute performs sets or gets the default expiration seconds of live of cache record.
48 Default is 0 -- disable
49  
50 =head1 METHODS
51  
52 This class inherits all methods from L<Mojo::Base> and implements the following new ones
53  
54 =head2 clean
55  
56 $cache = $cache->clean;
57  
58 Remove all data from cache
59  
60 =head2 cleanup
61  
62 $cache = $cache->cleanup;
63  
64 Alias for L</clean>
65  
66 =head2 count
67  
68 my $count = $cache->count;
69  
70 Get actual number of cache records
71  
72 =head2 del
73  
74 $cache = $cache->del('foo');
75  
76 Alias for L</remove>
77  
78 =head2 get
79  
80 my $value = $cache->get('foo');
81  
82 Get cached value
83  
84 =head2 purge
85  
86 $cache = $cache->purge;
87  
88 Purge expired data
89  
90 This module does not purge expired data automatically. You need to call this method if you need
91  
92 =head2 remove
93  
94 $cache = $cache->remove('foo');
95  
96 Delete key from cache
97  
98 =head2 rm
99  
100 $cache = $cache->rm('foo');
101  
102 Alias for L</remove>
103  
104 =head2 set
105  
106 $cache = $cache->set(foo => 'bar');
107 $cache = $cache->set(foo => 'bar', 60);
108  
109 Set cached value with/without expiration time
110  
111 =head1 HISTORY
112  
113 See C<Changes> file
114  
115 =head1 TO DO
116  
117 See C<TODO> file
118  
119 =head1 SEE ALSO
120  
121 L<Mojolicious>, L<Mojo::Cache>, L<Cache::Memory::Simple>, L<Cache::Redis>
122  
123 =head1 AUTHOR
124  
125 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
126  
127 =head1 COPYRIGHT
128  
129 Copyright (C) 1998-2026 D&D Corporation
130  
131 =head1 LICENSE
132  
133 This program is distributed under the terms of the Artistic License Version 2.0
134  
135 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
136  
137 =cut
138  
139 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
140  
141 1 has 'max_keys' => 0;
142 1 has 'expiration' => 0;
143  
144 19 ▶sub get {
Subroutine get: called CC=8 SCAR=20.8
Pod documented
145 19 my $self = shift;
146 19 my $key = shift;
147 19 ▶ my $rec = ($self->{cache} // {})->{$key};
Condition100%2/2
$self->{"cache"} // {}
l!l
181
MC/DC100%1/1
$self->{"cache"} // {}
$self->{"cache"}
Truth table100%2/2
A
A= $self->{"cache"}
Aresult
11
00
148 19 ▶ return undef unless defined $rec; # Skip if undefined
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 5 14 19
149 14 my $exp = $rec->[0];
150 14 my $val = $rec->[1];
151 14 ▶ my $max = $self->max_keys || 0;
Condition100%2/2
$self->max_keys || 0
l!l
95
MC/DC100%1/1
$self->max_keys || 0
$self->max_keys
Truth table100%2/2
A
A= $self->max_keys
Aresult
11
00
152 14 ▶ $max = 0 if $max < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 0 0 14 14
153 14 ▶ return $val unless $exp; # If no exp found then need just to return value
Branch: 1 branch
BranchTrueFalseTotal
unless $exp 11 3 14
154 3 ▶ return $val if $exp > time; # Return value if not expired
Branch: 1 branch
BranchTrueFalseTotal
if $exp > time 1 2 3
155 2 delete $self->{cache}->{$key}; # Remove expired data from cache
156 2 ▶ $self->_dequeue($key) if $max; # Remove from queue
Branch: 1 branch
BranchTrueFalseTotal
if $max 0 2 2
157 2 return undef;
158 }
159 12 ▶sub set {
Subroutine set: called CC=10 SCAR=23.1
Pod documented
160 12 my $self = shift;
161 12 my $key = shift;
162 12 my $val = shift;
163 12 ▶ my $exp = shift // $self->expiration;
Condition66%2/3
shift() // $self->expiration
l!l&&r!l&&!r
570
MC/DC0%0/2
shift() // $self->expiration
shift() $self->expiration
Truth table66%2/3
A // B
A= shift()B= $self->expiration
ABresult
1X1
011
000
164 12 ▶ my $max = $self->max_keys || 0;
Condition100%2/2
$self->max_keys || 0
l!l
75
MC/DC100%1/1
$self->max_keys || 0
$self->max_keys
Truth table100%2/2
A
A= $self->max_keys
Aresult
11
00
165 12 ▶ $max = 0 if $max < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 0 0 12 12
166 12 ▶ my $cache = $self->{cache} //= {};
Condition100%2/2
$self->{"cache"} //= {}
l!l
111
MC/DC100%1/1
$self->{"cache"} //= {}
$self->{"cache"}
Truth table100%2/2
A
A= $self->{"cache"}
Aresult
11
00
167 12 ▶ my $queue = $self->{queue} //= [];
Condition100%2/2
$self->{"queue"} //= []
l!l
111
MC/DC100%1/1
$self->{"queue"} //= []
$self->{"queue"}
Truth table100%2/2
A
A= $self->{"queue"}
Aresult
11
00
168 12 ▶ if ($max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max) 7 5 12
169 7 ▶ delete $cache->{shift @$queue} while @$queue >= $max; # Remove first cache-records
Branch: 1 branch
BranchTrueFalseTotal
@$queue >= $max and do { delete $$cache{shift @$queue} } 5 7 12
170 7 ▶ push @$queue, $key unless exists $cache->{$key}; # Add key of cache-record to queue (enqueue) if it yet not exists
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cache->{$key} 6 1 7
171 }
172 12 ▶ $cache->{$key} = [$exp ? ($exp + time) : undef, $val]; # Sets the new or updates the existed cache-record
Branch: 1 branch
BranchTrueFalseTotal
$exp ? : 3 9 12
173 12 return $self;
174 }
175 2 ▶sub remove {
Subroutine remove: called CC=5 SCAR=16.8
Pod documented
176 2 my $self = shift;
177 2 my $key = shift;
178 2 ▶ my $cache = $self->{cache} //= {};
Condition50%1/2
$self->{"cache"} //= {}
l!l
20
MC/DC0%0/1
$self->{"cache"} //= {}
$self->{"cache"}
Truth table50%1/2
A
A= $self->{"cache"}
Aresult
11
00
179 2 delete $cache->{$key};
180 2 ▶ my $max = $self->max_keys || 0;
Condition50%1/2
$self->max_keys || 0
l!l
02
MC/DC0%0/1
$self->max_keys || 0
$self->max_keys
Truth table50%1/2
A
A= $self->max_keys
Aresult
11
00
181 2 ▶ $max = 0 if $max < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 0 0 2 2
182 2 ▶ $self->_dequeue($key) if $max; # Remove from queue
Branch: 1 branch
BranchTrueFalseTotal
if $max 0 2 2
183 2 return $self;
184 }
185 0 ▶sub rm { goto &remove } # alias
Subroutine rm: not called CC=1 SCAR=6.9
Pod documented
186 0 ▶sub del { goto &remove } # alias
Subroutine del: not called CC=1 SCAR=6.9
Pod documented
187 5 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
188 5 my $self = shift;
189 5 return 1 * keys %{$self->{cache}};
190 }
191 1 ▶sub purge {
Subroutine purge: called CC=8 SCAR=21.6
Pod documented
192 1 my $self = shift;
193 1 ▶ my $cache = $self->{cache} //= {};
Condition50%1/2
$self->{"cache"} //= {}
l!l
10
MC/DC0%0/1
$self->{"cache"} //= {}
$self->{"cache"}
Truth table50%1/2
A
A= $self->{"cache"}
Aresult
11
00
194 1 ▶ my $max = $self->max_keys || 0;
Condition50%1/2
$self->max_keys || 0
l!l
01
MC/DC0%0/1
$self->max_keys || 0
$self->max_keys
Truth table50%1/2
A
A= $self->max_keys
Aresult
11
00
195 1 ▶ $max = 0 if $max < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 0 0 1 1
196 1 for my $key (keys %$cache) {
197 2 my $exp = $cache->{$key}->[0];
198 2 ▶ if ($exp && $exp < time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp and $exp < time) 1 1 2
Condition66%2/3
$exp and $exp < time
!ll&&!rl&&r
101
MC/DC50%1/2
$exp and $exp < time
$exp $exp < time
Truth table66%2/3
A and B
A= $expB= $exp < time
ABresult
0X0
100
111
199 1 delete $cache->{$key};
200 1 ▶ $self->_dequeue($key) if $max; # Remove from queue
Branch: 1 branch
BranchTrueFalseTotal
if $max 0 1 1
201 }
202 }
203 1 return $self;
204 }
205 1 ▶sub clean {
Subroutine clean: called CC=1 SCAR=0.0
Pod documented
206 1 my $self = shift;
207 1 $self->{cache} = {};
208 1 $self->{queue} = [];
209 1 return $self;
210 }
211 0 ▶sub cleanup { goto &clean } # alias
Subroutine cleanup: not called CC=1 SCAR=6.9
Pod documented
212  
213 0 ▶sub _dequeue {
Subroutine _dequeue: not called CC=5 SCAR=34.0
214 0 my $self = shift;
215 0 my $key = shift;
216 0 ▶ return $self unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
217 0 ▶ my $queue = $self->{queue} //= [];
Condition0%0/2
$self->{"queue"} //= []
l!l
00
MC/DC0%0/1
$self->{"queue"} //= []
$self->{"queue"}
Truth table0%0/2
A
A= $self->{"queue"}
Aresult
11
00
218 0 my $pointer = 0;
219 0 foreach my $pointer (0 .. scalar(@$queue)-1) {
220 0 ▶ if ($queue->[$pointer] eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue->[$pointer] eq $key) 0 0 0
221 0 splice(@$queue, $pointer, 1);
222 0 last;
223 }
224 }
225 0 return $self;
226 }
227  
228 1 1;
229  
230 __END__
« Suffit/Util.pm ↑ Summary Suffit/Const.pm »