Recipes/Cache.pm

filter: Statementstmt 86.3% 38 / 44 Branchbran 50.0% 8 / 16 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 92.3% 12 / 13 Podpod 85.7% 6 / 7 totaltotal 70.5% 72 / 102 CC 15 SCAR 13.9
CC
15
Cov
72%
new
7.6
exists
6.0
get
4.4
CRAP 4.0
« Recipes/Add.pm ↑ Summary Recipes/Delete.pm »
1 package CGI::Ex::Recipes::Cache;
2 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use CGI::Ex::Dump qw(debug dex_warn ctrace dex_trace);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our $VERSION = '0.01';
9  
10 1 ▶sub new {
Subroutine new: called CC=6 SCAR=20.3
Pod undocumented
11 1 ▶ my $class = shift || __PACKAGE__;
Condition50%1/2
shift() || __PACKAGE__
l!l
10
MC/DC0%0/1
shift() || __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
12 1 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
13 1 ▶ $args->{expires} ||= time + 3600;#one hour by default
Condition33%1/3
$args->{"expires"} ||= time + 3600
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{"expires"} ||= time + 3600
$args->{"expires"} time + 3600
Truth table33%1/3
A ||= B
A= $args->{"expires"}B= time + 3600
ABresult
1X1
011
000
14 1 ▶ $args->{cache_hash} ||= {};
Condition50%1/2
$args->{"cache_hash"} ||= {}
l!l
01
MC/DC0%0/1
$args->{"cache_hash"} ||= {}
$args->{"cache_hash"}
Truth table50%1/2
A
A= $args->{"cache_hash"}
Aresult
11
00
15 1 ▶ $args->{dbh} || croak 'Please provide a database handle with a `cache` table in the database!';
Branch: 1 branch
BranchTrueFalseTotal
$args->{"dbh"} or croak("Please provide a database handle with a `cache` table in the database!") 1 0 1
16 1 return bless {%$args}, $class;
17 }
18  
19 #tries first the 'our %CACHE_HASH', then the database.
20 0 ▶sub exists {
Subroutine exists: not called CC=2 SCAR=17.9
Pod documented
21 0 my ($self,$key) = @_;
22 0 ▶ 1 if(exists $self->{cache_hash}{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cache_hash"}{$key} 0 0 0
23 0 0;
24 }
25  
26 3 ▶sub get {
Subroutine get: called CC=4 SCAR=14.8
Pod documented
27 3 my ($self,$key) = @_;
28 #dex_trace(); #debug $self;
29 3 ▶ if(exists $self->{cache_hash}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cache_hash"}{$key}) 0 3 3
30 0 return $self->{cache_hash}{$key}{value};
31 }
32 #warn 'getting $key'.$key.' from database';
33 3 ▶ my $row = $self->{dbh}->selectrow_hashref('SELECT * FROM cache WHERE id=?',{},$key)
Condition100%2/2
$self->{'dbh'}->selectrow_hashref('SELECT * FROM cache WHERE id=?', {}, $key) || (return undef)
l!l
21
MC/DC100%1/1
$self->{'dbh'}->selectrow_hashref('SELECT * FROM cache WHERE id=?', {}, $key) || (return undef)
$self->{'dbh'}->selectrow_hashref('SELECT * FROM cache WHERE id=?', {}, $key)
Truth table100%2/2
A
A= $self->{'dbh'}->selectrow_hashref('SELECT * FROM cache WHERE id=?', {}, $key)
Aresult
11
00
34 || return undef;
35 2 $self->{cache_hash}{$key} = $row;
36 2 ▶ if($self->{cache_hash}{$key}{expires} < time ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_hash"}{$key}{"expires"} < time) 0 2 2
37 #warn 'could not $key'.$key.' from database. data expired.';
38 0 return undef;
39 }
40 2 return $self->{cache_hash}{$key}{value};
41 }
42  
43 2 ▶sub set {
Subroutine set: called CC=3 SCAR=11.7
Pod documented
44 2 ▶ if (!$_[2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[2]) 0 2 2
45 0 croak 'Please provide a value to be set!';
46 }
47 #NOTE: compatible only with SQLITE and MySQL
48 $_[0]->{dbh}->prepare(
49 'REPLACE INTO `cache` (id, value, tstamp, expires) VALUES ( ?,?,?,? )'
50 2 ▶ )->execute( $_[1], $_[2], time, ($_[3]?time+$_[3]:$_[0]->{expires}) );
Branch: 1 branch
BranchTrueFalseTotal
$_[3] ? : 1 1 2
51 }
52  
53 1 ▶sub clear {
Subroutine clear: called CC=2 SCAR=7.2
Pod documented
54 1 $_[0]->{cache_hash} = {};
55 $_[0]->{dbh}->do('DELETE FROM `cache`')
56 1 ▶ and $_[0]->{dbh}->do('VACUUM');
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"dbh"}->do("DELETE FROM `cache`") and $_[0]{"dbh"}->do("VACUUM") 1 0 1
57  
58 }
59  
60 1 ▶sub freeze {
Subroutine freeze: called CC=3 SCAR=14.2
Pod documented
61 $_[0]->set(
62 $_[1],
63 ref $_[2] ? Storable::nfreeze($_[2]) : $_[2],
64 $_[3] || $_[0]->{expires},
65 1 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[2] ? : 1 0 1
Condition33%1/3
$_[3] || $_[0]{'expires'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$_[3] || $_[0]{'expires'}
$_[3] $_[0]{'expires'}
Truth table33%1/3
A || B
A= $_[3]B= $_[0]{'expires'}
ABresult
1X1
011
000
66 }
67  
68 1 ▶sub thaw {
Subroutine thaw: called CC=1 SCAR=0.0
Pod documented
69 1 Storable::thaw( $_[0]->get($_[1]) ) ;
70 }
71  
72  
73 2 1;
74  
75 __END__
« Recipes/Add.pm ↑ Summary Recipes/Delete.pm »