line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CHI::Driver::Memory; |
2
|
|
|
|
|
|
|
$CHI::Driver::Memory::VERSION = '0.61'; |
3
|
20
|
|
|
20
|
|
152
|
use Carp qw(cluck croak); |
|
20
|
|
|
|
|
44
|
|
|
20
|
|
|
|
|
1516
|
|
4
|
20
|
|
|
20
|
|
8114
|
use CHI::Constants qw(CHI_Meta_Namespace); |
|
20
|
|
|
|
|
57
|
|
|
20
|
|
|
|
|
1074
|
|
5
|
20
|
|
|
20
|
|
138
|
use Moo; |
|
20
|
|
|
|
|
46
|
|
|
20
|
|
|
|
|
142
|
|
6
|
20
|
|
|
20
|
|
18235
|
use MooX::Types::MooseLike::Base qw(:all); |
|
20
|
|
|
|
|
140676
|
|
|
20
|
|
|
|
|
7234
|
|
7
|
20
|
|
|
20
|
|
195
|
use strict; |
|
20
|
|
|
|
|
44
|
|
|
20
|
|
|
|
|
627
|
|
8
|
20
|
|
|
20
|
|
120
|
use warnings; |
|
20
|
|
|
|
|
41
|
|
|
20
|
|
|
|
|
13705
|
|
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
|
29
|
|
|
29
|
0
|
652
|
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
|
481
|
|
|
481
|
0
|
1723
|
my ( $self, $params ) = @_; |
27
|
|
|
|
|
|
|
|
28
|
481
|
100
|
|
|
|
1657
|
if ( defined $self->{global} ) { |
29
|
|
|
|
|
|
|
croak "cannot specify both 'datastore' and 'global'" |
30
|
376
|
50
|
|
|
|
1144
|
if ( defined( $self->{datastore} ) ); |
31
|
376
|
100
|
|
|
|
1640
|
$self->{datastore} = $self->{global} ? \%Global_Datastore : {}; |
32
|
|
|
|
|
|
|
} |
33
|
481
|
100
|
|
|
|
3429
|
if ( !defined( $self->{datastore} ) ) { |
34
|
6
|
|
|
|
|
110
|
cluck "must specify either 'datastore' hashref or 'global' flag"; |
35
|
6
|
|
|
|
|
12276
|
$self->{datastore} = \%Global_Datastore; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub fetch { |
40
|
9127
|
|
|
9127
|
0
|
26926
|
my ( $self, $key ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
9127
|
100
|
|
|
|
19993
|
if ( $self->{is_size_aware} ) { |
43
|
2942
|
|
|
|
|
7110
|
$self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time}->{$key} |
44
|
|
|
|
|
|
|
= time; |
45
|
|
|
|
|
|
|
} |
46
|
9127
|
|
|
|
|
36112
|
return $self->{datastore}->{ $self->{namespace} }->{$key}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub store { |
50
|
3508
|
|
|
3508
|
0
|
14558
|
my ( $self, $key, $data ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
3508
|
|
|
|
|
13579
|
$self->{datastore}->{ $self->{namespace} }->{$key} = $data; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub remove { |
56
|
1000
|
|
|
1000
|
0
|
13693
|
my ( $self, $key ) = @_; |
57
|
|
|
|
|
|
|
|
58
|
1000
|
|
|
|
|
3523
|
delete $self->{datastore}->{ $self->{namespace} }->{$key}; |
59
|
|
|
|
|
|
|
delete $self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time} |
60
|
1000
|
|
|
|
|
8303
|
->{$key}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub clear { |
64
|
531
|
|
|
531
|
0
|
6637
|
my ($self) = @_; |
65
|
|
|
|
|
|
|
|
66
|
531
|
|
|
|
|
7853
|
$self->{datastore}->{ $self->{namespace} } = {}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub get_keys { |
70
|
890
|
|
|
890
|
0
|
1805
|
my ($self) = @_; |
71
|
|
|
|
|
|
|
|
72
|
890
|
|
|
|
|
1336
|
return keys( %{ $self->{datastore}->{ $self->{namespace} } } ); |
|
890
|
|
|
|
|
6816
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub get_namespaces { |
76
|
33
|
|
|
33
|
0
|
595
|
my ($self) = @_; |
77
|
|
|
|
|
|
|
|
78
|
33
|
|
|
|
|
49
|
return keys( %{ $self->{datastore} } ); |
|
33
|
|
|
|
|
198
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub discard_policy_lru { |
82
|
209
|
|
|
209
|
0
|
469
|
my ($self) = @_; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $last_used_time = |
85
|
209
|
|
|
|
|
576
|
$self->{datastore}->{ CHI_Meta_Namespace() }->{last_used_time}; |
86
|
|
|
|
|
|
|
my @keys_in_lru_order = |
87
|
209
|
|
|
|
|
646
|
sort { $last_used_time->{$a} <=> $last_used_time->{$b} } $self->get_keys; |
|
1858
|
|
|
|
|
3483
|
|
88
|
|
|
|
|
|
|
return sub { |
89
|
434
|
|
|
434
|
|
1297
|
shift(@keys_in_lru_order); |
90
|
209
|
|
|
|
|
1584
|
}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |