| 1 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Sub::Contract::Cache - Implement a subroutine's cache | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # $Id: Cache.pm,v 1.3 2009/06/16 12:23:58 erwan_lemonnier Exp $ | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Sub::Contract::Cache; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 23 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 23 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 23 | ▶ | use Symbol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 23 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # NOTE: to speed up things, we do very little sanity control of method | ||||||||||||||||||||||||||||
| 19 | # arguments, so that a key can for example be undefined though it | ||||||||||||||||||||||||||||
| 20 | # should be an error. This class is to be used internally by | ||||||||||||||||||||||||||||
| 21 | # Sub::Contract only. If you attempt to use it directly for other | ||||||||||||||||||||||||||||
| 22 | # purpose, make sure you really need to do that, and if so, don't rely | ||||||||||||||||||||||||||||
| 23 | # on Sub::Contract::Cache to validate them for you. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 8 | my ($class,%args) = @_; | |||||||||||||||||||||||||||
| 27 | 8 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 28 | 8 | ▶ | my $size = delete $args{size} or croak "BUG: missing max_size"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 8 | ▶ | my $namespace = delete $args{namespace} or croak "BUG: missing namespace"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 8 | ▶ | croak "BUG: new() got unknown arguments: ".Dumper(%args) if (%args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 8 | ▶ | croak "BUG: size should be a number" if (!defined $size || $size !~ /^\d+$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $size and $size =~ /^\d+$/
MC/DC0%0/2
defined $size and $size =~ /^\d+$/
defined $size
$size =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $sizeB= $size =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # NOTE: $contract->reset() deletes this cache | ||||||||||||||||||||||||||||
| 35 | # TODO: do we want to keep previous content of cache? | ||||||||||||||||||||||||||||
| 36 | 8 | my $self = bless({},$class); | |||||||||||||||||||||||||||
| 37 | 8 | $self->{cache} = {}; | |||||||||||||||||||||||||||
| 38 | 8 | $self->{cache_max_size} = $size; | |||||||||||||||||||||||||||
| 39 | 8 | $self->{cache_size} = 0; | |||||||||||||||||||||||||||
| 40 | 8 | $self->{namespace} = $namespace; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 8 | return $self; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | # a fast way to delete all keys in a hash | ||||||||||||||||||||||||||||
| 48 | 1 | delete @{$self->{cache}}{keys %{$self->{cache}}}; | |||||||||||||||||||||||||||
| 49 | 1 | $self->{cache_size} = 0; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 71 | ▶ | sub has { | ||||||||||||||||||||||||||
|
Subroutine
has: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 71 | my ($self,$key) = @_; | |||||||||||||||||||||||||||
| 54 | 71 | return exists $self->{cache}->{$key}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 31 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 31 | my ($self,$key,$value) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 31 | ▶ | croak "BUG: undefined cache key".Dumper($key,$value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | if (!defined $key); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 31 | ▶ | if ($self->{cache_size} >= $self->{cache_max_size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | $self->clear; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 31 | $self->{cache}->{$key} = $value; | |||||||||||||||||||||||||||
| 68 | 31 | $self->{cache_size}++; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 40 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 40 | my ($self,$key) = @_; | |||||||||||||||||||||||||||
| 74 | 40 | return $self->{cache}->{$key}; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 23 | 1; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =pod | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 NAME | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Sub::Contract::Cache - A data cache | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | my $cache = new Sub::Contract::Cache(max_size => 10000, namespace => 'foo'); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | if ($cache->has($key)) { | ||||||||||||||||||||||||||||
| 90 | return $cache->get($key); | ||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | my $value = foo(@args); | ||||||||||||||||||||||||||||
| 93 | $cache->set($key,$value); | ||||||||||||||||||||||||||||
| 94 | return $value; | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | A Sub::Contract::Cache is just a data cache used by contracts to | ||||||||||||||||||||||||||||
| 100 | memoize subroutine's results. Sub::Contract has its own cache | ||||||||||||||||||||||||||||
| 101 | implementation for efficiency reasons. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 API | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =over 4 | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =item C<< my $cache = new(max_size => $max_size, namespace => $name) >> | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Return an empty cache object that may contain up to C<$max_size> | ||||||||||||||||||||||||||||
| 110 | elements and caches results from the subroutine C<$name>. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =item C<< $contract->clear([size => $max_size]) >> | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Empty this cache of all its elements. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =item C<< $contract->set($key,$ref_result) >> | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Add a cache entry for the key C<$key> with result C<$result>. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item C<< $contract->has($key) >> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Return true if the cache contains a result for this key, false if not. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item C<< $contract->get($key) >> | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Return the cached result associated with key C<$key>. You must call | ||||||||||||||||||||||||||||
| 127 | C<has> first to ensure that there really is a cached result for this | ||||||||||||||||||||||||||||
| 128 | key. C<get> on an unknown key will return undef and not fail. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =back | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | See 'Sub::Contract'. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 VERSION | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | $Id: Cache.pm,v 1.3 2009/06/16 12:23:58 erwan_lemonnier Exp $ | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Erwan Lemonnier C<< <erwan@cpan.org> >> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | See Sub::Contract. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||