line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cache::Ref::LIFO; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
1198
|
$Cache::Ref::LIFO::AUTHORITY = 'cpan:NUFFIN'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
20
|
$Cache::Ref::LIFO::VERSION = '0.05'; # TRIAL |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
# ABSTRACT: Saves entries until full, discarding subsequent sets. |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
476
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use namespace::autoclean; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
extends qw(Cache::Ref); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
with ( |
17
|
|
|
|
|
|
|
'Cache::Ref::Role::API', |
18
|
|
|
|
|
|
|
'Cache::Ref::Role::Index' => { |
19
|
|
|
|
|
|
|
-alias => { |
20
|
|
|
|
|
|
|
_index_get => "get", |
21
|
|
|
|
|
|
|
}, |
22
|
|
|
|
|
|
|
}, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has size => ( |
26
|
|
|
|
|
|
|
isa => "Int", |
27
|
|
|
|
|
|
|
is => "ro", |
28
|
|
|
|
|
|
|
required => 1, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has _keys => ( |
32
|
|
|
|
|
|
|
traits => [qw(Array)], |
33
|
|
|
|
|
|
|
isa => "ArrayRef", |
34
|
|
|
|
|
|
|
is => "ro", |
35
|
|
|
|
|
|
|
default => sub { [] }, |
36
|
|
|
|
|
|
|
handles => { |
37
|
|
|
|
|
|
|
_add_key => "push", |
38
|
|
|
|
|
|
|
#_splice_keys => "splice", |
39
|
|
|
|
|
|
|
_clear_keys => "clear", |
40
|
|
|
|
|
|
|
}, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub remove { |
44
|
|
|
|
|
|
|
my ( $self, @keys ) = @_; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$self->_index_delete(@keys); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my %keys; |
49
|
|
|
|
|
|
|
undef @keys{@keys}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
@{ $self->_keys } = grep { not exists $keys{$_} } @{ $self->_keys }; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub clear { |
57
|
|
|
|
|
|
|
my $self = shift; |
58
|
|
|
|
|
|
|
$self->_index_clear; |
59
|
|
|
|
|
|
|
$self->_clear_keys; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub hit { } |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub set { |
67
|
|
|
|
|
|
|
my ( $self, $key, $value ) = @_; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
if ( defined $self->_index_get($key) ) { |
70
|
|
|
|
|
|
|
$self->_index_set($key, $value) |
71
|
|
|
|
|
|
|
} elsif ( $self->_index_size < $self->size ) { |
72
|
|
|
|
|
|
|
$self->_index_set($key, $value); |
73
|
|
|
|
|
|
|
$self->_add_key($key); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
return $value; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub expire { |
80
|
|
|
|
|
|
|
my ( $self, $how_many ) = @_; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$how_many ||= 1; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
#my @keys = $self->_splice_keys( -$how_many ); |
85
|
|
|
|
|
|
|
my @keys = splice @{ $self->_keys }, -$how_many; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
$self->_index_delete(@keys); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
__PACKAGE__; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=pod |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=encoding utf-8 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 NAME |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Cache::Ref::LIFO - Saves entries until full, discarding subsequent sets. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 SYNOPSIS |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $c = Cache::Ref::LIFO->new( size => $n ); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$c->set( foo => 42 ); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$c->get("foo"); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 DESCRIPTION |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This is a very naive cache algorithm, it saves cache sets until the |
115
|
|
|
|
|
|
|
cache is full, at which point all additional saves which aren't a |
116
|
|
|
|
|
|
|
value update are discarded immediately. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
For very predictable workflows this is potentially a good fit, |
119
|
|
|
|
|
|
|
provided the MFU is used early on. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
The advantages is that the code is very simple as a result. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 AUTHOR |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Yuval Kogman |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
This software is copyright (c) 2011 by Yuval Kogman. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
132
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
__END__ |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# ex: set sw=4 et: |