1######################################################################
2# $Id: MemoryBackend.pm,v 1.10 2003/01/16 18:10:16 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
11package Cache::MemoryBackend;
12
13use strict;
14use Cache::CacheUtils qw( Clone_Data );
15
16
17my $Store_Ref = { };
18
19
20sub new
21{
22  my ( $proto ) = @_;
23  my $class = ref( $proto ) || $proto;
24  my $self  = {};
25  $self = bless( $self, $class );
26  $self->_initialize_memory_backend( );
27  return $self;
28}
29
30
31sub delete_key
32{
33  my ( $self, $p_namespace, $p_key ) = @_;
34
35  delete $self->_get_store_ref( )->{ $p_namespace }{ $p_key };
36}
37
38
39sub delete_namespace
40{
41  my ( $self, $p_namespace ) = @_;
42
43  delete $self->_get_store_ref( )->{ $p_namespace };
44}
45
46
47sub get_keys
48{
49  my ( $self, $p_namespace ) = @_;
50
51  return keys %{ $self->_get_store_ref( )->{ $p_namespace } };
52}
53
54
55sub get_namespaces
56{
57  my ( $self ) = @_;
58
59  return keys %{ $self->_get_store_ref( ) };
60}
61
62
63sub get_size
64{
65  my ( $self, $p_namespace, $p_key ) = @_;
66
67  if ( exists $self->_get_store_ref( )->{ $p_namespace }{ $p_key } )
68  {
69    return length $self->_get_store_ref( )->{ $p_namespace }{ $p_key };
70  }
71  else
72  {
73    return 0;
74  }
75}
76
77
78sub restore
79{
80  my ( $self, $p_namespace, $p_key ) = @_;
81
82  return Clone_Data( $self->_get_store_ref( )->{ $p_namespace }{ $p_key } );
83}
84
85
86sub store
87{
88  my ( $self, $p_namespace, $p_key, $p_data ) = @_;
89
90  $self->_get_store_ref( )->{ $p_namespace }{ $p_key } = $p_data;
91}
92
93
94sub _initialize_memory_backend
95{
96  my ( $self ) = @_;
97
98  if ( not defined $self->_get_store_ref( ) )
99  {
100    $self->_set_store_ref( { } );
101  }
102}
103
104
105sub _get_store_ref
106{
107  return $Store_Ref;
108}
109
110
111sub _set_store_ref
112{
113  my ( $self, $p_store_ref ) = @_;
114
115  $Store_Ref = $p_store_ref;
116}
117
118
119
1201;
121
122__END__
123
124=pod
125
126=head1 NAME
127
128Cache::MemoryBackend -- a memory based persistence mechanism
129
130=head1 DESCRIPTION
131
132The 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
142See Cache::Backend for the API documentation.
143
144=head1 SEE ALSO
145
146Cache::Backend, Cache::FileBackend, Cache::ShareMemoryBackend
147
148=head1 AUTHOR
149
150Original author: DeWitt Clinton <dewitt@unto.net>
151
152Last author:     $Author: dclinton $
153
154Copyright (C) 2001-2003 DeWitt Clinton
155
156=cut
157
158