DRI/Cache.pm

filter: Statementstmt 89.2% 58 / 65 Branchbran 65.0% 26 / 40 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 90.9% 10 / 11 Podpod 50.0% 3 / 6 totaltotal 73.9% 105 / 142 CC 25 SCAR 21.5
CC
25
Cov
77%
get
13.4
set_result_from_cache
12.0
set
9.4
CRAP 8.6
« DRI/Protocol/DAS/AdamsNames/Message.pm ↑ Summary DRI/DRD/IIS.pm »
1 ## Domain Registry Interface, local global cache
2 ##
3 ## Copyright (c) 2005,2008,2009,2011,2013 Patrick Mevzek <netdri@dotandco.com>. All rights reserved.
4 ##
5 ## This file is part of Net::DRI
6 ##
7 ## Net::DRI is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
11 ##
12 ## See the LICENSE file that comes with this distribution for more details.
13 #########################################################################################
14  
15 package Net::DRI::Cache;
16  
17 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 86 ▶use base qw(Class::Accessor::Chained::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 86 __PACKAGE__->mk_accessors(qw/ttl/);
22  
23 86 ▶use Net::DRI::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 86 ▶use Net::DRI::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =pod
27  
28 =head1 NAME
29  
30 Net::DRI::Cache - Local cache for Net::DRI
31  
32 =head1 DESCRIPTION
33  
34 Please see the README file for details.
35  
36 =head1 SUPPORT
37  
38 For now, support questions should be sent to:
39  
40 E<lt>netdri@dotandco.comE<gt>
41  
42 Please also see the SUPPORT file in the distribution.
43  
44 =head1 SEE ALSO
45  
46 E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt>
47  
48 =head1 AUTHOR
49  
50 Patrick Mevzek, E<lt>netdri@dotandco.comE<gt>
51  
52 =head1 COPYRIGHT
53  
54 Copyright (c) 2005,2008,2009,2011,2013 Patrick Mevzek <netdri@dotandco.com>.
55 All rights reserved.
56  
57 This program is free software; you can redistribute it and/or modify
58 it under the terms of the GNU General Public License as published by
59 the Free Software Foundation; either version 2 of the License, or
60 (at your option) any later version.
61  
62 See the LICENSE file that comes with this distribution for more details.
63  
64 =cut
65  
66 ####################################################################################################
67 sub new
68 {
69 81 ▶ my ($c,$ttl)=@_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
70  
71 81 my $self={
72 ttl => $ttl, ## if negative, never use cache
73 data => {},
74 };
75  
76 81 bless($self,$c);
77 81 return $self;
78 }
79  
80 sub set
81 {
82 5 ▶ my ($self,$regname,$type,$key,$data,$ttl)=@_;
Subroutine set: called CC=9 SCAR=22.4
Pod documented
83 5 ▶ Net::DRI::Exception::err_insufficient_parameters() unless Net::DRI::Util::all_valid($regname,$type,$key);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::DRI::Util::all_valid($regname, $type, $key) 0 5 5
84  
85 5 my $now=Net::DRI::Util::microtime();
86 5 ▶ $ttl=$self->{ttl} unless defined($ttl);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ttl 4 1 5
87 5 ▶ my $until=($ttl==0)? 0 : $now+1000000*$ttl;
Branch: 1 branch
BranchTrueFalseTotal
$ttl == 0 ? : 0 5 5
88 5 my %c=(_on => $now,
89 _from => $regname,
90 _until => $until,
91 );
92  
93 5 ▶ if ($data && (ref $data eq 'HASH'))
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data eq "HASH") 5 0 5
Condition33%1/3
$data and ref $data eq "HASH"
!ll&&!rl&&r
005
MC/DC0%0/2
$data and ref $data eq "HASH"
$data ref $data eq "HASH"
Truth table33%1/3
A and B
A= $dataB= ref $data eq "HASH"
ABresult
0X0
100
111
94 {
95 5 ▶ while(my ($k,$v)=each(%$data))
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 5 5 10
96 {
97 5 $c{$k}=$v;
98 }
99 }
100  
101 5 ▶ if ($self->{ttl} >= 0) ## we really store something
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ttl"} >= 0) 4 1 5
102 {
103 4 ▶ $self->{data}->{$type}={} unless exists $self->{data}->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$type} 2 2 4
104 ## We store only the last version of a given key, so start from scratch
105 4 $self->{data}->{$type}->{$key}=\%c;
106 }
107  
108 5 return \%c;
109 }
110  
111 sub set_result_from_cache
112 {
113 0 ▶ my ($self,$type,$key)=@_;
Subroutine set_result_from_cache: not called CC=3 SCAR=24.8
Pod undocumented
114 0 ▶ Net::DRI::Exception::err_insufficient_parameters() unless Net::DRI::Util::all_valid($type,$key);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::DRI::Util::all_valid($type, $key) 0 0 0
115 0 ▶ return unless exists $self->{data}->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$type} 0 0 0
116 0 $self->{data}->{$type}->{$key}->{result_from_cache}=1;
117 0 return;
118 }
119  
120 sub get
121 {
122 5 ▶ my ($self,$type,$key,$data,$from)=@_;
Subroutine get: called CC=11 SCAR=25.9
Pod documented
123  
124 5 ▶ return if ($self->{ttl} < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ttl"} < 0 0 5 5
125 5 ▶ Net::DRI::Exception::err_insufficient_parameters() unless Net::DRI::Util::all_valid($type,$key);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::DRI::Util::all_valid($type, $key) 0 5 5
126 5 ($type,$key)=Net::DRI::Util::normalize_name($type,$key);
127 5 ▶ return unless exists $self->{data}->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$type} 0 5 5
128 5 ▶ return unless exists $self->{data}->{$type}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$type}{$key} 0 5 5
129  
130 5 my $c=$self->{data}->{$type}->{$key};
131  
132 5 ▶ if ($c->{_until} > 0 && (Net::DRI::Util::microtime() > $c->{_until}))
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"_until"} > 0 and Net::DRI::Util::microtime() > $c->{"_until"}) 1 4 5
Condition66%2/3
$c->{"_until"} > 0 and Net::DRI::Util::microtime() > $c->{"_until"}
!ll&&!rl&&r
041
MC/DC50%1/2
$c->{"_until"} > 0 and Net::DRI::Util::microtime() > $c->{"_until"}
$c->{"_until"} > 0 Net::DRI::Util::microtime() > $c->{"_until"}
Truth table66%2/3
A and B
A= $c->{"_until"} > 0B= Net::DRI::Util::microtime() > $c->{"_until"}
ABresult
0X0
100
111
133 {
134 1 delete $self->{data}->{$type}->{$key};
135 1 return;
136 }
137  
138 4 ▶ return if (defined $from && ($c->{_from} ne $from));
Branch: 1 branch
BranchTrueFalseTotal
if defined $from and $c->{"_from"} ne $from 1 3 4
Condition66%2/3
defined $from and $c->{"_from"} ne $from
!ll&&!rl&&r
031
MC/DC50%1/2
defined $from and $c->{"_from"} ne $from
defined $from $c->{"_from"} ne $from
Truth table66%2/3
A and B
A= defined $fromB= $c->{"_from"} ne $from
ABresult
0X0
100
111
139  
140 3 ▶ if (defined $data)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 3 0 3
141 {
142 3 ▶ return $c->{$data} if exists $c->{$data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->{$data} 3 0 3
143 } else
144 {
145 0 return $c;
146 }
147  
148 0 return;
149 }
150  
151 sub delete_expired
152 {
153 1 ▶ my $self=shift;
Subroutine delete_expired: called CC=5 SCAR=16.4
Pod undocumented
154 1 my $now=Net::DRI::Util::microtime();
155 1 my $c=$self->{data};
156 1 ▶ while(my ($type,$c1)=each(%$c))
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$c) 1 1 2
157 {
158 1 ▶ while(my ($key,$c2)=each(%{$c1}))
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$c1;}) 1 1 2
159 {
160 1 ▶ delete $c->{$type}->{$key} if ($c2->{_until} > 0 && ($now > $c2->{_until}));
Branch: 1 branch
BranchTrueFalseTotal
if $c2->{"_until"} > 0 and $now > $c2->{"_until"} 1 0 1
Condition33%1/3
$c2->{"_until"} > 0 and $now > $c2->{"_until"}
!ll&&!rl&&r
001
MC/DC0%0/2
$c2->{"_until"} > 0 and $now > $c2->{"_until"}
$c2->{"_until"} > 0 $now > $c2->{"_until"}
Truth table33%1/3
A and B
A= $c2->{"_until"} > 0B= $now > $c2->{"_until"}
ABresult
0X0
100
111
161 }
162 }
163 1 return;
164 }
165  
166 sub delete ## no critic (Subroutines::ProhibitBuiltinHomonyms)
167 {
168 1 ▶ my $self=shift;
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
169 1 $self->{data}={};
170 1 return;
171 }
172  
173 ####################################################################################################
174 86 1;
« DRI/Protocol/DAS/AdamsNames/Message.pm ↑ Summary DRI/DRD/IIS.pm »