home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / MemoryBackend.pm < prev    next >
Encoding:
Perl POD Document  |  2003-04-15  |  2.6 KB  |  158 lines

  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. use strict;
  14. use Cache::CacheUtils qw( Clone_Data );
  15.  
  16.  
  17. my $Store_Ref = { };
  18.  
  19.  
  20. sub 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.  
  31. sub delete_key
  32. {
  33.   my ( $self, $p_namespace, $p_key ) = @_;
  34.  
  35.   delete $self->_get_store_ref( )->{ $p_namespace }{ $p_key };
  36. }
  37.  
  38.  
  39. sub delete_namespace
  40. {
  41.   my ( $self, $p_namespace ) = @_;
  42.  
  43.   delete $self->_get_store_ref( )->{ $p_namespace };
  44. }
  45.  
  46.  
  47. sub get_keys
  48. {
  49.   my ( $self, $p_namespace ) = @_;
  50.  
  51.   return keys %{ $self->_get_store_ref( )->{ $p_namespace } };
  52. }
  53.  
  54.  
  55. sub get_namespaces
  56. {
  57.   my ( $self ) = @_;
  58.  
  59.   return keys %{ $self->_get_store_ref( ) };
  60. }
  61.  
  62.  
  63. sub 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.  
  78. sub 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.  
  86. sub 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.  
  94. sub _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.  
  105. sub _get_store_ref
  106. {
  107.   return $Store_Ref;
  108. }
  109.  
  110.  
  111. sub _set_store_ref
  112. {
  113.   my ( $self, $p_store_ref ) = @_;
  114.  
  115.   $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.