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