OpenInteract2/Cache.pm

filter: Statementstmt 15.1% 15 / 99 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 25.0% 4 / 16 Podpod 0.0% 0 / 10 totaltotal 7.7% 19 / 244 CC 44 SCAR 47.3
CC
44
Cov
8%
get
272.0
set
110.0
clear
90.0
CRAP 113.8
« OpenInteract2/CreateSecurity.pm ↑ Summary OpenInteract2/Controller/MainTemplate.pm »
1 package OpenInteract2::Cache;
2  
3 # $Id: Cache.pm,v 1.13 2005/03/18 04:09:48 lachoy Exp $
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 $OpenInteract2::Cache::VERSION = sprintf("%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/);
11  
12 # Returns: caching object (implementation-neutral)
13  
14 2 my ( $log );
15  
16 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
17 0 my ( $pkg, $conf ) = @_;
18 0 ▶ my $class = ref $pkg || $pkg;
Condition0%0/3
ref $pkg || $pkg
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table0%0/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
19 0 ▶ $conf ||= {};
Condition0%0/2
$conf ||= {}
l!l
00
MC/DC0%0/1
$conf ||= {}
$conf
Truth table0%0/2
A
A= $conf
Aresult
11
00
20 0 ▶ $log ||= get_logger( LOG_CACHE );
Condition0%0/3
$log ||= get_logger("OI2.CACHE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.CACHE")
$log get_logger("OI2.CACHE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.CACHE")
ABresult
1X1
011
000
21 0 ▶ if ( $log->is_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log->is_info) 0 0 0
22 0 $log->info( "Instantiating new cache of class '$class'" );
23 0 foreach my $key ( keys %{ $conf } ) {
24 0 $log->info( "...with key '$key' => '$conf->{ $key }'" );
25 }
26 }
27 0 my $self = bless( {}, $class );
28 0 $self->{_cache_object} = $self->initialize( $conf );
29 0 return $self;
30 }
31  
32  
33 # Returns: data from the cache
34  
35 0 ▶sub get {
Subroutine get: not called CC=16 SCAR=56.1
Pod undocumented
36 0 my ( $self, $p ) = @_;
37 0 ▶ $log ||= get_logger( LOG_CACHE );
Condition0%0/3
$log ||= get_logger("OI2.CACHE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.CACHE")
$log get_logger("OI2.CACHE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.CACHE")
ABresult
1X1
011
000
38  
39 # if the cache hasn't been initialized, bail
40  
41 0 ▶ unless ( $self->{_cache_object} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_cache_object"}) 0 0 0
42 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Object from cache requested, cache object not created") 0 0 0
43 $log->info( "Object from cache requested, cache object not created" );
44 0 return undef;
45 }
46  
47 0 my $key = $p->{key};
48 0 my $is_object = 0;
49 0 my $obj_class = undef;
50 0 ▶ if ( ! $key and $p->{class} and $p->{object_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $key and $p->{"class"} and $p->{"object_id"}) 0 0 0
Condition0%0/6
not $key and $p->{"class"} and $p->{"object_id"}
!ll&&!rl&&r
000
not $key and $p->{"class"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $key and $p->{"class"} and $p->{"object_id"}
not $key $p->{"class"} $p->{"object_id"}
Truth table0%0/4
not(A) and B and C
A= not $keyB= $p->{"class"}C= $p->{"object_id"}
ABCresult
0XX0
10X0
1100
1111
51 0 $key = _make_spops_idx( $p->{class}, $p->{object_id} );
52 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Created class+id key [$key]") 0 0 0
53 $log->debug( "Created class+id key [$key]" );
54 0 $obj_class = $p->{class};
55 0 $is_object++;
56 0 ▶ return undef unless ( $obj_class->pre_cache_get( $p->{object_id} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $obj_class->pre_cache_get($p->{"object_id"}) 0 0 0
57 }
58 0 ▶ unless ( $key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 0 0
59 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Cache MISS (no key)") 0 0 0
60 $log->debug( "Cache MISS (no key)" );
61 0 return undef;
62 }
63  
64 0 my $data = $self->get_data( $self->{_cache_object}, $key );
65 0 ▶ unless ( $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
66 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Cache MISS [$key]") 0 0 0
67 $log->debug( "Cache MISS [$key]" );
68 0 return undef;
69 }
70  
71 $log->is_debug &&
72 0 ▶ $log->debug( "Cache HIT [$key]" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Cache HIT [$key]") 0 0 0
73 0 ▶ if ( $is_object ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_object) 0 0 0
74 0 ▶ return undef unless ( $obj_class->post_cache_get( $data ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $obj_class->post_cache_get($data) 0 0 0
75 }
76 0 return $data;
77 }
78  
79 0 ▶sub set {
Subroutine set: not called CC=10 SCAR=47.0
Pod undocumented
80 0 my ( $self, $p ) = @_;
81 0 ▶ $log ||= get_logger( LOG_CACHE );
Condition0%0/3
$log ||= get_logger("OI2.CACHE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.CACHE")
$log get_logger("OI2.CACHE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.CACHE")
ABresult
1X1
011
000
82  
83 # if the cache hasn't been initialized, bail
84  
85 0 ▶ unless ( $self->{_cache_object} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_cache_object"}) 0 0 0
86 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Request to cache object, cache object not created") 0 0 0
87 $log->info( "Request to cache object, cache object not created" );
88 0 return undef;
89 }
90  
91  
92 0 my $is_object = 0;
93 0 my $key = $p->{key};
94 0 my $data = $p->{data};
95 0 my ( $obj );
96 0 ▶ if ( _is_object( $data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_object($data)) 0 0 0
97 0 $obj = $data;
98 0 $key = _make_spops_idx( ref $obj, $obj->id );
99 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Created class+id key [$key]") 0 0 0
100 $log->debug( "Created class+id key [$key]" );
101 0 $is_object++;
102 0 ▶ return undef unless ( $obj->pre_cache_save );
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->pre_cache_save 0 0 0
103 0 $data = $obj->as_data_only;
104 }
105 0 $self->set_data( $self->{_cache_object}, $key, $data, $p->{expire} );
106 0 ▶ if ( $obj and $obj->can( 'post_cache_save' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj and $obj->can("post_cache_save")) 0 0 0
Condition0%0/3
$obj and $obj->can("post_cache_save")
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and $obj->can("post_cache_save")
$obj $obj->can("post_cache_save")
Truth table0%0/3
A and B
A= $objB= $obj->can("post_cache_save")
ABresult
0X0
100
111
107 0 ▶ return undef if ( $obj->post_cache_save );
Branch: 1 branch
BranchTrueFalseTotal
if $obj->post_cache_save 0 0 0
108 }
109 0 return 1;
110 }
111  
112 0 ▶sub clear {
Subroutine clear: not called CC=9 SCAR=45.0
Pod undocumented
113 0 my ( $self, $p ) = @_;
114 0 ▶ $log ||= get_logger( LOG_CACHE );
Condition0%0/3
$log ||= get_logger("OI2.CACHE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.CACHE")
$log get_logger("OI2.CACHE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.CACHE")
ABresult
1X1
011
000
115  
116 # if the cache hasn't been initialized, bail
117 0 ▶ return undef unless ( $self->{_cache_object} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_cache_object"} 0 0 0
118  
119 0 my $key = $p->{key};
120 0 ▶ if ( ! $key and _is_object( $p->{data} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $key and _is_object($p->{'data'})) { } 0 0 0
elsif (not $key and $p->{'class'} and $p->{'object_id'}) { } 0 0 0
Condition0%0/3
not $key and _is_object($p->{"data"})
!ll&&!rl&&r
000
MC/DC0%0/2
not $key and _is_object($p->{"data"})
not $key _is_object($p->{"data"})
Truth table0%0/3
not(A) and B
A= not $keyB= _is_object($p->{"data"})
ABresult
0X0
100
111
121 0 $key = _make_spops_idx( ref $p->{data}, $p->{data}->id );
122 }
123 ▶ elsif ( ! $key and $p->{class} and $p->{object_id} ) {
Condition0%0/6
not $key and $p->{"class"} and $p->{"object_id"}
!ll&&!rl&&r
000
not $key and $p->{"class"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $key and $p->{"class"} and $p->{"object_id"}
not $key $p->{"class"} $p->{"object_id"}
Truth table0%0/4
not(A) and B and C
A= not $keyB= $p->{"class"}C= $p->{"object_id"}
ABCresult
0XX0
10X0
1100
1111
124 0 $key = _make_spops_idx( $p->{class}, $p->{object_id} );
125 }
126 $log->is_debug &&
127 0 ▶ $log->debug( "Trying to clear cache of [$key]" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Trying to clear cache of [$key]") 0 0 0
128 0 return $self->clear_data( $self->{_cache_object}, $key );
129 }
130  
131  
132 0 ▶sub purge {
Subroutine purge: not called CC=5 SCAR=34.0
Pod undocumented
133 0 my ( $self ) = @_;
134 0 ▶ $log ||= get_logger( LOG_CACHE );
Condition0%0/3
$log ||= get_logger("OI2.CACHE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.CACHE")
$log get_logger("OI2.CACHE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.CACHE")
ABresult
1X1
011
000
135  
136 # if the cache hasn't been initialized, bail
137  
138 0 ▶ unless ( $self->{_cache_object} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_cache_object"}) 0 0 0
139 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Purge of cache requested, cache object not created") 0 0 0
140 $log->info( "Purge of cache requested, cache object not created" );
141 0 return undef;
142 }
143  
144 $log->is_info &&
145 0 ▶ $log->info( "Trying to purge cache of all objects" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Trying to purge cache of all objects") 0 0 0
146 0 return $self->purge_all( $self->{_cache_object} );
147 }
148  
149  
150 0 ▶sub _is_object {
Subroutine _is_object: not called CC=3 SCAR=24.8
151 0 my ( $item ) = @_;
152 0 my $typeof = ref $item;
153 0 ▶ return undef if ( ! $typeof );
Branch: 1 branch
BranchTrueFalseTotal
unless $typeof 0 0 0
154 0 ▶ return undef if ( $typeof =~ /^(HASH|ARRAY|SCALAR)$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $typeof =~ /^(HASH|ARRAY|SCALAR)$/ 0 0 0
155 0 return 1;
156 }
157  
158 0 ▶sub _make_spops_idx {
Subroutine _make_spops_idx: not called CC=1 SCAR=6.9
159 0 return join '--', $_[0], $_[1];
160 }
161  
162 ########################################
163 # SUBCLASS TO OVERRIDE
164  
165 0 ▶sub initialize { die "Subclass must define initialize()\n" }
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
166 0 ▶sub get_data { die "Subclass must define get_data()\n" }
Subroutine get_data: not called CC=1 SCAR=6.9
Pod undocumented
167 0 ▶sub set_data { die "Subclass must define set_data()\n" }
Subroutine set_data: not called CC=1 SCAR=6.9
Pod undocumented
168 0 ▶sub clear_data { die "Subclass must define clear_data()\n" }
Subroutine clear_data: not called CC=1 SCAR=6.9
Pod undocumented
169 0 ▶sub purge_all { die "Subclass must define purge_all()\n" }
Subroutine purge_all: not called CC=1 SCAR=6.9
Pod undocumented
170  
171 2 1;
172  
173 __END__
« OpenInteract2/CreateSecurity.pm ↑ Summary OpenInteract2/Controller/MainTemplate.pm »