blib/lib/Cache/Memcached/Fast/Safe.pm

filter: Statementstmt 45.9% 56 / 122 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.0% 12 / 20 Podpod 100% 2 / 2 totaltotal 43.4% 70 / 161 SCAR 38.7
CC
13
Cov
41%
__ANON__
42.0
__ANON__
20.0
get_or_set
12.0
CRAP 47.9
↑ Summary Build »
1 package Cache::Memcached::Fast::Safe;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Cache::Memcached::Fast 0.19;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use URI::Escape::XS qw/uri_escape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Digest::SHA qw/sha1_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use parent qw/Cache::Memcached::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use POSIX::AtFork;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.04';
13 our $SANITIZE_METHOD = sub {
14 0 ▶ my $key = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
15 0 $key = uri_escape($key,"\x00-\x20\x7f-\xff");
16 0 ▶ if ( length $key > 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key > 200) 0 0 0
17 0 $key = sha1_hex($key);
18 }
19 0 $key;
20 3 };
21  
22 sub new {
23 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
24 0 ▶ my %args = ref $_[0] ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
25 0 my $mem = $class->SUPER::new(\%args);
26 # fork safe
27 0 weaken(my $mem_weaken = $mem);
28 POSIX::AtFork->add_to_child(sub {
29 0 ▶ eval { $mem_weaken->disconnect_all };
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 });
31 0 $mem;
32 }
33  
34 3 for my $method ( qw/set cas add replace append prepend incr decr delete/ ) {
35 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 27 my $super = 'SUPER::'.$method;
37 27 *{$method} = sub {
38 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 my $key = shift;
40 0 $self->$super($SANITIZE_METHOD->($key), @_);
41 27 };
42 }
43 3 for my $method (qw/set_multi cas_multi add_multi replace_multi append_multi prepend_multi incr_multi decr_multi delete_multi/ ) {
44 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 27 my $super = 'SUPER::'.$method;
46 27 *{$method} = sub {
47 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
48 0 my @request = @_;
49 0 my @request_keys;
50 my %sanitized_keys;
51 0 my @sanitized_request;
52 0 for my $keyval (@request) {
53 0 my $key;
54 my $sanitized_key;
55 0 my $sanitized_keyval;
56 0 ▶ if ( ref $keyval ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $keyval) { } 0 0 0
57 0 my @keyval = @$keyval;
58 0 $key = shift @keyval;
59 0 $sanitized_key = $SANITIZE_METHOD->($key);
60 0 $sanitized_keyval = [$sanitized_key, @keyval];
61 }
62 else {
63 0 $key = $keyval;
64 0 $sanitized_key = $SANITIZE_METHOD->($key);
65 0 $sanitized_keyval = $sanitized_key
66 }
67 0 $sanitized_keys{$sanitized_key} = $key;
68 0 push @request_keys, $key;
69 0 push @sanitized_request, $sanitized_keyval;
70 }
71 0 my $sanitized_result = $self->$super(@sanitized_request);
72 0 my %result;
73 0 for my $key ( keys %$sanitized_result ) {
74 0 $result{$sanitized_keys{$key}} = $sanitized_result->{$key};
75 }
76 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
77 0 my @result;
78 0 for my $key ( @request_keys ) {
79 0 push @result, $result{$key};
80 }
81 0 return @result;
82 }
83 0 \%result;
84 }
85 27 }
86  
87 3 *remove = \&delete;
88  
89 3 for my $method (qw/get gets/) {
90 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 6 my $super = 'SUPER::'.$method;
92 6 *{$method} = sub {
93 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 0 my $key = shift;
95 0 $self->$super($SANITIZE_METHOD->($key));
96 6 };
97 }
98 3 for my $method (qw/get_multi gets_multi/) {
99 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 6 my $super = 'SUPER::'.$method;
101 6 *{$method} = sub {
102 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
103 0 my @request;
104 my %sanitized_keys;
105 0 for my $key (@_) {
106 0 my $sanitized_key = $SANITIZE_METHOD->($key);
107 0 $sanitized_keys{$sanitized_key} = $key;
108 0 push @request, $sanitized_key;
109 }
110 0 ▶ return {} if ! @request;
Branch: 1 branch
BranchTrueFalseTotal
unless @request 0 0 0
111 0 my $sanitized_result = $self->$super(@request);
112 0 my %result;
113 0 for my $key ( keys %$sanitized_result ) {
114 0 $result{$sanitized_keys{$key}} = $sanitized_result->{$key};
115 }
116 0 \%result;
117 }
118 6 }
119  
120 sub get_or_set {
121 0 ▶ my($self, $key, $sub, $expire) = @_;
Subroutine get_or_set: not called CC=3 SCAR=24.8
Pod documented
122 0 ▶ if (my $value = $self->get($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $self->get($key)) 0 0 0
123 0 return $value;
124 }
125 0 my ($value, $ret_expire) = $sub->();
126 0 ▶ $self->set($key, $value, $expire || $ret_expire);
Condition0%0/3
$expire || $ret_expire
l!l&&r!l&&!r
000
MC/DC0%0/2
$expire || $ret_expire
$expire $ret_expire
Truth table0%0/3
A || B
A= $expireB= $ret_expire
ABresult
1X1
011
000
127 0 $value;
128 }
129  
130 3 1;
131  
132 __END__
↑ Summary Build »