| 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"} // {}
MC/DC100%1/1
+(shift())->{"cache"} // {}
+(shift())->{"cache"}
Truth table100%2/2
A
A= +(shift())->{"cache"}
| |||||||||||||
| 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
| |||||||||||||
| 12 | |||||||||||||
| 13 | 148 | ▶ | my $cache = $self->{cache} //= {}; | ||||||||||
|
Condition100%2/2
$self->{"cache"} //= {}
MC/DC100%1/1
$self->{"cache"} //= {}
$self->{"cache"}
Truth table100%2/2
A
A= $self->{"cache"}
| |||||||||||||
| 14 | 148 | ▶ | my $queue = $self->{queue} //= []; | ||||||||||
|
Condition100%2/2
$self->{"queue"} //= []
MC/DC100%1/1
$self->{"queue"} //= []
$self->{"queue"}
Truth table100%2/2
A
A= $self->{"queue"}
| |||||||||||||
| 15 | 148 | ▶ | delete $cache->{shift @$queue} while @$queue >= $max; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | 148 | ▶ | push @$queue, $key unless exists $cache->{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | ||||||||||||