Mojo/Cache.pm

filter: Statementstmt 100% 14 / 14 Branchbran 83.3% 5 / 6 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 3 / 3 Podpod 100% 2 / 2 totaltotal 97.0% 33 / 34 CC 7 SCAR 15.2
CC
7
Cov
96%
set
6.0
get
2.0
BEGIN
1.0
CRAP 4.6
« Mojolicious/Command/eval.pm ↑ Summary Mojo/UserAgent/Server.pm »
1 package Mojo::Cache;
2 22 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 22 has 'max_keys' => 100;
5  
6 288 ▶sub get { (shift->{cache} // {})->{shift()} }
Subroutine get: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"cache"} // {}
l!l
24741
MC/DC100%1/1
+(shift())->{"cache"} // {}
+(shift())->{"cache"}
Truth table100%2/2
A
A= +(shift())->{"cache"}
Aresult
11
00
7  
8 157 ▶sub set {
Subroutine set: called CC=6 SCAR=17.9
Pod documented
9 157 my ($self, $key, $value) = @_;
10  
11 157 ▶ return $self unless (my $max = $self->max_keys) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless (my $max = $self->max_keys) > 0 9 148 157
12  
13 148 ▶ my $cache = $self->{cache} //= {};
Condition100%2/2
$self->{"cache"} //= {}
l!l
11929
MC/DC100%1/1
$self->{"cache"} //= {}
$self->{"cache"}
Truth table100%2/2
A
A= $self->{"cache"}
Aresult
11
00
14 148 ▶ my $queue = $self->{queue} //= [];
Condition100%2/2
$self->{"queue"} //= []
l!l
11929
MC/DC100%1/1
$self->{"queue"} //= []
$self->{"queue"}
Truth table100%2/2
A
A= $self->{"queue"}
Aresult
11
00
15 148 ▶ delete $cache->{shift @$queue} while @$queue >= $max;
Branch: 1 branch
BranchTrueFalseTotal
@$queue >= $max and do { delete $$cache{shift @$queue} } 6 148 154
16 148 ▶ push @$queue, $key unless exists $cache->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cache->{$key} 148 0 148
17 148 $cache->{$key} = $value;
18  
19 148 return $self;
20 }
21  
22 22 1;
23  
24 =encoding utf8
25  
26 =head1 NAME
27  
28 Mojo::Cache - Naive in-memory cache
29  
30 =head1 SYNOPSIS
31  
32 use Mojo::Cache;
33  
34 my $cache = Mojo::Cache->new(max_keys => 50);
35 $cache->set(foo => 'bar');
36 my $foo = $cache->get('foo');
37  
38 =head1 DESCRIPTION
39  
40 L<Mojo::Cache> is a naive in-memory cache with size limits.
41  
42 =head1 ATTRIBUTES
43  
44 L<Mojo::Cache> implements the following attributes.
45  
46 =head2 max_keys
47  
48 my $max = $cache->max_keys;
49 $cache = $cache->max_keys(50);
50  
51 Maximum number of cache keys, defaults to C<100>. Setting the value to C<0> will disable caching.
52  
53 =head1 METHODS
54  
55 L<Mojo::Cache> inherits all methods from L<Mojo::Base> and implements the following new ones.
56  
57 =head2 get
58  
59 my $value = $cache->get('foo');
60  
61 Get cached value.
62  
63 =head2 set
64  
65 $cache = $cache->set(foo => 'bar');
66  
67 Set cached value.
68  
69 =head1 SEE ALSO
70  
71 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
72  
73 =cut
« Mojolicious/Command/eval.pm ↑ Summary Mojo/UserAgent/Server.pm »