line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Role::Cache::LRU; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
8138
|
use namespace::clean; |
|
1
|
|
|
|
|
9173
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
570
|
use strictures 2; |
|
1
|
|
|
|
|
1309
|
|
|
1
|
|
|
|
|
31
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
501
|
use Cache::LRU; |
|
1
|
|
|
|
|
530
|
|
|
1
|
|
|
|
|
74
|
|
7
|
1
|
|
|
1
|
|
7
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
8
|
1
|
|
|
1
|
|
5
|
use Moo::Role; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
9
|
1
|
|
|
1
|
|
736
|
use Sub::Quote qw(quote_sub); |
|
1
|
|
|
|
|
3850
|
|
|
1
|
|
|
|
|
49
|
|
10
|
1
|
|
|
1
|
|
467
|
use Types::Standard qw(InstanceOf); |
|
1
|
|
|
|
|
68879
|
|
|
1
|
|
|
|
|
10
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has cache => ( |
15
|
|
|
|
|
|
|
isa => InstanceOf['Cache::LRU'], |
16
|
|
|
|
|
|
|
is => 'lazy', |
17
|
|
|
|
|
|
|
builder => quote_sub(q{ Cache::LRU->new }), |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub get_cache { |
21
|
2
|
|
|
2
|
1
|
8057
|
my ($self, $key) = @_; |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
|
|
36
|
return $self->cache->get($key); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub set_cache { |
27
|
1
|
|
|
1
|
1
|
696
|
my ($self, $key, $data) = @_; |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
23
|
return $self->cache->set($key, $data); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub get_cache_size { |
33
|
2
|
|
|
2
|
1
|
497
|
my $self = shift; |
34
|
|
|
|
|
|
|
|
35
|
2
|
|
|
|
|
35
|
return $self->cache->{size}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub set_cache_size { |
39
|
3
|
|
|
3
|
1
|
1550
|
my ($self, $max) = @_; |
40
|
|
|
|
|
|
|
|
41
|
3
|
100
|
|
|
|
27
|
croak q|Invalid cache size!| if ($max <= 0); |
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
|
|
21
|
return $self->cache->{size} = $max; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
1; |
47
|
|
|
|
|
|
|
__END__ |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=encoding utf-8 |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=for stopwords lru |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 NAME |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Role::Cache::LRU - LRU caching role for Moo class. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 SYNOPSIS |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
package MyPackage; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
use Moo; |
62
|
|
|
|
|
|
|
with 'Role::Cache::LRU'; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $mp = MyPackage->new; |
65
|
|
|
|
|
|
|
$mp->set_cache('foo', {bar => 1}); |
66
|
|
|
|
|
|
|
$mp->get_cache('foo'); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 DESCRIPTION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Role::Cache::LRU is a Moo's role that provides LRU caching based on |
71
|
|
|
|
|
|
|
L<Cache::LRU|Cache::LRU>. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 DEVELOPMENT |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Source repository at L<https://github.com/kianmeng/role-cache-lru|https://github.com/kianmeng/role-cache-lru>. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
How to contribute? Follow through the L<CONTRIBUTING.md|https://github.com/kianmeng/role-cache-lru/blob/master/CONTRIBUTING.md> document to setup your development environment. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 METHODS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 set_cache($key, $item) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Add a cache item to the cache. The $key must be a string. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $mp = MyPackage->new; |
86
|
|
|
|
|
|
|
$mp->set_cache('foo', {bar => 1}); |
87
|
|
|
|
|
|
|
$mp->set_cache('bar', [1, 2, 3]); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 get_cache($key) |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Get a cached item based on the $key. If nothing is found, returns undef. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $mp = MyPackage->new; |
94
|
|
|
|
|
|
|
my $item = $mp->get_cache('fishball'); |
95
|
|
|
|
|
|
|
print $item; # undef |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 set_cache_size($max) |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Set the maximum cached size. The $max value must be larger or equal to 1. |
100
|
|
|
|
|
|
|
Adjust this to your available maximum memory in your script. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $mp = MyPackage->new; |
103
|
|
|
|
|
|
|
$mp->set_cache_size(4096); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 get_cache_size() |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Get the maximum cache size. The default maximum value is 1024. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
my $mp = MyPackage->new; |
110
|
|
|
|
|
|
|
print $mp->get_cache_size(); |
111
|
|
|
|
|
|
|
# 1024 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This software is Copyright (c) 2019 Kian Meng, Ang. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This is free software, licensed under: |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 AUTHOR |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Kian Meng, Ang E<lt>kianmeng@users.noreply.github.comE<gt> |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 SEE ALSO |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
L<Cache::LRU|Cache::LRU> |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |