| 1 | ###################################################################### | ||
| 2 | # $Id: SharedMemoryCache.pm,v 1.24 2004/04/24 15:46:47 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 | |||
| 12 | package Cache::SharedMemoryCache; | ||
| 13 | |||
| 14 | |||
| 15 | use strict; | ||
| 16 | use vars qw( @ISA ); | ||
| 17 | use Cache::Cache; | ||
| 18 | use Cache::MemoryCache; | ||
| 19 | use Cache::CacheUtils qw( Assert_Defined Static_Params ); | ||
| 20 | use Cache::SharedMemoryBackend; | ||
| 21 | use Error; | ||
| 22 | |||
| 23 | |||
| 24 | @ISA = qw ( Cache::MemoryCache ); | ||
| 25 | |||
| 26 | |||
| 27 | sub Clear | ||
| 28 | { | ||
| 29 | foreach my $namespace ( _Namespaces( ) ) | ||
| 30 | { | ||
| 31 | _Get_Backend( )->delete_namespace( $namespace ); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub Purge | ||
| 37 | { | ||
| 38 | foreach my $namespace ( _Namespaces( ) ) | ||
| 39 | { | ||
| 40 | _Get_Cache( $namespace )->purge( ); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | sub Size | ||
| 46 | { | ||
| 47 | my $size = 0; | ||
| 48 | |||
| 49 | foreach my $namespace ( _Namespaces( ) ) | ||
| 50 | { | ||
| 51 | $size += _Get_Cache( $namespace )->size( ); | ||
| 52 | } | ||
| 53 | |||
| 54 | return $size; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub _Namespaces | ||
| 59 | { | ||
| 60 | return _Get_Backend( )->get_namespaces( ); | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | sub _Get_Backend | ||
| 66 | { | ||
| 67 | return new Cache::SharedMemoryBackend( ); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | sub _Get_Cache | ||
| 72 | { | ||
| 73 | my ( $p_namespace ) = Static_Params( @_ ); | ||
| 74 | |||
| 75 | Assert_Defined( $p_namespace ); | ||
| 76 | |||
| 77 | return new Cache::SharedMemoryCache( { 'namespace' => $p_namespace } ); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub new | ||
| 82 | { | ||
| 83 | my ( $self ) = _new( @_ ); | ||
| 84 | |||
| 85 | $self->_complete_initialization( ); | ||
| 86 | |||
| 87 | return $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | sub _new | ||
| 92 | { | ||
| 93 | my ( $proto, $p_options_hash_ref ) = @_; | ||
| 94 | my $class = ref( $proto ) || $proto; | ||
| 95 | my $self = $class->SUPER::_new( $p_options_hash_ref ); | ||
| 96 | $self->_set_backend( new Cache::SharedMemoryBackend( ) ); | ||
| 97 | return $self; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | 1; | ||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | =pod | ||
| 106 | |||
| 107 | =head1 NAME | ||
| 108 | |||
| 109 | Cache::SharedMemoryCache -- extends the MemoryCache. | ||
| 110 | |||
| 111 | =head1 DESCRIPTION | ||
| 112 | |||
| 113 | The SharedMemoryCache extends the MemoryCache class and binds the data | ||
| 114 | store to shared memory so that separate process can use the same | ||
| 115 | cache. | ||
| 116 | |||
| 117 | The official recommendation is now to use FileCache instead of | ||
| 118 | SharedMemoryCache. The reasons for this include: | ||
| 119 | |||
| 120 | 1) FileCache provides equal or better performance in all cases that | ||
| 121 | we've been able to test. This is due to all modern OS's ability to | ||
| 122 | buffer and cache file system accesses very well. | ||
| 123 | |||
| 124 | 2) FileCache has no real limits on cached object size or the number of | ||
| 125 | cached objects, whereas the SharedMemoryCache has limits, and rather | ||
| 126 | low ones at that. | ||
| 127 | |||
| 128 | 3) FileCache works well on every OS, whereas the SharedMemoryCache | ||
| 129 | works only on systems that support IPC::ShareLite. And IPC::ShareLite | ||
| 130 | is an impressive effort -- but think about how hard it is to get | ||
| 131 | shared memory working properly on *one* system. Now imagine writing a | ||
| 132 | wrapper around shared memory for many operating systems. | ||
| 133 | |||
| 134 | |||
| 135 | =head1 SYNOPSIS | ||
| 136 | |||
| 137 | use Cache::SharedMemoryCache; | ||
| 138 | |||
| 139 | my %cache_options_= ( 'namespace' => 'MyNamespace', | ||
| 140 | 'default_expires_in' => 600 ); | ||
| 141 | |||
| 142 | my $shared_memory_cache = | ||
| 143 | new Cache::SharedMemoryCache( \%cache_options ) or | ||
| 144 | croak( "Couldn't instantiate SharedMemoryCache" ); | ||
| 145 | |||
| 146 | =head1 METHODS | ||
| 147 | |||
| 148 | See Cache::Cache for the API documentation. | ||
| 149 | |||
| 150 | =head1 OPTIONS | ||
| 151 | |||
| 152 | See Cache::Cache for the standard options. | ||
| 153 | |||
| 154 | =head1 PROPERTIES | ||
| 155 | |||
| 156 | See Cache::Cache for the default properties. | ||
| 157 | |||
| 158 | =head1 SEE ALSO | ||
| 159 | |||
| 160 | Cache::Cache, Cache::MemoryCache | ||
| 161 | |||
| 162 | =head1 AUTHOR | ||
| 163 | |||
| 164 | Original author: DeWitt Clinton <dewitt@unto.net> | ||
| 165 | |||
| 166 | Last author: $Author: dclinton $ | ||
| 167 | |||
| 168 | Copyright (C) 2001-2003 DeWitt Clinton | ||
| 169 | |||
| 170 | =cut |