line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CHI::Driver::Memory; |
2
|
|
|
|
|
|
|
$CHI::Driver::Memory::VERSION = '0.60'; |
3
|
20
|
|
|
20
|
|
96
|
use Carp qw(cluck croak); |
|
20
|
|
|
|
|
25
|
|
|
20
|
|
|
|
|
1421
|
|
4
|
20
|
|
|
20
|
|
6724
|
use CHI::Constants qw(CHI_Meta_Namespace); |
|
20
|
|
|
|
|
36
|
|
|
20
|
|
|
|
|
1014
|
|
5
|
20
|
|
|
20
|
|
100
|
use Moo; |
|
20
|
|
|
|
|
26
|
|
|
20
|
|
|
|
|
123
|
|
6
|
20
|
|
|
20
|
|
20070
|
use MooX::Types::MooseLike::Base qw(:all); |
|
20
|
|
|
|
|
111912
|
|
|
20
|
|
|
|
|
7609
|
|
7
|
20
|
|
|
20
|
|
154
|
use strict; |
|
20
|
|
|
|
|
28
|
|
|
20
|
|
|
|
|
678
|
|
8
|
20
|
|
|
20
|
|
83
|
use warnings; |
|
20
|
|
|
|
|
31
|
|
|
20
|
|
|
|
|
9677
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
extends 'CHI::Driver'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our %Global_Datastore = (); ## no critic (ProhibitPackageVars) |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has 'datastore' => ( is => 'ro', isa => HashRef ); |
15
|
|
|
|
|
|
|
has 'global' => ( is => 'ro', isa => Bool ); |
16
|
|
|
|
|
|
|
|
17
|
26
|
|
|
26
|
0
|
561
|
sub default_discard_policy { 'lru' } |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# We see a lot of repeated '$self->{datastore}->{$self->{namespace}}' |
20
|
|
|
|
|
|
|
# expressions below. The reason this cannot be easily memoized in the object |
21
|
|
|
|
|
|
|
# is that we want the cache to be cleared across multiple existing CHI |
22
|
|
|
|
|
|
|
# objects when the datastore itself is emptied - e.g. %datastore = () |
23
|
|
|
|
|
|
|
# |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub BUILD { |
26
|
464
|
|
|
464
|
0
|
4171
|
my ( $self, $params ) = @_; |
27
|
|
|
|
|
|
|
|
28
|
464
|
100
|
|
|
|
1548
|
if ( defined $self->{global} ) { |
29
|
371
|
50
|
|
|
|
1037
|
croak "cannot specify both 'datastore' and 'global'" |
30
|
|
|
|
|
|
|
if ( defined( $self->{datastore} ) ); |
31
|
371
|
100
|
|
|
|
1482
|
$self->{datastore} = $self->{global} ? \%Global_Datastore : {}; |
32
|
|
|
|
|
|
|
} |
33
|
464
|
100
|
|
|
|
8931
|
if ( !defined( $self->{datastore} ) ) { |
34
|
6
|
|
|
|
|
135
|
cluck "must specify either 'datastore' hashref or 'global' flag"; |
35
|
6
|
|
|
|
|
15284
|
$self->{datastore} = \%Global_Datastore; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub fetch { |
40
|
8554
|
|
|
8554
|
0
|
19083
|
my ( $self, $key ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
8554
|
100
|
|
|
|
18114
|
if ( $self->{is_size_aware} ) { |
43
|
2661
|
|
|
|
|
6481
|
$self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time}->{$key} |
44
|
|
|
|
|
|
|
= time; |
45
|
|
|
|
|
|
|
} |
46
|
8554
|
|
|
|
|
35759
|
return $self->{datastore}->{ $self->{namespace} }->{$key}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub store { |
50
|
3238
|
|
|
3238
|
0
|
10415
|
my ( $self, $key, $data ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
3238
|
|
|
|
|
13710
|
$self->{datastore}->{ $self->{namespace} }->{$key} = $data; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub remove { |
56
|
892
|
|
|
892
|
0
|
11304
|
my ( $self, $key ) = @_; |
57
|
|
|
|
|
|
|
|
58
|
892
|
|
|
|
|
4474
|
delete $self->{datastore}->{ $self->{namespace} }->{$key}; |
59
|
892
|
|
|
|
|
8755
|
delete $self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time} |
60
|
|
|
|
|
|
|
->{$key}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub clear { |
64
|
499
|
|
|
499
|
0
|
5345
|
my ($self) = @_; |
65
|
|
|
|
|
|
|
|
66
|
499
|
|
|
|
|
7867
|
$self->{datastore}->{ $self->{namespace} } = {}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub get_keys { |
70
|
822
|
|
|
822
|
0
|
1217
|
my ($self) = @_; |
71
|
|
|
|
|
|
|
|
72
|
822
|
|
|
|
|
895
|
return keys( %{ $self->{datastore}->{ $self->{namespace} } } ); |
|
822
|
|
|
|
|
6608
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub get_namespaces { |
76
|
33
|
|
|
33
|
0
|
474
|
my ($self) = @_; |
77
|
|
|
|
|
|
|
|
78
|
33
|
|
|
|
|
33
|
return keys( %{ $self->{datastore} } ); |
|
33
|
|
|
|
|
158
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub discard_policy_lru { |
82
|
185
|
|
|
185
|
0
|
303
|
my ($self) = @_; |
83
|
|
|
|
|
|
|
|
84
|
185
|
|
|
|
|
501
|
my $last_used_time = |
85
|
|
|
|
|
|
|
$self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time}; |
86
|
1661
|
|
|
|
|
2411
|
my @keys_in_lru_order = |
87
|
185
|
|
|
|
|
716
|
sort { $last_used_time->{$a} <=> $last_used_time->{$b} } $self->get_keys; |
88
|
|
|
|
|
|
|
return sub { |
89
|
386
|
|
|
386
|
|
1253
|
shift(@keys_in_lru_order); |
90
|
185
|
|
|
|
|
1458
|
}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |