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 / SharedMemoryBackend.pm < prev    next >
Encoding:
Perl POD Document  |  2003-04-15  |  4.6 KB  |  226 lines

  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.   return new IPC::ShareLite( %ipc_options );
  86. }
  87.  
  88.  
  89. # this method uses the shared created by Instantiate_Share to
  90. # transparently retrieve a reference to a shared hash structure
  91.  
  92. sub _Restore_Shared_Hash_Ref
  93. {
  94.   my ( $p_ipc_identifier ) = Static_Params( @_ );
  95.  
  96.   Assert_Defined( $p_ipc_identifier );
  97.  
  98.   my $frozen_hash_ref = _Instantiate_Share( $p_ipc_identifier )->fetch( ) or
  99.     return { };
  100.  
  101.   return Thaw_Data( $frozen_hash_ref );
  102. }
  103.  
  104.  
  105. # this method uses the shared created by Instantiate_Share to
  106. # transparently retrieve a reference to a shared hash structure, and
  107. # additionally exlusively locks the share
  108.  
  109. sub _Restore_Shared_Hash_Ref_With_Lock
  110. {
  111.   my ( $p_ipc_identifier ) = Static_Params( @_ );
  112.  
  113.   Assert_Defined( $p_ipc_identifier );
  114.  
  115.   my $share = _Instantiate_Share( $p_ipc_identifier );
  116.  
  117.   $share->lock( LOCK_EX );
  118.  
  119.   my $frozen_hash_ref = $share->fetch( ) or
  120.     return { };
  121.  
  122.   return Thaw_Data( $frozen_hash_ref );
  123. }
  124.  
  125.  
  126. # this method uses the shared created by Instantiate_Share to
  127. # transparently persist a reference to a shared hash structure
  128.  
  129. sub _Store_Shared_Hash_Ref
  130. {
  131.   my ( $p_ipc_identifier, $p_hash_ref ) = @_;
  132.  
  133.   Assert_Defined( $p_ipc_identifier );
  134.   Assert_Defined( $p_hash_ref );
  135.  
  136.   _Instantiate_Share( $p_ipc_identifier )->store( Freeze_Data( $p_hash_ref ) );
  137. }
  138.  
  139.  
  140. # this method uses the shared created by Instantiate_Share to
  141. # transparently persist a reference to a shared hash structure and
  142. # additionally unlocks the share
  143.  
  144. sub _Store_Shared_Hash_Ref_And_Unlock
  145. {
  146.   my ( $p_ipc_identifier, $p_hash_ref ) = @_;
  147.  
  148.   Assert_Defined( $p_ipc_identifier );
  149.   Assert_Defined( $p_hash_ref );
  150.  
  151.   my $share = _Instantiate_Share( $p_ipc_identifier );
  152.  
  153.   $share->store( Freeze_Data( $p_hash_ref ) );
  154.  
  155.   $share->unlock( LOCK_UN );
  156. }
  157.  
  158.  
  159. sub _get_locked_store_ref
  160. {
  161.   return _Restore_Shared_Hash_Ref_With_Lock( $IPC_IDENTIFIER );
  162. }
  163.  
  164.  
  165. sub _set_locked_store_ref
  166. {
  167.   my ( $self, $p_store_ref ) = @_;
  168.  
  169.   _Store_Shared_Hash_Ref_And_Unlock( $IPC_IDENTIFIER, $p_store_ref );
  170. }
  171.  
  172.  
  173. sub _get_store_ref
  174. {
  175.   return _Restore_Shared_Hash_Ref( $IPC_IDENTIFIER );
  176. }
  177.  
  178.  
  179. sub _set_store_ref
  180. {
  181.   my ( $self, $p_store_ref ) = @_;
  182.  
  183.   _Store_Shared_Hash_Ref( $IPC_IDENTIFIER, $p_store_ref );
  184. }
  185.  
  186.  
  187.  
  188. 1;
  189.  
  190.  
  191. __END__
  192.  
  193. =pod
  194.  
  195. =head1 NAME
  196.  
  197. Cache::SharedMemoryBackend -- a shared memory based persistance mechanism
  198.  
  199. =head1 DESCRIPTION
  200.  
  201. The SharedMemoryBackend class is used to persist data to shared memory
  202.  
  203. =head1 SYNOPSIS
  204.  
  205.   my $backend = new Cache::SharedMemoryBackend( );
  206.  
  207.   See Cache::Backend for the usage synopsis.
  208.  
  209. =head1 METHODS
  210.  
  211. See Cache::Backend for the API documentation.
  212.  
  213. =head1 SEE ALSO
  214.  
  215. Cache::Backend, Cache::FileBackend, Cache::ShareMemoryBackend
  216.  
  217. =head1 AUTHOR
  218.  
  219. Original author: DeWitt Clinton <dewitt@unto.net>
  220.  
  221. Last author:     $Author: dclinton $
  222.  
  223. Copyright (C) 2001-2003 DeWitt Clinton
  224.  
  225. =cut
  226.