| 1 | package Cache::LRU; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use 5.008_001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Scalar::Util qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | our $VERSION = '0.04'; | |||||||||
| 11 | |||||||||||
| 12 | sub GC_FACTOR () { 10 } | ||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 15 | 2 | my ($klass, %args) = @_; | |||||||||
| 16 | 2 | return bless { | |||||||||
| 17 | size => 1024, | ||||||||||
| 18 | %args, | ||||||||||
| 19 | _entries => {}, # $key => $weak_valueref | ||||||||||
| 20 | _fifo => [], # fifo queue of [ $key, $valueref ] | ||||||||||
| 21 | }, $klass; | ||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 9 | ▶ | sub set { | ||||||||
|
Subroutine
set: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 25 | 9 | my ($self, $key, $value) = @_; | |||||||||
| 26 | |||||||||||
| 27 | 9 | my $entries = $self->{_entries}; | |||||||||
| 28 | |||||||||||
| 29 | 9 | ▶ | if (my $old_value_ref = $entries->{$key}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 2 | $$old_value_ref = undef; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | # register | ||||||||||
| 34 | 9 | my $value_ref = \$value; | |||||||||
| 35 | 9 | Scalar::Util::weaken($entries->{$key} = $value_ref); | |||||||||
| 36 | 9 | $self->_update_fifo($key, $value_ref); | |||||||||
| 37 | |||||||||||
| 38 | # expire the oldest entry if full | ||||||||||
| 39 | 9 | ▶ | while (scalar(keys %$entries) > $self->{size}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 16 | my $exp_key = shift(@{$self->{_fifo}})->[0]; | |||||||||
| 41 | delete $entries->{$exp_key} | ||||||||||
| 42 | 16 | ▶ | unless $entries->{$exp_key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 9 | $value; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 2 | ▶ | sub remove { | ||||||||
|
Subroutine
remove: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 49 | 2 | my ($self, $key) = @_; | |||||||||
| 50 | 2 | my $value_ref = delete $self->{_entries}->{$key}; | |||||||||
| 51 | 2 | ▶ | return undef unless $value_ref; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 2 | my $value = $$value_ref; | |||||||||
| 53 | 2 | $$value_ref = undef; | |||||||||
| 54 | 2 | $value; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 25 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 58 | 25 | my ($self, $key) = @_; | |||||||||
| 59 | |||||||||||
| 60 | 25 | my $value_ref = $self->{_entries}->{$key}; | |||||||||
| 61 | 25 | ▶ | return undef unless $value_ref; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 18 | $self->_update_fifo($key, $value_ref); | |||||||||
| 64 | 18 | $$value_ref; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 1 | ▶ | sub clear { | ||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 68 | 1 | my $self = shift; | |||||||||
| 69 | |||||||||||
| 70 | 1 | $self->{_entries} = {}; | |||||||||
| 71 | 1 | $self->{_fifo} = []; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 27 | ▶ | sub _update_fifo { | ||||||||
|
Subroutine
_update_fifo: called CC=4 SCAR=23.1
| |||||||||||
| 75 | # precondition: $self->{_entries} should contain given key | ||||||||||
| 76 | 27 | my ($self, $key, $value_ref) = @_; | |||||||||
| 77 | 27 | my $fifo = $self->{_fifo}; | |||||||||
| 78 | |||||||||||
| 79 | 27 | push @$fifo, [ $key, $value_ref ]; | |||||||||
| 80 | 27 | ▶ | if (@$fifo >= $self->{size} * GC_FACTOR) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | my $entries = $self->{_entries}; | |||||||||
| 82 | 0 | my @new_fifo; | |||||||||
| 83 | 0 | my %need = map { $_ => 1 } keys %$entries; | |||||||||
| 84 | 0 | ▶ | while (%need) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | my $fifo_entry = pop @$fifo; | |||||||||
| 86 | unshift @new_fifo, $fifo_entry | ||||||||||
| 87 | 0 | ▶ | if delete $need{$fifo_entry->[0]}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | } | ||||||||||
| 89 | 0 | $self->{_fifo} = \@new_fifo; | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 2 | 1; | |||||||||
| 94 | __END__ | ||||||||||