File Coverage

blib/lib/Cache/MemoryBackend.pm
Criterion Covered Total %
statement 32 36 88.9
branch 2 4 50.0
condition 1 3 33.3
subroutine 12 13 92.3
pod 0 8 0.0
total 47 64 73.4


line stmt bran cond sub pod time code
1             ######################################################################
2             # $Id: MemoryBackend.pm,v 1.11 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::MemoryBackend;
12              
13 2     2   32 use strict;
  2         30  
  2         32  
14 2     2   31 use Cache::CacheUtils qw( Clone_Data );
  2         20  
  2         36  
15              
16              
17             my $Store_Ref = { };
18              
19              
20             sub new
21             {
22 66     66 0 2678   my ( $proto ) = @_;
23 66   33     951   my $class = ref( $proto ) || $proto;
24 66         1784   my $self = {};
25 66         704   $self = bless( $self, $class );
26 66         685   $self->_initialize_memory_backend( );
27 66         790   return $self;
28             }
29              
30              
31             sub delete_key
32             {
33 27     27 0 264   my ( $self, $p_namespace, $p_key ) = @_;
34              
35 27         854   delete $self->_get_store_ref( )->{ $p_namespace }{ $p_key };
36             }
37              
38              
39             sub delete_namespace
40             {
41 23     23 0 210   my ( $self, $p_namespace ) = @_;
42              
43 23         210   delete $self->_get_store_ref( )->{ $p_namespace };
44             }
45              
46              
47             sub get_keys
48             {
49 45     45 0 439   my ( $self, $p_namespace ) = @_;
50              
51 45         360   return keys %{ $self->_get_store_ref( )->{ $p_namespace } };
  45         490  
52             }
53              
54              
55             sub get_namespaces
56             {
57 36     36 0 341   my ( $self ) = @_;
58              
59 36         314   return keys %{ $self->_get_store_ref( ) };
  36         326  
60             }
61              
62              
63             sub get_size
64             {
65 119     119 0 1410   my ( $self, $p_namespace, $p_key ) = @_;
66              
67 119 50       1291   if ( exists $self->_get_store_ref( )->{ $p_namespace }{ $p_key } )
68               {
69 119         1093     return length $self->_get_store_ref( )->{ $p_namespace }{ $p_key };
70               }
71               else
72               {
73 0         0     return 0;
74               }
75             }
76              
77              
78             sub restore
79             {
80 117     117 0 1070   my ( $self, $p_namespace, $p_key ) = @_;
81              
82 117         1099   return Clone_Data( $self->_get_store_ref( )->{ $p_namespace }{ $p_key } );
83             }
84              
85              
86             sub store
87             {
88 86     86 0 848   my ( $self, $p_namespace, $p_key, $p_data ) = @_;
89              
90 86         977   $self->_get_store_ref( )->{ $p_namespace }{ $p_key } = $p_data;
91             }
92              
93              
94             sub _initialize_memory_backend
95             {
96 66     66   598   my ( $self ) = @_;
97              
98 66 50       618   if ( not defined $self->_get_store_ref( ) )
99               {
100 0         0     $self->_set_store_ref( { } );
101               }
102             }
103              
104              
105             sub _get_store_ref
106             {
107 638     638   13506   return $Store_Ref;
108             }
109              
110              
111             sub _set_store_ref
112             {
113 0     0       my ( $self, $p_store_ref ) = @_;
114              
115 0             $Store_Ref = $p_store_ref;
116             }
117              
118              
119              
120             1;
121              
122             __END__
123            
124             =pod
125            
126             =head1 NAME
127            
128             Cache::MemoryBackend -- a memory based persistance mechanism
129            
130             =head1 DESCRIPTION
131            
132             The MemoryBackend class is used to persist data to memory
133            
134             =head1 SYNOPSIS
135            
136             my $backend = new Cache::MemoryBackend( );
137            
138             See Cache::Backend for the usage synopsis.
139            
140             =head1 METHODS
141            
142             See Cache::Backend for the API documentation.
143            
144             =head1 SEE ALSO
145            
146             Cache::Backend, Cache::FileBackend, Cache::ShareMemoryBackend
147            
148             =head1 AUTHOR
149            
150             Original author: DeWitt Clinton <dewitt@unto.net>
151            
152             Last author: $Author: dclinton $
153            
154             Copyright (C) 2001-2003 DeWitt Clinton
155            
156             =cut
157            
158