lib/Mail/Decency/Helper/Cache.pm

filter: Statementstmt 75.2% 67 / 89 Branchbran 53.1% 17 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 71.4% 15 / 21 Podpod 83.3% 5 / 6 totaltotal 69.9% 107 / 153 CC 19 SCAR 15.9
CC
19
Cov
69%
hash_to_name
20.0
entry
6.0
BUILD
5.2
CRAP 4.9
« lib/Mail/Decency/Core/POEFilterSMTP.pm ↑ Summary script/decency-server.pl »
1 package Mail::Decency::Helper::Cache;
2  
3 25 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use Digest::MD5 qw/ md5_hex /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use Scalar::Util qw/ weaken /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Mail::Decency::Helper
14  
15 =head1 DESCRIPTION
16  
17 Helper modules for Decency policies or content filters
18  
19 =cut
20  
21 25 has cache => ( is => 'ro', isa => 'Cache' );
22 25 has writer => ( is => 'rw', isa => 'CodeRef' );
23 25 has reader => ( is => 'rw', isa => 'CodeRef' );
24  
25 2 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.6
Pod undocumented
26 2 my ( $self, $args_ref ) = @_;
27
28 2 ▶ unless ( lc( $args_ref->{ class } ) eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $args_ref->{'class'} eq 'none') { } 0 2 2
29
30 # determine class
31 2 my $cache_class = delete $args_ref->{ class };
32 2 $cache_class = "Cache::$cache_class";
33
34 # load class
35 1 ▶ eval "use $cache_class";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶ die "Could not load cache module $cache_class: $@\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
37 if $@;
38
39 # init cache
40 2 $self->{ cache } = $cache_class->new( %$args_ref );
41
42 # fastmmap, has no timeout for set .. emulate!
43 2 ▶ if ( $cache_class eq 'Cache::FastMmap' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cache_class eq 'Cache::FastMmap') { } 1 1 2
elsif ($self->cache->can('freeze')) { } 1 0 1
44 2 ▶ $self->writer( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 my ( $self, $key, $value, $timeout ) = @_;
46 2 return $self->cache->set( $key => [ $value, $timeout ] );
47 1 } );
48 4 ▶ $self->reader( sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
49 4 my ( $self, $key ) = @_;
50 4 my $res = $self->cache->get( $key );
51 4 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 2 2 4
52 2 my ( $value, $timeout ) = @$res;
53 2 ▶ if ( $timeout && $timeout < time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and $timeout < time) 1 1 2
Condition66%2/3
$timeout and $timeout < time
!ll&&!rl&&r
101
MC/DC50%1/2
$timeout and $timeout < time
$timeout $timeout < time
Truth table66%2/3
A and B
A= $timeoutB= $timeout < time
ABresult
0X0
100
111
54 1 $self->remove( $key );
55 1 return ;
56 }
57 1 return $value;
58 1 } );
59 }
60
61 # those who speek freeze directly
62 elsif ( $self->cache->can( 'freeze' ) ) {
63 2 ▶ $self->writer( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 2 my ( $self, $key, $value, $timeout ) = @_;
65 2 return $self->cache->freeze( $key => $value, $timeout );
66 1 } );
67 4 ▶ $self->reader( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 4 my ( $self, $key ) = @_;
69 4 return $self->cache->thaw( $key );
70 1 } );
71 }
72
73 # ant the rest, freezing/thawing as needed
74 else {
75
76 0 ▶ $self->writer( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 my ( $self, $key, $value, $timeout ) = @_;
78 0 return $self->cache->set( $key => $value, $timeout );
79 0 } );
80 0 ▶ $self->reader( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my ( $self, $key ) = @_;
82 0 return $self->cache->get( $key );
83 0 } );
84 }
85 }
86
87 else {
88 0 ▶ $self->writer( sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 ▶ $self->reader( sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 }
91
92 2 return $self;
93 }
94  
95  
96 =head2 get
97  
98 Read from cache
99  
100 my $ref = $cache->get( "name" );
101  
102 =cut
103  
104 8 ▶sub get {
Subroutine get: called CC=4 SCAR=14.2
Pod documented
105 8 my ( $self, $key ) = @_;
106
107 # no cache ?
108 8 ▶ return unless $self->cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cache 0 8 8
109
110 # get unserialized
111 8 my $res;
112 8 eval {
113 8 $res = $self->reader->( $self, $key );
114 };
115 8 ▶ $res = $self->cache->get( $key ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
116
117 # de-scalar-ref if any ..
118 8 ▶ return ref( $res ) eq 'SCALAR' ? $$res : $res;
Branch: 1 branch
BranchTrueFalseTotal
ref $res eq 'SCALAR' ? : 2 6 8
119
120 0 return $res;
121
122 }
123  
124 =head2 set
125  
126 Write to cache
127  
128 $cache->set( name => { some => [ "data" ] }, "5 s" );
129 $cache->set( name => { some => [ "data" ] }, time() + 5 );
130  
131 =cut
132  
133 4 ▶sub set {
Subroutine set: called CC=4 SCAR=14.9
Pod documented
134 4 my ( $self, $key, $value, $timeout ) = @_;
135
136 # no cache ?
137 4 ▶ return unless $self->cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cache 0 4 4
138
139 # remove from cache /
140 4 ▶ return $self->remove( $key ) unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
141
142 # set data
143 4 ▶ $self->writer->( $self, $key, ref( $value ) ? $value : \$value, $timeout );
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 4 4
144 }
145  
146 =head2 remove
147  
148 Remove a key from cache
149  
150 $cache->remove( "name" );
151  
152 =cut
153  
154 3 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.1
Pod documented
155 3 my ( $self, $key ) = @_;
156 3 ▶ return unless $self->cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cache 0 3 3
157 3 return $self->cache->remove( $key );
158 }
159  
160 =head2 entry
161  
162 Return the Cache::Entry instance
163  
164 my $entry = $cache->entry( "name" );
165  
166 =cut
167  
168 0 ▶sub entry {
Subroutine entry: not called CC=2 SCAR=17.9
Pod documented
169 0 my ( $self, $key ) = @_;
170 0 ▶ return unless $self->cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cache 0 0 0
171 0 return $self->cache->entry( $key );
172 }
173  
174  
175 =head2 hash_to_name
176  
177 Transforms hash keys and values into short md5 string .. to be used for caching
178  
179 =cut
180  
181 0 ▶sub hash_to_name {
Subroutine hash_to_name: not called CC=4 SCAR=30.0
Pod documented
182 0 my ( $self, $hash_ref, @names ) = @_;
183 0 ▶ unless ( @names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@names) { } 0 0 0
184 0 @names = sort keys %$hash_ref;
185 }
186 else {
187 0 @names = sort @names;
188 }
189
190 0 my ( @data, @prefices );
191 0 foreach my $name( @names ) {
192 0 ▶ if ( $hash_ref->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_ref->{$name}) 0 0 0
193 0 push @data, "$name=". ( $hash_ref->{ $name } );
194 0 push @prefices, substr( $hash_ref->{ $name }, 0, 2 );
195 }
196 }
197
198 0 return md5_hex( join( "", @data ) ). "-". join( "", @prefices );
199 }
200  
201  
202  
203  
204 =head1 AUTHOR
205  
206 Ulrich Kautz <uk@fortrabbit.de>
207  
208 =head1 COPYRIGHT
209  
210 Copyright (c) 2010 the L</AUTHOR> as listed above
211  
212 =head1 LICENCSE
213  
214 This library is free software and may be distributed under the same terms as perl itself.
215  
216 =cut
217  
218 25 1;
« lib/Mail/Decency/Core/POEFilterSMTP.pm ↑ Summary script/decency-server.pl »