line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Cache; |
2
|
53
|
|
|
53
|
|
66119
|
use Mojo::Base -base; |
|
53
|
|
|
|
|
157
|
|
|
53
|
|
|
|
|
414
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
has 'max_keys' => 100; |
5
|
|
|
|
|
|
|
|
6
|
1502
|
|
100
|
1502
|
1
|
8277
|
sub get { (shift->{cache} // {})->{shift()} } |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub set { |
9
|
843
|
|
|
843
|
1
|
2220
|
my ($self, $key, $value) = @_; |
10
|
|
|
|
|
|
|
|
11
|
843
|
100
|
|
|
|
2373
|
return $self unless (my $max = $self->max_keys) > 0; |
12
|
|
|
|
|
|
|
|
13
|
578
|
|
100
|
|
|
2355
|
my $cache = $self->{cache} //= {}; |
14
|
578
|
|
100
|
|
|
2094
|
my $queue = $self->{queue} //= []; |
15
|
578
|
|
|
|
|
1856
|
delete $cache->{shift @$queue} while @$queue >= $max; |
16
|
578
|
50
|
|
|
|
2191
|
push @$queue, $key unless exists $cache->{$key}; |
17
|
578
|
|
|
|
|
2013
|
$cache->{$key} = $value; |
18
|
|
|
|
|
|
|
|
19
|
578
|
|
|
|
|
1696
|
return $self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
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 is a naive in-memory cache with size limits. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
L 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 inherits all methods from L 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, L, L. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |