/usr/share/perl5/Cache/Ref/CLOCK/Base.pm is in libcache-ref-perl 0.04-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | package Cache::Ref::CLOCK::Base;
BEGIN {
$Cache::Ref::CLOCK::Base::AUTHORITY = 'cpan:NUFFIN';
}
BEGIN {
$Cache::Ref::CLOCK::Base::VERSION = '0.04';
}
use Moose::Role;
use namespace::autoclean;
with qw(
Cache::Ref::Role::API
Cache::Ref::Role::Index
);
requires qw(_hit);
has size => (
isa => "Int",
is => "ro",
required => 1,
);
has _hand => (
isa => "ScalarRef",
is => "ro",
default => sub { my $x = -1; return \$x },
);
has _buffer => (
isa => "ArrayRef",
is => "ro",
lazy => 1,
default => sub {
my ( $self, $p ) = @_;
return [ map { [] } 1 .. $self->size ],
},
);
sub hit {
my ( $self, @keys ) = @_;
$self->_hit( [ grep { defined } $self->_index_get(@keys) ] );
return;
}
sub get {
my ( $self, @keys ) = @_;
my @ret;
my @entries = $self->_index_get(@keys);
$self->_hit( [ grep { defined } @entries ] );
return ( @keys == 1 ? ($entries[0] && $entries[0][2]) : map { $_ && $_->[2] } @entries );
}
sub clear {
my $self = shift;
$self->_index_clear;
@$_ = () for @{ $self->_buffer };
}
sub remove {
my ( $self, @keys ) = @_;
@$_ = () for $self->_index_delete(@keys);
}
sub set {
my ( $self, $key, $value ) = @_;
if ( my $e = $self->_index_get($key) ) {
$e->[2] = $value;
} else {
my $e = $self->_find_free_slot;
@$e = ( 0, $key, $value ); # start at 0, not k
$self->_index_set( $key, $e );
}
}
sub expire {
my ( $self, $how_many ) = @_;
my $i = $self->_hand;
my $b = $self->_buffer;
while ( $how_many ) {
if ( $$i == $#$b ) {
$$i = -1;
}
if ( my $e = $b->[++$$i] ) {
if ( !$e->[0] ) {
$self->remove($e->[1]); # also clears @$e
$how_many--;
} else {
$e->[0]--;
}
}
}
return;
}
sub _find_free_slot {
my $self = shift;
my $i = $self->_hand;
my $b = $self->_buffer;
loop: {
if ( $$i == $#$b ) {
$$i = -1;
}
my $e = $b->[++$$i];
if ( not @$e ) {
return $e;
} elsif ( !$e->[0] ) {
$self->remove($e->[1]); # also clears @$e
return $e;
} else {
$e->[0]--;
redo loop;
}
}
}
# ex: set sw=4 et:
__PACKAGE__;
__END__
=pod
=encoding utf-8
=head1 NAME
Cache::Ref::CLOCK::Base
=head1 AUTHOR
Yuval Kogman
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Yuval Kogman.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|