File Coverage

blib/lib/Cache/Ref/CLOCK.pm
Criterion Covered Total %
statement 3 5 60.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Cache::Ref::CLOCK;
2             BEGIN {
3 1     1   9615 $Cache::Ref::CLOCK::AUTHORITY = 'cpan:NUFFIN';
4             }
5             BEGIN {
6 1     1   24 $Cache::Ref::CLOCK::VERSION = '0.05'; # TRIAL
7             }
8             # ABSTRACT: CLOCK cache replacement algorithm
9              
10 1     1   774 use Moose;
  0            
  0            
11              
12             use namespace::autoclean;
13              
14             extends qw(Cache::Ref);
15              
16             with qw(Cache::Ref::CLOCK::Base);
17              
18             has k => (
19             isa => "Int",
20             is => "ro",
21             default => 1,
22             );
23              
24             sub _hit {
25             my ( $self, $e ) = @_;
26              
27             my $k = 0+$self->k; # moose stingifies default
28             $_->[0] = $k for @$e;
29             }
30              
31             __PACKAGE__->meta->make_immutable;
32              
33             __PACKAGE__;
34              
35              
36             # ex: set sw=4 et:
37              
38             __END__
39             =pod
40              
41             =encoding utf-8
42              
43             =head1 NAME
44              
45             Cache::Ref::CLOCK - CLOCK cache replacement algorithm
46              
47             =head1 SYNOPSIS
48              
49             my $c = Cache::Ref::CLOCK->new(
50             size => $n,
51             k => $k,
52             );
53              
54             =head1 DESCRIPTION
55              
56             This algorithm is provides a second chance FIFO cache expiry policy using a
57             circular buffer.
58              
59             It is a very well accepted page replacement algorithm, but largely for reasons
60             which are irrelevant in this context (cache hits don't need to be serialized in
61             a multiprocessing context as they only require an idempotent operation (setting
62             a bit to 1)).
63              
64             =head1 ATTRIBUTES
65              
66             =over 4
67              
68             =item size
69              
70             The size of the live entries.
71              
72             =item k
73              
74             This is the initial value given to all hit entries.
75              
76             As the hand moves through the circular buffer it decrements the counters.
77              
78             The default is C<1>, providing semantics similar to a second chance FIFO cache.
79              
80             Larger values of C<k> model LRU more accurately.
81              
82             This is pretty silly though, as L<Cache::Ref::LRU> is probably way more
83             efficient for any C<k> bigger than 1.
84              
85             =back
86              
87             =head1 AUTHOR
88              
89             Yuval Kogman
90              
91             =head1 COPYRIGHT AND LICENSE
92              
93             This software is copyright (c) 2011 by Yuval Kogman.
94              
95             This is free software; you can redistribute it and/or modify it under
96             the same terms as the Perl 5 programming language system itself.
97              
98             =cut
99