File Coverage

blib/lib/Slovo/Cache.pm
Criterion Covered Total %
statement 18 18 100.0
branch 3 4 75.0
condition 5 8 62.5
subroutine 4 4 100.0
pod 3 3 100.0
total 33 37 89.1


line stmt bran cond sub pod time code
1             package Slovo::Cache;
2 15     15   264106 use Mojo::Base 'Mojo::Cache';
  15         44  
  15         105  
3              
4             has cache => sub { {} };
5             has key_prefix => '';
6             has max_keys => 111;
7              
8             sub new {
9 19     19 1 11000 my $self = shift->SUPER::new(@_);
10 19   100     281 $self->{key_prefix} //= '';
11 19   50     120 $self->{cache} ||= {};
12 19   50     139 $self->{queue} ||= [];
13 19         72 return $self;
14             }
15              
16             ## no critic qw(RequireArgUnpacking RequireFinalReturn)
17 467   50 467 1 881531 sub get { $_[0]->{cache}{$_[0]->{key_prefix} . ($_[1] // '')} }
18              
19             ## no critic qw(ProhibitAmbiguousNames)
20             sub set {
21 111     111 1 65939 my ($self, $key, $value) = @_;
22 111         370 $key = $_[0]->{key_prefix} . $key;
23 111 100       395 return $self if not((my $max = $self->max_keys) > 0);
24              
25 109         791 my $cache = $self->{cache};
26 109         229 my $queue = $self->{queue};
27 109         354 delete $cache->{shift @$queue} while @$queue >= $max;
28 109 50       465 push @$queue, $key unless exists $cache->{$key};
29 109         388 $cache->{$key} = $value;
30              
31 109         348 return $self;
32             }
33              
34             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 is a naive in-memory cache with size limits and key prefixes.
53             It is a modification of L. 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 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 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 inherits all methods from L 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,
92             L, L, L.
93              
94             =cut