SharedMemoryBackend.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran n/a Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 105 SCAR 6.9
CC
1
Cov
0%
new
2.0
delete_key
2.0
delete_namespace
2.0
CRAP 2.0
1 ######################################################################
2 # $Id: SharedMemoryBackend.pm,v 1.7 2003/04/15 14:46:23 dclinton Exp $
3 # Copyright (C) 2001-2003 DeWitt Clinton All Rights Reserved
4 #
5 # Software distributed under the License is distributed on an "AS
6 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or
7 # implied. See the License for the specific language governing
8 # rights and limitations under the License.
9 ######################################################################
10  
11 package Cache::SharedMemoryBackend;
12  
13 use strict;
14 use Cache::CacheUtils qw( Assert_Defined Freeze_Data Static_Params Thaw_Data );
15 use Cache::MemoryBackend;
16 use IPC::ShareLite qw( LOCK_EX LOCK_UN );
17  
18 use vars qw( @ISA );
19  
20 @ISA = qw ( Cache::MemoryBackend );
21  
22  
23 my $IPC_IDENTIFIER = 'ipcc';
24  
25  
26 sub new
27 {
28 my ( $proto ) = @_;
29 my $class = ref( $proto ) || $proto;
30 return $class->SUPER::new( );
31 }
32  
33  
34 sub delete_key
35 {
36 my ( $self, $p_namespace, $p_key ) = @_;
37  
38 my $store_ref = $self->_get_locked_store_ref( );
39  
40 delete $store_ref->{ $p_namespace }{ $p_key };
41  
42 $self->_set_locked_store_ref( $store_ref );
43 }
44  
45  
46 sub delete_namespace
47 {
48 my ( $self, $p_namespace ) = @_;
49  
50 my $store_ref = $self->_get_locked_store_ref( );
51  
52 delete $store_ref->{ $p_namespace };
53  
54 $self->_set_locked_store_ref( $store_ref );
55 }
56  
57  
58 sub store
59 {
60 my ( $self, $p_namespace, $p_key, $p_data ) = @_;
61  
62 my $store_ref = $self->_get_locked_store_ref( );
63  
64 $store_ref->{ $p_namespace }{ $p_key } = $p_data;
65  
66 $self->_set_locked_store_ref( $store_ref );
67 }
68  
69  
70 # create a IPC::ShareLite share under the ipc_identifier
71  
72 sub _Instantiate_Share
73 {
74 my ( $p_ipc_identifier ) = Static_Params( @_ );
75  
76 Assert_Defined( $p_ipc_identifier );
77  
78 my %ipc_options = (
79 -key => $p_ipc_identifier,
80 -create => 'yes',
81 -destroy => 'no',
82 -exclusive => 'no'
83 );
84  
85 my $share = new IPC::ShareLite( %ipc_options ) or
86 throw Error::Simple( "Couldn't instantiate IPC::ShareLite: $!" );
87  
88 return $share;
89 }
90  
91  
92 # this method uses the shared created by Instantiate_Share to
93 # transparently retrieve a reference to a shared hash structure
94  
95 sub _Restore_Shared_Hash_Ref
96 {
97 my ( $p_ipc_identifier ) = Static_Params( @_ );
98  
99 Assert_Defined( $p_ipc_identifier );
100  
101 my $frozen_hash_ref = _Instantiate_Share( $p_ipc_identifier )->fetch( ) or
102 return { };
103  
104 return Thaw_Data( $frozen_hash_ref );
105 }
106  
107  
108 # this method uses the shared created by Instantiate_Share to
109 # transparently retrieve a reference to a shared hash structure, and
110 # additionally exlusively locks the share
111  
112 sub _Restore_Shared_Hash_Ref_With_Lock
113 {
114 my ( $p_ipc_identifier ) = Static_Params( @_ );
115  
116 Assert_Defined( $p_ipc_identifier );
117  
118 my $share = _Instantiate_Share( $p_ipc_identifier );
119  
120 $share->lock( LOCK_EX );
121  
122 my $frozen_hash_ref = $share->fetch( ) or
123 return { };
124  
125 return Thaw_Data( $frozen_hash_ref );
126 }
127  
128  
129 # this method uses the shared created by Instantiate_Share to
130 # transparently persist a reference to a shared hash structure
131  
132 sub _Store_Shared_Hash_Ref
133 {
134 my ( $p_ipc_identifier, $p_hash_ref ) = @_;
135  
136 Assert_Defined( $p_ipc_identifier );
137 Assert_Defined( $p_hash_ref );
138  
139 _Instantiate_Share( $p_ipc_identifier )->store( Freeze_Data( $p_hash_ref ) );
140 }
141  
142  
143 # this method uses the shared created by Instantiate_Share to
144 # transparently persist a reference to a shared hash structure and
145 # additionally unlocks the share
146  
147 sub _Store_Shared_Hash_Ref_And_Unlock
148 {
149 my ( $p_ipc_identifier, $p_hash_ref ) = @_;
150  
151 Assert_Defined( $p_ipc_identifier );
152 Assert_Defined( $p_hash_ref );
153  
154 my $share = _Instantiate_Share( $p_ipc_identifier );
155  
156 $share->store( Freeze_Data( $p_hash_ref ) );
157  
158 $share->unlock( LOCK_UN );
159 }
160  
161  
162 sub _get_locked_store_ref
163 {
164 return _Restore_Shared_Hash_Ref_With_Lock( $IPC_IDENTIFIER );
165 }
166  
167  
168 sub _set_locked_store_ref
169 {
170 my ( $self, $p_store_ref ) = @_;
171  
172 _Store_Shared_Hash_Ref_And_Unlock( $IPC_IDENTIFIER, $p_store_ref );
173 }
174  
175  
176 sub _get_store_ref
177 {
178 return _Restore_Shared_Hash_Ref( $IPC_IDENTIFIER );
179 }
180  
181  
182 sub _set_store_ref
183 {
184 my ( $self, $p_store_ref ) = @_;
185  
186 _Store_Shared_Hash_Ref( $IPC_IDENTIFIER, $p_store_ref );
187 }
188  
189  
190  
191 1;
192  
193  
194 __END__