CannedQuery/SimpleQueryCache.pm

filter: Statementstmt 100% 64 / 64 Branchbran 75.0% 9 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 95.1% 98 / 103 CC 8 SCAR 6.3
CC
8
Cov
95%
get_query_from_cache
4.0
_make_digest_key
3.0
add_query_to_cache
2.0
CRAP 1.9
« CannedQuery.pm ↑ Summary
1 #!perl
2 #
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Rose::DBx::CannedQuery::SimpleQueryCache;
8  
9 1 our ($VERSION) = '1.00';
10  
11 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Types::Standard qw/ CodeRef Object /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 has backend => (
18 isa => Object,
19 is => 'ro',
20 required => 1,
21 lazy => 1,
22 builder => '_build_backend'
23 );
24  
25 1 has args_to_key => (
26 isa => CodeRef,
27 is => 'ro',
28 required => 1,
29 lazy => 1,
30 builder => '_build_args_to_key'
31 );
32  
33 1 ▶sub _build_backend {
Subroutine _build_backend: called CC=1 SCAR=0.0
34 1 Rose::DBx::CannedQuery::SimpleQueryCache::_PlainOldHash->new();
35 }
36  
37 {
38 1 my $dumper = Data::Dumper->new( [] )->Sortkeys(1);
39  
40 14 ▶ sub _make_digest_key {
Subroutine _make_digest_key: called CC=3 SCAR=11.1
41 14 ▶ my (%args) = ref $_[0] ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 14 0 14
42 my $key =
43 14 $dumper->Reset->Values( [ map { $_ => $args{$_} } sort keys %args ] )
44 ->Dump;
45 14 $key =~ s/\s+/ /g;
46 14 $key = lc $key;
47 14 ▶ $key = md5_hex($key) if length($key) > 512;
Branch: 1 branch
BranchTrueFalseTotal
if length $key > 512 0 14 14
48 14 return $key;
49 }
50 }
51  
52 1 ▶sub _build_args_to_key {
Subroutine _build_args_to_key: called CC=1 SCAR=0.0
53 1 return \&_make_digest_key;
54 }
55  
56 9 ▶sub get_query_from_cache {
Subroutine get_query_from_cache: called CC=4 SCAR=13.9
Pod documented
57 9 my $self = shift;
58 9 my $key = $self->args_to_key->(@_);
59 9 my $qry = $self->backend->get($key);
60  
61 9 ▶ if ($qry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qry) 5 4 9
62 5 my $sth = $qry->sth;
63  
64 # We take this roundabout path to check on connectedness because
65 # $sth's own Active attribute may be false if prepared but not yet
66 # executed, and we don't want to go through Rose::DB->dbh to get
67 # DBI's database handle, since it'll reconnect under the hood if
68 # it's been disconnected, which doesn't help $sth
69 5 ▶ return $qry if $sth->{Active} or $sth->{Database}->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"Active"} or $sth->{"Database"}{"Active"} 4 1 5
Condition66%2/3
$sth->{"Active"} or $sth->{"Database"}{"Active"}
l!l&&r!l&&!r
401
MC/DC50%1/2
$sth->{"Active"} or $sth->{"Database"}{"Active"}
$sth->{"Active"} $sth->{"Database"}{"Active"}
Truth table66%2/3
A or B
A= $sth->{"Active"}B= $sth->{"Database"}{"Active"}
ABresult
1X1
011
000
70 1 $self->backend->remove($key);
71 }
72 5 return;
73 }
74  
75 4 ▶sub add_query_to_cache {
Subroutine add_query_to_cache: called CC=2 SCAR=7.0
Pod documented
76 4 my $self = shift;
77 4 my $query = shift;
78 4 my $key = $self->args_to_key->(@_);
79 4 my $ret = $self->backend->set( $key, $query );
80 4 ▶ return $ret ? $query : $ret;
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 4 0 4
81 }
82  
83 1 ▶sub remove_query_from_cache {
Subroutine remove_query_from_cache: called CC=1 SCAR=0.0
Pod documented
84 1 my $self = shift;
85 1 my $key = $self->args_to_key->(@_);
86 1 return $self->backend->remove($key);
87 }
88  
89 1 ▶sub clear_query_cache {
Subroutine clear_query_cache: called CC=1 SCAR=0.0
Pod documented
90 1 shift->backend->clear();
91 }
92  
93 {
94  
95 package Rose::DBx::CannedQuery::SimpleQueryCache::_PlainOldHash;
96  
97 1 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
98 1 bless {}, shift;
99 }
100  
101 9 ▶ sub get {
Subroutine get: called CC=2 SCAR=6.9
102 9 my ( $self, $key ) = @_;
103 9 ▶ return $self->{$key} if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 5 4 9
104 4 return;
105 }
106  
107 4 ▶ sub set {
Subroutine set: called CC=1 SCAR=0.0
108 4 my ( $self, $key, $query ) = @_;
109 4 $self->{$key} = $query;
110 4 return $query;
111 }
112  
113 2 ▶ sub remove {
Subroutine remove: called CC=1 SCAR=0.0
114 2 my ( $self, $key ) = @_;
115 2 delete $self->{$key};
116 }
117  
118 1 ▶ sub clear {
Subroutine clear: called CC=1 SCAR=0.0
119 1 my $self = shift;
120 1 undef %$self;
121 1 return 1;
122 }
123 }
124  
125 1 1;
126  
127 __END__
« CannedQuery.pm ↑ Summary