| 1 | package Cache::Ref::LIFO; | ||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | $Cache::Ref::LIFO::AUTHORITY = 'cpan:NUFFIN'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | 1 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | $Cache::Ref::LIFO::VERSION = '0.05'; # TRIAL | |||||||||||||
| 7 | } | ||||||||||||||
| 8 | # ABSTRACT: Saves entries until full, discarding subsequent sets. | ||||||||||||||
| 9 | |||||||||||||||
| 10 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | extends qw(Cache::Ref); | |||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | with ( | |||||||||||||
| 17 | 'Cache::Ref::Role::API', | ||||||||||||||
| 18 | 'Cache::Ref::Role::Index' => { | ||||||||||||||
| 19 | -alias => { | ||||||||||||||
| 20 | _index_get => "get", | ||||||||||||||
| 21 | }, | ||||||||||||||
| 22 | }, | ||||||||||||||
| 23 | ); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 1 | 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 | 2 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 1 | handles => { | |||||||||||||
| 37 | _add_key => "push", | ||||||||||||||
| 38 | #_splice_keys => "splice", | ||||||||||||||
| 39 | _clear_keys => "clear", | ||||||||||||||
| 40 | }, | ||||||||||||||
| 41 | ); | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 2 | ▶ | sub remove { | ||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 44 | 2 | my ( $self, @keys ) = @_; | |||||||||||||
| 45 | |||||||||||||||
| 46 | 2 | $self->_index_delete(@keys); | |||||||||||||
| 47 | |||||||||||||||
| 48 | 2 | my %keys; | |||||||||||||
| 49 | 2 | undef @keys{@keys}; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | @{ $self->_keys } = grep { not exists $keys{$_} } @{ $self->_keys }; | |||||||||||||
| 52 | |||||||||||||||
| 53 | 2 | return; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 1 | ▶ | sub clear { | ||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 57 | 1 | my $self = shift; | |||||||||||||
| 58 | 1 | $self->_index_clear; | |||||||||||||
| 59 | 1 | $self->_clear_keys; | |||||||||||||
| 60 | |||||||||||||||
| 61 | 1 | return; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 0 | ▶ | sub hit { } | ||||||||||||
|
Subroutine
hit: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 65 | |||||||||||||||
| 66 | 1275 | ▶ | sub set { | ||||||||||||
|
Subroutine
set: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 67 | 1275 | my ( $self, $key, $value ) = @_; | |||||||||||||
| 68 | |||||||||||||||
| 69 | 1275 | ▶ | if ( defined $self->_index_get($key) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 70 | 1 | $self->_index_set($key, $value) | |||||||||||||
| 71 | } elsif ( $self->_index_size < $self->size ) { | ||||||||||||||
| 72 | 9 | $self->_index_set($key, $value); | |||||||||||||
| 73 | 9 | $self->_add_key($key); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 1275 | return $value; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 1 | ▶ | sub expire { | ||||||||||||
|
Subroutine
expire: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 80 | 1 | my ( $self, $how_many ) = @_; | |||||||||||||
| 81 | |||||||||||||||
| 82 | 1 | ▶ | $how_many ||= 1; | ||||||||||||
|
Condition50%1/2
$how_many ||= 1
MC/DC0%0/1
$how_many ||= 1
$how_many
Truth table50%1/2
A
A= $how_many
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | #my @keys = $self->_splice_keys( -$how_many ); | ||||||||||||||
| 85 | 1 | my @keys = splice @{ $self->_keys }, -$how_many; | |||||||||||||
| 86 | |||||||||||||||
| 87 | 1 | $self->_index_delete(@keys); | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 1 | __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__ | ||||||||||||||