Prog/Cache.pm

filter: Statementstmt 88.5% 31 / 35 Branchbran 40.0% 4 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 88.8% 8 / 9 Podpod 100% 5 / 5 totaltotal 79.0% 49 / 62 CC 7 SCAR 9.2
CC
7
Cov
77%
delete
6.0
get
3.1
init
2.0
CRAP 2.5
« Prog/Native/Result.pm ↑ Summary Prog/Result.pm »
1 package SWISH::Prog::Cache;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw( SWISH::Prog::Class );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 __PACKAGE__->mk_accessors(qw( cache ));
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.75';
9  
10 =pod
11  
12 =head1 NAME
13  
14 SWISH::Prog::Cache - simple in-memory cache class
15  
16 =head1 SYNOPSIS
17  
18 use SWISH::Prog::Cache;
19 my $cache = SWISH::Prog::Cache->new;
20 $cache->add( foo => 'bar' );
21 $cache->has( 'foo' ); # returns true
22 $cache->get( 'foo' ); # returns 'bar'
23 $cache->delete( 'foo' ); # removes 'foo' from cache and returns 'bar'
24  
25 =head1 DESCRIPTION
26  
27 SWISH::Prog::Cache is a simple in-memory caching class. It's basically
28 just a Perl hash, but implemented as a class so that you can subclass it
29 and use different storage (e.g. Cache::* modules).
30  
31 =cut
32  
33 =head1 METHODS
34  
35 See SWISH::Prog::Class. Only new or overridden methods are documented here.
36  
37 =head2 init
38  
39 Initialize the cache. Called internally by new(). You should not need to
40 call this yourself.
41  
42 =cut
43  
44 2 ▶sub init {
Subroutine init: called CC=2 SCAR=7.1
Pod documented
45 2 my $self = shift;
46 2 $self->SUPER::init(@_);
47 2 ▶ $self->{cache} ||= {};
Condition50%1/2
$self->{"cache"} ||= {}
l!l
02
MC/DC0%0/1
$self->{"cache"} ||= {}
$self->{"cache"}
Truth table50%1/2
A
A= $self->{"cache"}
Aresult
11
00
48 }
49  
50 =head2 cache([ I<hash_ref> ])
51  
52 Get/set the internal in-memory cache. The default is just a hash ref.
53 Subclasses are encouraged to implement their own storage.
54  
55 =head2 has( I<key> )
56  
57 Does I<key> exist in cache.
58  
59 =cut
60  
61 19 ▶sub has {
Subroutine has: called CC=2 SCAR=7.0
Pod documented
62 19 my $self = shift;
63 19 my $key = shift;
64 19 ▶ defined($key) or croak "key required";
Branch: 1 branch
BranchTrueFalseTotal
defined $key or croak("key required") 19 0 19
65 19 return exists $self->{cache}->{$key};
66 }
67  
68 =head2 get( I<key> )
69  
70 Returns value for I<key>. Returns undef if has( I<key> ) is false.
71  
72 =cut
73  
74 9 ▶sub get {
Subroutine get: called CC=3 SCAR=11.4
Pod documented
75 9 my $self = shift;
76 9 my $key = shift;
77 9 ▶ defined($key) or croak "key required";
Branch: 1 branch
BranchTrueFalseTotal
defined $key or croak("key required") 9 0 9
78 9 ▶ return exists $self->{cache}->{$key} ? $self->{cache}->{$key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'cache'}{$key} ? : 9 0 9
79 }
80  
81 =head2 delete( I<key> )
82  
83 Delete I<key> from cache.
84  
85 =cut
86  
87 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
88 0 my $self = shift;
89 0 my $key = shift;
90 0 ▶ defined($key) or croak "key required";
Branch: 1 branch
BranchTrueFalseTotal
defined $key or croak("key required") 0 0 0
91 0 delete $self->{cache}->{$key};
92 }
93  
94 =head2 add( I<key> => I<value> )
95  
96 Add I<key> to cache with value I<value>.
97  
98 =cut
99  
100 18 ▶sub add {
Subroutine add: called CC=2 SCAR=7.0
Pod documented
101 18 my $self = shift;
102 18 my $key = shift;
103 18 my $val = shift;
104 18 ▶ defined($key) or croak "key required";
Branch: 1 branch
BranchTrueFalseTotal
defined $key or croak("key required") 18 0 18
105 18 $self->{cache}->{$key} = $val;
106 }
107  
108 2 1;
109  
110 __END__
« Prog/Native/Result.pm ↑ Summary Prog/Result.pm »