Changeset 5620
- Timestamp:
- 01/27/08 14:34:23 (5 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Cache-Memcached-LibMemcached/branch/perl-memcached/lib/Cache/Memcached/LibMemcached.pm
r5425 r5620 40 40 41 41 # proxy these methods 42 foreach my $method qw(delete ) {42 foreach my $method qw(delete get set add replace) { 43 43 eval <<" EOSUB"; 44 44 sub $method { … … 69 69 $self->set_servers($args->{servers}); 70 70 71 Memcached::libmemcached::memcached_set_fetch_callback($memd, \&_deflate_callback); 71 # Set compression/serialization callbacks 72 Memcached::libmemcached::memcached_set_callback_coderefs($memd, 73 # Closures so we have reference to $self 74 $self->_mk_callbacks() 75 ); 72 76 73 77 return $self; … … 96 100 } 97 101 98 sub get99 {100 my $self = shift;101 my $key = shift;102 my ($flags, $rc);103 my $value = Memcached::libmemcached::memcached_get($self->[MEMD_BACKEND], $key, $flags, $rc);104 105 if ($flags & F_COMPRESS) {106 if (! HAVE_ZLIB) {107 croak("Data for $key is compressed, but we have no Compress::Zlib");108 }109 $value = Compress::Zlib::memGunzip($value);110 }111 112 if ($flags & F_STORABLE) {113 $value = Storable::thaw($value);114 }115 return $value;116 }117 118 102 sub get_multi 119 103 { … … 124 108 } 125 109 126 sub _deflate_callback 127 { 128 my ($key, $value, $flags) = @_; 129 if ($flags & F_COMPRESS) { 130 if (! HAVE_ZLIB) { 131 croak("Data for $key is compressed, but we have no Compress::Zlib"); 110 sub _mk_callbacks 111 { 112 my $self = shift; 113 114 my $inflate = sub { 115 my ($key, $flags) = @_; 116 if ($flags & F_COMPRESS) { 117 if (! HAVE_ZLIB) { 118 croak("Data for $key is compressed, but we have no Compress::Zlib"); 119 } 120 $_ = Compress::Zlib::memGunzip($_); 132 121 } 133 $value = Compress::Zlib::memGunzip($value); 134 } 135 136 if ($flags & F_STORABLE) { 137 $value = Storable::thaw($value); 138 } 139 return $value; 140 } 141 142 sub set 143 { 144 my $self = shift; 145 my $key = shift; 146 my $value = shift; 147 my $expires = shift || 0; 148 149 my $flags = 0; 150 # Check if we have a complex structure 151 if (ref $value) { 152 $value = Storable::nfreeze($value); 153 $flags |= F_STORABLE; 154 } 155 156 # Check if we need compression 157 if (HAVE_ZLIB && $self->[MEMD_COMPRESS_ENABLE] && $self->[MEMD_COMPRESS_THRESHOLD]) { 158 # Find the byte length 159 my $length = bytes::length($value); 160 if ($length > $self->[MEMD_COMPRESS_THRESHOLD]) { 161 my $tmp = Compress::Zlib::memGzip($value); 162 if (1 - bytes::length($tmp) / $length < $self->[MEMD_COMPRESS_SAVINGS]) { 163 $value = $tmp; 164 $flags |= F_COMPRESS; 122 123 if ($flags & F_STORABLE) { 124 $_ = Storable::thaw($_); 125 } 126 return (); 127 }; 128 129 my $deflate = sub { 130 my ($key, $flags) = @_; 131 # Check if we have a complex structure 132 133 if (ref $_) { 134 $_ = Storable::nfreeze($_); 135 $flags |= F_STORABLE; 136 } 137 138 # Check if we need compression 139 if (HAVE_ZLIB && $self->[MEMD_COMPRESS_ENABLE] && $self->[MEMD_COMPRESS_THRESHOLD]) { 140 # Find the byte length 141 my $length = bytes::length($_); 142 if ($length > $self->[MEMD_COMPRESS_THRESHOLD]) { 143 my $tmp = Compress::Zlib::memGzip($_); 144 if (1 - bytes::length($tmp) / $length < $self->[MEMD_COMPRESS_SAVINGS]) { 145 $_ = $tmp; 146 $flags |= F_COMPRESS; 147 } 165 148 } 166 149 } 167 } 168 169 return Memcached::libmemcached::memcached_set($self->[MEMD_BACKEND], $key, $value, $expires, $flags); 170 } 171 172 sub replace 173 { 174 my $self = shift; 175 Memcached::libmemcached::memcached_replace($self->[MEMD_BACKEND], @_[0, 1, 2]); 150 return (); 151 }; 152 return ($deflate, $inflate); 176 153 } 177 154 … … 180 157 my $self = shift; 181 158 my $val; 182 Memcached::libmemcached::memcached_increment($self->[MEMD_BACKEND], $_[0], $_[1] || 1, $val);159 Memcached::libmemcached::memcached_increment($self->[MEMD_BACKEND], @_, $val); 183 160 return $val; 184 161 } … … 188 165 my $self = shift; 189 166 my $val; 190 Memcached::libmemcached::memcached_decrement($self->[MEMD_BACKEND], $_[0], $_[1] || 1, $val);167 Memcached::libmemcached::memcached_decrement($self->[MEMD_BACKEND], @_, $val); 191 168 return $val; 192 169 } … … 199 176 200 177 *remove = \&delete; 178 179 sub disconnect_all 180 { 181 my $self = shift; 182 Memcached::libmemcached::memcached_quit($self->[MEMD_BACKEND]); 183 } 184 185 sub stats { +{} } 201 186 202 187 1;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)