1NAME
2 AnyEvent::Memcached - AnyEvent memcached client
3
4SYNOPSIS
5 use AnyEvent::Memcached;
6
7 my $memd = AnyEvent::Memcached->new(
8 servers => [ "10.0.0.15:11211", "10.0.0.15:11212" ], # same as in Cache::Memcached
9 debug => 1,
10 compress_threshold => 10000,
11 namespace => 'my-namespace:',
12
13 # May use another hashing algo:
14 hasher => 'AnyEvent::Memcached::Hash::WithNext',
15
16 cv => $cv, # AnyEvent->condvar: group callback
17 );
18
19 $memd->set_servers([ "10.0.0.15:11211", "10.0.0.15:11212" ]);
20
21 # Basic methods are like in Cache::Memcached, but with additional cb => sub { ... };
22 # first argument to cb is return value, second is the error(s)
23
24 $memd->set( key => $value, cb => sub {
25 shift or warn "Set failed: @_"
26 } );
27
28 # Single get
29 $memd->get( 'key', cb => sub {
30 my ($value,$err) = shift;
31 $err and return warn "Get failed: @_";
32 warn "Value for key is $value";
33 } );
34
35 # Multi-get
36 $memd->get( [ 'key1', 'key2' ], cb => sub {
37 my ($values,$err) = shift;
38 $err and return warn "Get failed: @_";
39 warn "Value for key1 is $values->{key1} and value for key2 is $values->{key2}"
40 } );
41
42 # Additionally there is rget (see memcachedb-1.2.1-beta)
43
44 $memd->rget( 'fromkey', 'tokey', cb => sub {
45 my ($values,$err) = shift;
46 $err and warn "Get failed: @_";
47 while (my ($key,$value) = each %$values) {
48 # ...
49 }
50 } );
51
52 # Rget with sorted responce values
53 $memd->rget( 'fromkey', 'tokey', rv => 'array' cb => sub {
54 my ($values,$err) = shift;
55 $err and warn "Get failed: @_";
56 for (0 .. $#values/2) {
57 my ($key,$value) = @$values[$_*2,$_*2+1];
58 }
59 } );
60
61DESCRIPTION
62 Asyncronous "memcached/memcachedb" client for AnyEvent framework
63
64NOTICE
65 There is a notices in Cache::Memcached::AnyEvent related to this module.
66 They all has been fixed
67
68 Prerequisites
69 We no longer need Object::Event and Devel::Leak::Cb. At all, the
70 dependency list is like in Cache::Memcached + AnyEvent
71
72 Binary protocol
73 It seems to me, that usage of binary protocol from pure perl gives
74 very little advantage. So for now I don't implement it
75
76 Unimplemented Methods
77 There is a note, that get_multi is not implementeted. In fact, it
78 was implemented by method "get", but the documentation was wrong.
79
80 In general, this module follows the spirit of AnyEvent rather than
81 correspondence to Cache::Memcached interface.
82
83METHODS
84 new %args
85 Currently supported options:
86
87 servers =item namespace =item debug =item cv =item compress_threshold
88 =item compress_enable =item timeout =item hasher
89 If set, will use instance of this class for hashing instead of
90 default. For implementing your own hashing, see sources of
91 AnyEvent::Memcached::Hash and AnyEvent::Memcached::Hash::With::Next
92
93 noreply
94 If true, additional connection will established for noreply
95 commands.
96
97 cas If true, will enable cas/gets commands (since they are not suppotred
98 in memcachedb)
99
100 set_servers
101 Setup server list
102
103 connect
104 Establish connection to all servers and invoke event C<connected>, when ready
105
106 set( $key, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
107 Unconditionally sets a key to a given value in the memcache.
108
109 $rc is
110
111 '1' Successfully stored
112
113 '0' Item was not stored
114
115 undef
116 Error happens, see $err
117
118 cas( $key, $cas, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
119 $memd->gets($key, cb => sub {
120 my $value = shift;
121 unless (@_) { # No errors
122 my ($cas,$val) = @$value;
123 # Change your value in $val
124 $memd->cas( $key, $cas, $value, cb => sub {
125 my $rc = shift;
126 if ($rc) {
127 # stored
128 } else {
129 # ...
130 }
131 });
132 }
133 })
134
135 $rc is the same, as for "set"
136
137 Store the $value on the server under the $key, but only if CAS value
138 associated with this key is equal to $cas. See also "gets"
139
140 add( $key, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
141 Like "set", but only stores in memcache if the key doesn't already
142 exist.
143
144 replace( $key, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
145 Like "set", but only stores in memcache if the key already exists. The
146 opposite of add.
147
148 append( $key, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
149 Append the $value to the current value on the server under the $key.
150
151 append command first appeared in memcached 1.2.4.
152
153 prepend( $key, $value, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
154 Prepend the $value to the current value on the server under the $key.
155
156 prepend command first appeared in memcached 1.2.4.
157
158 get( $key, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
159 Retrieve the value for a $key. $key should be a scalar
160
161 get( $keys : ARRAYREF, [cv => $cv], [ expire => $expire ], cb => $cb->( $values_hash, $err ) )
162 Retrieve the values for a $keys. Return a hash with keys/values
163
164 gets( $key, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
165 Retrieve the value and its CAS for a $key. $key should be a scalar.
166
167 $rc is a reference to an array [$cas, $value], or nothing for
168 non-existent key
169
170 gets( $keys : ARRAYREF, [cv => $cv], [ expire => $expire ], cb => $cb->( $rc, $err ) )
171 Retrieve the values and their CAS for a $keys.
172
173 $rc is a hash reference with $rc->{$key} is a reference to an array
174 [$cas, $value]
175
176 delete( $key, [cv => $cv], [ noreply => 1 ], cb => $cb->( $rc, $err ) )
177 Delete $key and its value from the cache.
178
179 If "noreply" is true, cb doesn't required
180
181 del
182 Alias for "delete"
183
184 remove
185 Alias for "delete"
186
187 incr( $key, $increment, [cv => $cv], [ noreply => 1 ], cb => $cb->( $rc, $err ) )
188 Increment the value for the $key by $delta. Starting with memcached
189 1.3.3 $key should be set to a number or the command will fail. Note that
190 the server doesn't check for overflow.
191
192 If "noreply" is true, cb doesn't required, and if passed, simply called
193 with rc = 1
194
195 Similar to DBI, zero is returned as "0E0", and evaluates to true in a
196 boolean context.
197
198 decr( $key, $decrement, [cv => $cv], [ noreply => 1 ], cb => $cb->( $rc, $err ) )
199 Opposite to "incr"
200
201 rget( $from, $till, [ max => 100 ], [ '+left' => 1 ], [ '+right' => 1 ], [cv => $cv], [ rv => 'array' ], cb => $cb->( $rc, $err ) )
202 Memcachedb 1.2.1-beta implements rget method, that allows to look
203 through the whole storage
204
205 $from
206 the starting key
207
208 $till
209 finishing key
210
211 +left
212 If true, then starting key will be included in results. true by
213 default
214
215 +right
216 If true, then finishing key will be included in results. true by
217 default
218
219 max Maximum number of results to fetch. 100 is the maximum and is the
220 default
221
222 rv If passed rv => 'array', then the return value will be arrayref with
223 values in order, returned by memcachedb.
224
225 incadd ( $key, $increment, [cv => $cv], [ noreply => 1 ], cb => $cb->( $rc, $err ) )
226 Increment key, and if it not exists, add it with initial value. If add
227 fails, try again to incr or fail
228
229 destroy
230 Shutdown object as much, as possible, incl cleaning of incapsulated
231 objects
232
233BUGS
234 Feature requests are welcome
235
236 Bug reports are welcome
237
238AUTHOR
239 Mons Anderson, "<mons at cpan.org>"
240
241COPYRIGHT & LICENSE
242 Copyright 2009 Mons Anderson, all rights reserved.
243
244 This program is free software; you can redistribute it and/or modify it
245 under the same terms as Perl itself.
246
247