AnyEvent-Memcached
view release on metacpan or search on metacpan
NAME
AnyEvent::Memcached - AnyEvent memcached client
SYNOPSIS
use AnyEvent::Memcached;
my $memd = AnyEvent::Memcached->new(
servers => [ "10.0.0.15:11211", "10.0.0.15:11212" ], # same as in Cache::Memcached
debug => 1,
compress_threshold => 10000,
namespace => 'my-namespace:',
# May use another hashing algo:
hasher => 'AnyEvent::Memcached::Hash::WithNext',
cv => $cv, # AnyEvent->condvar: group callback
);
$memd->set_servers([ "10.0.0.15:11211", "10.0.0.15:11212" ]);
# Basic methods are like in Cache::Memcached, but with additional cb => sub { ... };
# first argument to cb is return value, second is the error(s)
$memd->set( key => $value, cb => sub {
shift or warn "Set failed: @_"
} );
# Single get
$memd->get( 'key', cb => sub {
my ($value,$err) = shift;
$err and return warn "Get failed: @_";
warn "Value for key is $value";
} );
# Multi-get
$memd->get( [ 'key1', 'key2' ], cb => sub {
my ($values,$err) = shift;
$err and return warn "Get failed: @_";
warn "Value for key1 is $values->{key1} and value for key2 is $values->{key2}"
} );
# Additionally there is rget (see memcachedb-1.2.1-beta)
$memd->rget( 'fromkey', 'tokey', cb => sub {
my ($values,$err) = shift;
$err and warn "Get failed: @_";
while (my ($key,$value) = each %$values) {
# ...
}
} );
# Rget with sorted responce values
$memd->rget( 'fromkey', 'tokey', rv => 'array' cb => sub {
my ($values,$err) = shift;
$err and warn "Get failed: @_";
for (0 .. $#values/2) {
my ($key,$value) = @$values[$_*2,$_*2+1];
}
} );
DESCRIPTION
Asyncronous "memcached/memcachedb" client for AnyEvent framework
NOTICE
There is a notices in Cache::Memcached::AnyEvent related to this module.
They all has been fixed
Prerequisites
We no longer need Object::Event and Devel::Leak::Cb. At all, the
dependency list is like in Cache::Memcached + AnyEvent
Binary protocol
It seems to me, that usage of binary protocol from pure perl gives
very little advantage. So for now I don't implement it
Unimplemented Methods
There is a note, that get_multi is not implementeted. In fact, it
was implemented by method "get", but the documentation was wrong.
In general, this module follows the spirit of AnyEvent rather than
correspondence to Cache::Memcached interface.
METHODS
new %args
Currently supported options:
servers =item namespace =item debug =item cv =item compress_threshold
=item compress_enable =item timeout =item hasher
If set, will use instance of this class for hashing instead of
default. For implementing your own hashing, see sources of
AnyEvent::Memcached::Hash and AnyEvent::Memcached::Hash::With::Next
noreply
If true, additional connection will established for noreply
commands.
cas If true, will enable cas/gets commands (since they are not suppotred
in memcachedb)
set_servers
Setup server list
connect
Establish connection to all servers and invoke event C<connected>, when ready
set( $key, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
Unconditionally sets a key to a given value in the memcache.
$rc is
'1' Successfully stored
'0' Item was not stored
undef
Error happens, see $err
cas( $key, $cas, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
$memd->gets($key, cb => sub {
my $value = shift;
unless (@_) { # No errors
my ($cas,$val) = @$value;
# Change your value in $val
$memd->cas( $key, $cas, $value, cb => sub {
my $rc = shift;
if ($rc) {
# stored
} else {
# ...
}
});
}
})
$rc is the same, as for "set"
Store the $value on the server under the $key, but only if CAS value
associated with this key is equal to $cas. See also "gets"
add( $key, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
Like "set", but only stores in memcache if the key doesn't already
exist.
replace( $key, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
Like "set", but only stores in memcache if the key already exists. The
opposite of add.
( run in 1.216 second using v1.01-cache-2.11-cpan-39bf76dae61 )