blib/lib/Db/Mediasurface/Cache.pm

filter: Statementstmt 29.1% 14 / 48 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 4 / 8 Podpod 100% 4 / 4 totaltotal 26.5% 22 / 83 CC 12 SCAR 33.0
CC
12
Cov
20%
set
56.0
get
20.0
unset
12.0
CRAP 27.1
↑ Summary
1 package Db::Mediasurface::Cache;
2 1 $VERSION = 0.04;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use constant VALUE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use constant COUNT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
10 0 my ($class, %arg) = @_;
11 my $self = { hits => 0,
12 size => 0,
13 maxsize => $arg{size},
14 0 hash => undef
15 };
16 0 bless $self, $class;
17 }
18  
19 0 ▶sub get {
Subroutine get: not called CC=4 SCAR=30.0
Pod documented
20 0 my ($self, $key) = @_;
21 0 ▶ return unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
22 0 ▶ return unless exists $self->{hash}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hash"}{$key} 0 0 0
23 0 my $value = $self->{hash}->{$key}->[VALUE];
24 0 ▶ if (defined $self->{maxsize}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"maxsize"}) 0 0 0
25 0 $self->{hash}->{$key}->[COUNT]++;
26 0 $self->{hits} ++;
27 }
28 0 return $value;
29 }
30  
31 0 ▶sub set {
Subroutine set: not called CC=7 SCAR=40.3
Pod documented
32 0 my ($self, $key, $value) = @_;
33 0 ▶ return unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
34 0 ▶ if (exists $self->{hash}->{$key}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'hash'}{$key}) { } 0 0 0
35 0 $self->{hash}->{$key}->[VALUE] = $value;
36 0 $self->{hits} = $self->{hits} - $self->{hash}->{$key}->[COUNT];
37 0 $self->{hash}->{$key}->[COUNT] = 0;
38 } else {
39 0 $self->{hash}->{$key}->[VALUE] = $value;
40 0 $self->{hash}->{$key}->[COUNT] = 0;
41 0 $self->{size} ++;
42 }
43 0 ▶ if ((defined $self->{maxsize}) && ($self->{size} >= $self->{maxsize})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"maxsize"} and $self->{"size"} >= $self->{"maxsize"}) 0 0 0
Condition0%0/3
defined $self->{"maxsize"} and $self->{"size"} >= $self->{"maxsize"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"maxsize"} and $self->{"size"} >= $self->{"maxsize"}
defined $self->{"maxsize"} $self->{"size"} >= $self->{"maxsize"}
Truth table0%0/3
A and B
A= defined $self->{"maxsize"}B= $self->{"size"} >= $self->{"maxsize"}
ABresult
0X0
100
111
44 0 my $cutoff = int($self->{hits} / $self->{size}) + 1;
45 0 foreach my $test_key (keys %{$self->{hash}}){
46 0 ▶ if ( $self->{hash}->{$test_key}->[COUNT] > $cutoff ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'hash'}{$test_key}[1] > $cutoff) { } 0 0 0
47 0 $self->{hash}->{$test_key}->[COUNT] = 0;
48 } else {
49 0 delete $self->{hash}->{$test_key};
50 0 $self->{size} --;
51 }
52 }
53 0 $self->{hits} = 0;
54 }
55 }
56  
57 0 ▶sub unset {
Subroutine unset: not called CC=3 SCAR=24.8
Pod documented
58 0 my ($self,$key) = @_;
59 0 ▶ return unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
60 0 ▶ return unless exists $self->{hash}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"hash"}{$key} 0 0 0
61 0 $self->{hits} = $self->{hits} - $self->{hash}->{$key}->[COUNT];
62 0 delete $self->{hash}->{$key};
63 }
64  
65 1 1;
66  
67 =head1 NAME
68  
69 Db::Mediasurface::Cache - caches a specified number of key-value pairs, disgarding underused pairs.
70  
71 =head1 VERSION
72  
73 This document refers to version 0.04 of DB::Mediasurface::Cache, released August 3, 2001.
74  
75 =head1 SYNOPSIS
76  
77 use Db::Mediasurface::Cache;
78  
79 my $url = 'http://some.site.com/some/path?version=2';
80  
81 my $id = undef;
82  
83 my $cache = Db::Mediasurface::Cache->new( size => 1000 );
84  
85 unless (defined ($id = $cache->get($url)))
86 {
87 $id = urldecode2id($url);
88 $cache->set($url,$id);
89 }
90  
91 =head1 DESCRIPTION
92  
93 =head2 Overview
94  
95 Mediasurface relies on retrieving a unique ID for almost every object lookup. This module aims to cache url->id lookups in memory. The module allows commonly used key-value pairs to be stored towards the 'fresh' end of the store, and seldomly used pairs to drift towards the 'stale' end, from where they will eventually be pushed into oblivion, should the cache reach its maximum size. Basically, it's a trade-off between size and speed - the module will perform best when you need to perform lots of lookups of a wide range of urls, but the majority of lookups are contained within a much smaller subset of urls.
96  
97 =head2 Constructor
98  
99 =over 4
100  
101 =item $cache = Db::Mediasurface::Cache->new(size=>1000);
102  
103 This class method constructs a new cache. the size parameter can be used to set the maximum number of key-value pairs to be cached. If size is omitted, the cache defaults to using an infinite store, which has no protection from eating all your available RAM [NOTE: this is a change in behaviour from version 0.02].
104  
105 =back
106  
107 =head2 Methods
108  
109 =over 4
110  
111 =item $cache->set($key,$value)
112  
113 Sets key-value pairs. [Note that this method can now only accept *one* key-value pair. This is a change of behaviour from version 0.03].
114  
115 =item $id = $cache->get($key);
116  
117 Gets the value of a given key. Returns the value, or undef if the key doesn't exist.
118  
119 =item $cache->unset($key1);
120  
121 Delete the key-value pair specified by the given key.
122  
123 =back
124  
125 =head1 AUTHOR
126  
127 Nigel Wetters (nwetters@cpan.org)
128  
129 =head1 COPYRIGHT
130  
131 Copyright (c) 2001, Nigel Wetters. All Rights Reserved.
132 This module is free software. It may be used, redistributed
133 and/or modified under the same terms as Perl itself.
134  
↑ Summary