| 1 | package Slovo::Cache; | ||||||||||||
| 2 | 16 | ▶ | use Mojo::Base 'Mojo::Cache'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 0 | ▶ | has cache => sub { {} }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 5 | 16 | has key_prefix => ''; | |||||||||||
| 6 | 16 | has max_keys => 111; | |||||||||||
| 7 | |||||||||||||
| 8 | 20 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||
| 9 | 20 | my $self = shift->SUPER::new(@_); | |||||||||||
| 10 | 20 | ▶ | $self->{key_prefix} //= ''; | ||||||||||
|
Condition100%2/2
$self->{"key_prefix"} //= ""
MC/DC100%1/1
$self->{"key_prefix"} //= ""
$self->{"key_prefix"}
Truth table100%2/2
A
A= $self->{"key_prefix"}
| |||||||||||||
| 11 | 20 | ▶ | $self->{cache} ||= {}; | ||||||||||
|
Condition50%1/2
$self->{"cache"} ||= {}
MC/DC0%0/1
$self->{"cache"} ||= {}
$self->{"cache"}
Truth table50%1/2
A
A= $self->{"cache"}
| |||||||||||||
| 12 | 20 | ▶ | $self->{queue} ||= []; | ||||||||||
|
Condition50%1/2
$self->{"queue"} ||= []
MC/DC0%0/1
$self->{"queue"} ||= []
$self->{"queue"}
Truth table50%1/2
A
A= $self->{"queue"}
| |||||||||||||
| 13 | 20 | return $self; | |||||||||||
| 14 | } | ||||||||||||
| 15 | |||||||||||||
| 16 | ## no critic qw(RequireArgUnpacking RequireFinalReturn) | ||||||||||||
| 17 | 476 | ▶ | sub get { $_[0]->{cache}{$_[0]->{key_prefix} . ($_[1] // '')} } | ||||||||||
|
Subroutine
get: called CC=2 SCAR=7.6
Pod
documented
Condition50%1/2
$_[1] // ""
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||
| 18 | |||||||||||||
| 19 | ## no critic qw(ProhibitAmbiguousNames) | ||||||||||||
| 20 | 124 | ▶ | sub set { | ||||||||||
|
Subroutine
set: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 21 | 124 | my ($self, $key, $value) = @_; | |||||||||||
| 22 | 124 | $key = $_[0]->{key_prefix} . $key; | |||||||||||
| 23 | 124 | ▶ | return $self if not((my $max = $self->max_keys) > 0); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | |||||||||||||
| 25 | 122 | my $cache = $self->{cache}; | |||||||||||
| 26 | 122 | my $queue = $self->{queue}; | |||||||||||
| 27 | 122 | ▶ | delete $cache->{shift @$queue} while @$queue >= $max; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 122 | ▶ | push @$queue, $key unless exists $cache->{$key}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 122 | $cache->{$key} = $value; | |||||||||||
| 30 | |||||||||||||
| 31 | 122 | return $self; | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | 16 | 1; | |||||||||||
| 35 | |||||||||||||
| 36 | =encoding utf8 | ||||||||||||
| 37 | |||||||||||||
| 38 | =head1 NAME | ||||||||||||
| 39 | |||||||||||||
| 40 | Slovo::Cache - Naive in-memory cache | ||||||||||||
| 41 | |||||||||||||
| 42 | =head1 SYNOPSIS | ||||||||||||
| 43 | |||||||||||||
| 44 | use Slovo::Cache; | ||||||||||||
| 45 | |||||||||||||
| 46 | my $cache = Slovo::Cache->new(max_keys => 50, prefix=>'baz'); | ||||||||||||
| 47 | $cache->set(foo => 'bar'); | ||||||||||||
| 48 | my $foo = $cache->get('foo'); # bar | ||||||||||||
| 49 | |||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||
| 51 | |||||||||||||
| 52 | L<Slovo::Cache> is a naive in-memory cache with size limits and key prefixes. | ||||||||||||
| 53 | It is a modification of L<Mojo::Cache>. It can set a prefix to the keys. This | ||||||||||||
| 54 | is how we cache templates to support different themes per domain. | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 ATTRIBUTES | ||||||||||||
| 57 | |||||||||||||
| 58 | L<Slovo::Cache> implements the following attributes. | ||||||||||||
| 59 | |||||||||||||
| 60 | =head2 key_prefix | ||||||||||||
| 61 | |||||||||||||
| 62 | The prefix which will be used for each key. Defaults to empty string. It is | ||||||||||||
| 63 | reset in L<Slovo/around_dispatch> to set different namespace for cached | ||||||||||||
| 64 | templates per domain. | ||||||||||||
| 65 | |||||||||||||
| 66 | =head2 max_keys | ||||||||||||
| 67 | |||||||||||||
| 68 | my $max = $cache->max_keys; | ||||||||||||
| 69 | $cache = $cache->max_keys(50); | ||||||||||||
| 70 | |||||||||||||
| 71 | Maximum number of cache keys, defaults to C<111>. Setting the value to C<0> will disable caching. | ||||||||||||
| 72 | |||||||||||||
| 73 | =head1 METHODS | ||||||||||||
| 74 | |||||||||||||
| 75 | L<Mojo::Cache> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||||||||||
| 76 | |||||||||||||
| 77 | =head2 get | ||||||||||||
| 78 | |||||||||||||
| 79 | my $value = $cache->get('foo'); | ||||||||||||
| 80 | |||||||||||||
| 81 | Get cached value. | ||||||||||||
| 82 | |||||||||||||
| 83 | =head2 set | ||||||||||||
| 84 | |||||||||||||
| 85 | $cache = $cache->set(foo => 'bar'); | ||||||||||||
| 86 | |||||||||||||
| 87 | Set cached value. | ||||||||||||
| 88 | |||||||||||||
| 89 | =head1 SEE ALSO | ||||||||||||
| 90 | |||||||||||||
| 91 | L<Slovo/around_dispatch>, | ||||||||||||
| 92 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||
| 93 | |||||||||||||
| 94 | =cut | ||||||||||||