line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Slovo::Cache; |
2
|
15
|
|
|
15
|
|
219957
|
use Mojo::Base 'Mojo::Cache'; |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
84
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
has cache => sub { {} }; |
5
|
|
|
|
|
|
|
has key_prefix => ''; |
6
|
|
|
|
|
|
|
has max_keys => 111; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
19
|
|
|
19
|
1
|
7460
|
my $self = shift->SUPER::new(@_); |
10
|
19
|
|
100
|
|
|
239
|
$self->{key_prefix} //= ''; |
11
|
19
|
|
50
|
|
|
116
|
$self->{cache} ||= {}; |
12
|
19
|
|
50
|
|
|
105
|
$self->{queue} ||= []; |
13
|
19
|
|
|
|
|
47
|
return $self; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
## no critic qw(RequireArgUnpacking RequireFinalReturn) |
17
|
413
|
|
50
|
413
|
1
|
700231
|
sub get { $_[0]->{cache}{$_[0]->{key_prefix} . ($_[1] // '')} } |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
## no critic qw(ProhibitAmbiguousNames) |
20
|
|
|
|
|
|
|
sub set { |
21
|
104
|
|
|
104
|
1
|
46108
|
my ($self, $key, $value) = @_; |
22
|
104
|
|
|
|
|
267
|
$key = $_[0]->{key_prefix} . $key; |
23
|
104
|
100
|
|
|
|
317
|
return $self if not((my $max = $self->max_keys) > 0); |
24
|
|
|
|
|
|
|
|
25
|
102
|
|
|
|
|
597
|
my $cache = $self->{cache}; |
26
|
102
|
|
|
|
|
168
|
my $queue = $self->{queue}; |
27
|
102
|
|
|
|
|
278
|
delete $cache->{shift @$queue} while @$queue >= $max; |
28
|
102
|
50
|
|
|
|
333
|
push @$queue, $key unless exists $cache->{$key}; |
29
|
102
|
|
|
|
|
249
|
$cache->{$key} = $value; |
30
|
|
|
|
|
|
|
|
31
|
102
|
|
|
|
|
247
|
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 |