line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Cache; |
2
|
52
|
|
|
52
|
|
64992
|
use Mojo::Base -base; |
|
52
|
|
|
|
|
135
|
|
|
52
|
|
|
|
|
414
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
has 'max_keys' => 100; |
5
|
|
|
|
|
|
|
|
6
|
1481
|
|
100
|
1481
|
1
|
8566
|
sub get { (shift->{cache} // {})->{shift()} } |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub set { |
9
|
835
|
|
|
835
|
1
|
2276
|
my ($self, $key, $value) = @_; |
10
|
|
|
|
|
|
|
|
11
|
835
|
100
|
|
|
|
3673
|
return $self unless (my $max = $self->max_keys) > 0; |
12
|
|
|
|
|
|
|
|
13
|
570
|
|
100
|
|
|
2199
|
my $cache = $self->{cache} //= {}; |
14
|
570
|
|
100
|
|
|
1961
|
my $queue = $self->{queue} //= []; |
15
|
570
|
|
|
|
|
1976
|
delete $cache->{shift @$queue} while @$queue >= $max; |
16
|
570
|
50
|
|
|
|
2202
|
push @$queue, $key unless exists $cache->{$key}; |
17
|
570
|
|
|
|
|
1929
|
$cache->{$key} = $value; |
18
|
|
|
|
|
|
|
|
19
|
570
|
|
|
|
|
1602
|
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 |