| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use Test::More 'no_plan'; |
|---|
| 7 | |
|---|
| 8 | # Based almost 100% off testClient.py which is Copyright (c) 2007 Dustin Sallings <dustin@spy.net> |
|---|
| 9 | |
|---|
| 10 | # Command constants |
|---|
| 11 | use constant CMD_GET => 0; |
|---|
| 12 | use constant CMD_SET => 1; |
|---|
| 13 | use constant CMD_ADD => 2; |
|---|
| 14 | use constant CMD_REPLACE => 3; |
|---|
| 15 | use constant CMD_DELETE => 4; |
|---|
| 16 | use constant CMD_INCR => 5; |
|---|
| 17 | use constant CMD_DECR => 6; |
|---|
| 18 | use constant CMD_QUIT => 7; |
|---|
| 19 | use constant CMD_FLUSH => 8; |
|---|
| 20 | use constant CMD_GETQ => 9; |
|---|
| 21 | use constant CMD_NOOP => 10; |
|---|
| 22 | use constant CMD_VERSION => 11; |
|---|
| 23 | |
|---|
| 24 | use constant CMD_GETS => 50; |
|---|
| 25 | use constant CMD_CAS => 51; |
|---|
| 26 | |
|---|
| 27 | # CAS, Flags, expiration |
|---|
| 28 | use constant SET_PKT_FMT => "NNNN"; |
|---|
| 29 | |
|---|
| 30 | # Flags, expiration, id |
|---|
| 31 | use constant CAS_PKT_FMT => "NNNN"; |
|---|
| 32 | |
|---|
| 33 | # How long until the deletion takes effect. |
|---|
| 34 | use constant DEL_PKT_FMT => "N"; |
|---|
| 35 | |
|---|
| 36 | # amount, initial value, expiration |
|---|
| 37 | use constant INCRDECR_PKT_FMT => "NNNNN"; |
|---|
| 38 | |
|---|
| 39 | use constant REQ_MAGIC_BYTE => 0x80; |
|---|
| 40 | use constant RES_MAGIC_BYTE => 0x80; |
|---|
| 41 | |
|---|
| 42 | use constant PKT_FMT => "CCnCxxxNN"; |
|---|
| 43 | |
|---|
| 44 | #min recv packet size |
|---|
| 45 | use constant MIN_RECV_PACKET => length(pack(PKT_FMT)); |
|---|
| 46 | |
|---|
| 47 | my $mc = MC::Client->new; |
|---|
| 48 | my $check = sub { |
|---|
| 49 | my ($key, $orig_flags, $orig_value) = @_; |
|---|
| 50 | my ($flags, $value) = $mc->get($key); |
|---|
| 51 | is($flags, $orig_flags, "Flags is set properly"); |
|---|
| 52 | is($value, $orig_value, "Value is set properly"); |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | my $set = sub { |
|---|
| 56 | my ($key, $exp, $orig_flags, $orig_value) = @_; |
|---|
| 57 | $mc->set($key, $exp, $orig_flags, $orig_value); |
|---|
| 58 | $check->($key, $orig_flags, $orig_value); |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | my $empty = sub { |
|---|
| 62 | my $key = shift; |
|---|
| 63 | my $rv =()= eval { $mc->get($key) }; |
|---|
| 64 | is($rv, 0, "Didn't get a result from get"); |
|---|
| 65 | ok($@->not_found, "We got a not found error when we expected one"); |
|---|
| 66 | }; |
|---|
| 67 | |
|---|
| 68 | my $delete = sub { |
|---|
| 69 | my ($key, $when) = @_; |
|---|
| 70 | $mc->delete($key, $when); |
|---|
| 71 | $empty->($key); |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | diag "Flushing..."; |
|---|
| 75 | $mc->flush; |
|---|
| 76 | |
|---|
| 77 | { |
|---|
| 78 | diag "Test Version"; |
|---|
| 79 | my $v = $mc->version; |
|---|
| 80 | ok(defined $v && length($v), "Proper version: $v"); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | diag "Noop"; |
|---|
| 84 | $mc->noop; |
|---|
| 85 | |
|---|
| 86 | diag "Simple set/get"; |
|---|
| 87 | $set->('x', 5, 19, "somevalue"); |
|---|
| 88 | |
|---|
| 89 | diag "Delete"; |
|---|
| 90 | $delete->('x'); |
|---|
| 91 | |
|---|
| 92 | diag "Flush"; |
|---|
| 93 | $set->('x', 5, 19, "somevaluex"); |
|---|
| 94 | $set->('y', 5, 17, "somevaluey"); |
|---|
| 95 | $mc->flush; |
|---|
| 96 | $empty->('x'); |
|---|
| 97 | $empty->('y'); |
|---|
| 98 | |
|---|
| 99 | { |
|---|
| 100 | diag "Reservation delete"; |
|---|
| 101 | $set->('y', 5, 19, "someothervalue"); |
|---|
| 102 | $delete->('y', 1); |
|---|
| 103 | my $rv =()= eval { $mc->add('y', 5, 19, "yetanothervalue") }; |
|---|
| 104 | is($rv, 0, "Add didn't return anything"); |
|---|
| 105 | ok($@->exists, "We got an exists error like we expected"); |
|---|
| 106 | sleep 2; |
|---|
| 107 | $mc->add('y', 5, 19, "wibblevalue"); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | { |
|---|
| 111 | diag "Add"; |
|---|
| 112 | $empty->('i'); |
|---|
| 113 | $mc->add('i', 5, 19, "ex"); |
|---|
| 114 | $check->('i', 19, "ex"); |
|---|
| 115 | |
|---|
| 116 | my $rv =()= eval { $mc->add('i', 5, 19, "ex2") }; |
|---|
| 117 | is($rv, 0, "Add didn't return anything"); |
|---|
| 118 | ok($@->exists, "Expected exists error received"); |
|---|
| 119 | |
|---|
| 120 | $check->('i', 19, "ex"); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | { |
|---|
| 124 | diag "Replace"; |
|---|
| 125 | $empty->('j'); |
|---|
| 126 | |
|---|
| 127 | my $rv =()= eval { $mc->replace('j', 5, 19, "ex") }; |
|---|
| 128 | is($rv, 0, "Replace didn't return anything"); |
|---|
| 129 | ok($@->not_found, "Expected not_found error received"); |
|---|
| 130 | |
|---|
| 131 | $empty->('j'); |
|---|
| 132 | |
|---|
| 133 | $mc->add('j', 5, 14, "ex2"); |
|---|
| 134 | $check->('j', 14, "ex2"); |
|---|
| 135 | |
|---|
| 136 | $mc->replace('j', 5, 24, "ex3"); |
|---|
| 137 | $check->('j', 24, "ex3"); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | { |
|---|
| 141 | diag "MultiGet"; |
|---|
| 142 | $mc->add('xx', 5, 1, "ex"); |
|---|
| 143 | $mc->add('wye', 5, 2, "why"); |
|---|
| 144 | my $rv = $mc->getMulti(qw(xx wye zed)); |
|---|
| 145 | |
|---|
| 146 | is_deeply([1, 'ex'], $rv->{xx}, "X is correct"); |
|---|
| 147 | is_deeply([2, 'why'], $rv->{wye}, "Y is correct"); |
|---|
| 148 | is(keys(%$rv), 2, "Got only two answers like we expect"); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | diag "Test increment"; |
|---|
| 152 | $mc->flush; |
|---|
| 153 | is($mc->incr("x"), 0, "First incr call is zero"); |
|---|
| 154 | is($mc->incr("x"), 1, "Second incr call is one"); |
|---|
| 155 | is($mc->incr("x", 211), 212, "Adding 211 gives you 212"); |
|---|
| 156 | is($mc->incr("x", 2**33), 8589934804, "Blast the 32bit border"); |
|---|
| 157 | |
|---|
| 158 | diag "Test decrement"; |
|---|
| 159 | $mc->flush; |
|---|
| 160 | is($mc->incr("x", undef, 5), 5, "Initial value"); |
|---|
| 161 | is($mc->decr("x"), 4, "Decrease by one"); |
|---|
| 162 | is($mc->decr("x", 211), 0, "Floor is zero"); |
|---|
| 163 | |
|---|
| 164 | <<EOT; |
|---|
| 165 | def testIncrDoesntExistNoCreate(self): |
|---|
| 166 | """Testing incr when a value doesn't exist (and not creating).""" |
|---|
| 167 | try: |
|---|
| 168 | self.mc.incr("x", exp=-1) |
|---|
| 169 | self.fail("Expected failure to increment non-existent key") |
|---|
| 170 | except MemcachedError, e: |
|---|
| 171 | self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) |
|---|
| 172 | self.assertNotExists("x") |
|---|
| 173 | |
|---|
| 174 | def testIncrDoesntExistCreate(self): |
|---|
| 175 | """Testing incr when a value doesn't exist (and we make a new one)""" |
|---|
| 176 | self.assertNotExists("x") |
|---|
| 177 | self.assertEquals(19, self.mc.incr("x", init=19)) |
|---|
| 178 | |
|---|
| 179 | def testDecrDoesntExistNoCreate(self): |
|---|
| 180 | """Testing decr when a value doesn't exist (and not creating).""" |
|---|
| 181 | try: |
|---|
| 182 | self.mc.decr("x", exp=-1) |
|---|
| 183 | self.fail("Expected failiure to decrement non-existent key.") |
|---|
| 184 | except MemcachedError, e: |
|---|
| 185 | self.assertEquals(memcacheConstants.ERR_NOT_FOUND, e.status) |
|---|
| 186 | self.assertNotExists("x") |
|---|
| 187 | |
|---|
| 188 | def testDecrDoesntExistCreate(self): |
|---|
| 189 | """Testing decr when a value doesn't exist (and we make a new one)""" |
|---|
| 190 | self.assertNotExists("x") |
|---|
| 191 | self.assertEquals(19, self.mc.decr("x", init=19)) |
|---|
| 192 | EOT |
|---|
| 193 | |
|---|
| 194 | { |
|---|
| 195 | diag "CAS"; |
|---|
| 196 | $mc->flush; |
|---|
| 197 | |
|---|
| 198 | { |
|---|
| 199 | my $rv =()= eval { $mc->cas("x", 5, 19, 0x7FFFFFFFFF, "bad value") }; |
|---|
| 200 | is($rv, 0, "Empty return on expected failure"); |
|---|
| 201 | ok($@->not_found, "Error was 'not found' as expected"); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | $mc->add("x", 5, 19, "original value"); |
|---|
| 205 | |
|---|
| 206 | my ($flags, $i, $val) = $mc->gets("x"); |
|---|
| 207 | is($val, "original value", "->gets returned proper value"); |
|---|
| 208 | |
|---|
| 209 | { |
|---|
| 210 | my $rv =()= eval { $mc->cas("x", 5, 19, $i+1, "broken value") }; |
|---|
| 211 | is($rv, 0, "Empty return on expected failure (1)"); |
|---|
| 212 | ok($@->exists, "Expected error state of 'exists' (1)"); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | $mc->cas("x", 5, 19, $i, "new value"); |
|---|
| 216 | |
|---|
| 217 | my ($newflags, $newi, $newval) = $mc->gets("x"); |
|---|
| 218 | is($newval, "new value", "CAS properly overwrote value"); |
|---|
| 219 | |
|---|
| 220 | { |
|---|
| 221 | my $rv =()= eval { $mc->cas("x", 5, 19, $i, "replay value") }; |
|---|
| 222 | is($rv, 0, "Empty return on expected failure (2)"); |
|---|
| 223 | ok($@->exists, "Expected error state of 'exists' (2)"); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | (undef, undef, my $newval2) = $mc->gets("x"); |
|---|
| 227 | is($newval2, "new value", "CAS replay didn't overwrite value"); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | $mc->flush; |
|---|
| 231 | $mc->close; |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | package MC::Client; |
|---|
| 235 | |
|---|
| 236 | use strict; |
|---|
| 237 | use warnings; |
|---|
| 238 | |
|---|
| 239 | use fields qw(socket); |
|---|
| 240 | |
|---|
| 241 | use IO::Socket::INET; |
|---|
| 242 | |
|---|
| 243 | sub new { |
|---|
| 244 | my $self = shift; |
|---|
| 245 | |
|---|
| 246 | my $host = shift || '127.0.0.1'; |
|---|
| 247 | my $port = shift || 11211; |
|---|
| 248 | |
|---|
| 249 | my $sock = IO::Socket::INET->new(PeerHost => $host, PeerPort => $port); |
|---|
| 250 | |
|---|
| 251 | unless ($sock) { |
|---|
| 252 | warn "Unable to contact memcached."; |
|---|
| 253 | return; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | $self = fields::new($self); |
|---|
| 257 | |
|---|
| 258 | $self->{socket} = $sock; |
|---|
| 259 | |
|---|
| 260 | return $self; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | sub close { |
|---|
| 264 | my $self = shift; |
|---|
| 265 | return $self->{socket}->close(@_); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | sub _sendCmd { |
|---|
| 269 | my $self = shift; |
|---|
| 270 | die "Not enough args to _sendCmd" unless @_ >= 4; |
|---|
| 271 | my ($cmd, $key, $val, $opaque, $extraHeader) = @_; |
|---|
| 272 | |
|---|
| 273 | $extraHeader = '' unless defined $extraHeader; |
|---|
| 274 | |
|---|
| 275 | my $keylen = length($key); |
|---|
| 276 | my $vallen = length($val); |
|---|
| 277 | my $extralen = length($extraHeader); |
|---|
| 278 | |
|---|
| 279 | my $msg = pack(::PKT_FMT, ::REQ_MAGIC_BYTE, $cmd, $keylen, $extralen, |
|---|
| 280 | $keylen + $vallen + $extralen, $opaque); |
|---|
| 281 | return $self->{socket}->send($msg . $extraHeader . $key . $val); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | sub _handleSingleResponse { |
|---|
| 285 | my $self = shift; |
|---|
| 286 | my $myopaque = shift; |
|---|
| 287 | |
|---|
| 288 | $self->{socket}->recv(my $response, ::MIN_RECV_PACKET); |
|---|
| 289 | |
|---|
| 290 | Test::More::is(length($response), ::MIN_RECV_PACKET, "Expected read length"); |
|---|
| 291 | |
|---|
| 292 | my ($magic, $cmd, $errcode, $extralen, $remaining, |
|---|
| 293 | $opaque) = unpack(::PKT_FMT, $response); |
|---|
| 294 | |
|---|
| 295 | Test::More::is($magic, ::RES_MAGIC_BYTE, "Got proper magic"); |
|---|
| 296 | |
|---|
| 297 | return ($opaque, "") |
|---|
| 298 | if $remaining == 0; |
|---|
| 299 | |
|---|
| 300 | $self->{socket}->recv(my $rv, $remaining); |
|---|
| 301 | |
|---|
| 302 | if (defined $myopaque) { |
|---|
| 303 | Test::More::is($opaque, $myopaque, "Expected opaque"); |
|---|
| 304 | } else { |
|---|
| 305 | Test::More::pass("Implicit pass since myopaque is undefined"); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | if ($errcode) { |
|---|
| 309 | die MC::Error->new($errcode, $rv); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | return ($opaque, $rv); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | sub _doCmd { |
|---|
| 316 | my $self = shift; |
|---|
| 317 | die unless @_ >= 3; |
|---|
| 318 | my ($cmd, $key, $val, $extraHeader) = @_; |
|---|
| 319 | |
|---|
| 320 | $extraHeader = '' unless defined $extraHeader; |
|---|
| 321 | |
|---|
| 322 | my $opaque = int(rand(2**32)); |
|---|
| 323 | |
|---|
| 324 | $self->_sendCmd($cmd, $key, $val, $opaque, $extraHeader); |
|---|
| 325 | (undef, my $rv) = $self->_handleSingleResponse($opaque); |
|---|
| 326 | return $rv; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | sub __parseGet { |
|---|
| 330 | my $self = shift; |
|---|
| 331 | my $rv = shift; # currently contains 4 bytes of 'flag' followed by value |
|---|
| 332 | my $cas = substr $rv, 0, 8, ''; # $cas contains CAS value, $rv has f, v. |
|---|
| 333 | my $flag = substr $rv, 0, 4, ''; # Now $flag contains flags, $rv contains value |
|---|
| 334 | return unpack("N", $flag), $rv; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | sub get { |
|---|
| 338 | my $self = shift; |
|---|
| 339 | my $key = shift; |
|---|
| 340 | my $parts = $self->_doCmd(::CMD_GET, $key, ''); |
|---|
| 341 | return $self->__parseGet($parts); |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | sub _mutate { |
|---|
| 345 | my $self = shift; |
|---|
| 346 | my ($cmd, $key, $exp, $flags, $val) = @_; |
|---|
| 347 | |
|---|
| 348 | return $self->_doCmd($cmd, $key, $val, pack(::SET_PKT_FMT, 0, 0, $flags, $exp)); |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | sub set { |
|---|
| 352 | my $self = shift; |
|---|
| 353 | my ($key, $exp, $flags, $val) = @_; |
|---|
| 354 | |
|---|
| 355 | return $self->_mutate(::CMD_SET, $key, $exp, $flags, $val); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | sub __incrdecr { |
|---|
| 359 | my $self = shift; |
|---|
| 360 | my ($cmd, $key, $amt, $init, $exp) = @_; |
|---|
| 361 | |
|---|
| 362 | my $amt_hi = int($amt / 2 ** 32); |
|---|
| 363 | my $amt_lo = int($amt % 2 ** 32); |
|---|
| 364 | |
|---|
| 365 | my $init_hi = int($init / 2 ** 32); |
|---|
| 366 | my $init_lo = int($init % 2 ** 32); |
|---|
| 367 | |
|---|
| 368 | my $data = $self->_doCmd($cmd, $key, '', pack(::INCRDECR_PKT_FMT, $amt_hi, $amt_lo, $init_hi, $init_lo, $exp)); |
|---|
| 369 | my $header = substr $data, 0, 12, ''; |
|---|
| 370 | my ($resp_hi, $resp_lo) = unpack "NN", $header; |
|---|
| 371 | my $resp = ($resp_hi * 2 ** 32) + $resp_lo; |
|---|
| 372 | return $resp; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | sub incr { |
|---|
| 376 | my $self = shift; |
|---|
| 377 | my ($key, $amt, $init, $exp) = @_; |
|---|
| 378 | $amt = 1 unless defined $amt; |
|---|
| 379 | $init = 0 unless defined $init; |
|---|
| 380 | $exp = 0 unless defined $exp; |
|---|
| 381 | |
|---|
| 382 | return $self->__incrdecr(::CMD_INCR, $key, $amt, $init, $exp); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | sub decr { |
|---|
| 386 | my $self = shift; |
|---|
| 387 | my ($key, $amt, $init, $exp) = @_; |
|---|
| 388 | $amt = 1 unless defined $amt; |
|---|
| 389 | $init = 0 unless defined $init; |
|---|
| 390 | $exp = 0 unless defined $exp; |
|---|
| 391 | |
|---|
| 392 | return $self->__incrdecr(::CMD_DECR, $key, $amt, $init, $exp); |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | sub add { |
|---|
| 396 | my $self = shift; |
|---|
| 397 | my ($key, $exp, $flags, $val) = @_; |
|---|
| 398 | return $self->_mutate(::CMD_ADD, $key, $exp, $flags, $val); |
|---|
| 399 | } |
|---|
| 400 | sub replace { |
|---|
| 401 | my $self = shift; |
|---|
| 402 | my ($key, $exp, $flags, $val) = @_; |
|---|
| 403 | return $self->_mutate(::CMD_REPLACE, $key, $exp, $flags, $val); |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | sub getMulti { |
|---|
| 407 | my $self = shift; |
|---|
| 408 | my @keys = @_; |
|---|
| 409 | |
|---|
| 410 | for (my $i = 0; $i < @keys; $i++) { |
|---|
| 411 | $self->_sendCmd(::CMD_GETQ, $keys[$i], '', $i); |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | my $terminal = @keys + 10; |
|---|
| 415 | $self->_sendCmd(::CMD_NOOP, '', '', $terminal); |
|---|
| 416 | |
|---|
| 417 | my %return; |
|---|
| 418 | |
|---|
| 419 | while (1) { |
|---|
| 420 | my ($opaque, $data) = $self->_handleSingleResponse; |
|---|
| 421 | last if $opaque == $terminal; |
|---|
| 422 | |
|---|
| 423 | $return{$keys[$opaque]} = [$self->__parseGet($data)]; |
|---|
| 424 | } |
|---|
| 425 | return %return if wantarray; |
|---|
| 426 | return \%return; |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | sub gets { |
|---|
| 430 | my $self = shift; |
|---|
| 431 | my $key = shift; |
|---|
| 432 | |
|---|
| 433 | my $data = $self->_doCmd(::CMD_GETS, $key, ''); |
|---|
| 434 | my $header = substr $data, 0, 12, ''; |
|---|
| 435 | my ($flags, $ident_hi, $ident_lo) = unpack "NNN", $header; |
|---|
| 436 | my $ident = ($ident_hi * 2 ** 32) + $ident_lo; |
|---|
| 437 | |
|---|
| 438 | return $flags, $ident, $data; |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | sub cas { |
|---|
| 442 | my $self = shift; |
|---|
| 443 | my ($key, $exp, $flags, $oldVal, $val) = @_; |
|---|
| 444 | |
|---|
| 445 | my $oldVal_hi = int($oldVal / 2 ** 32); |
|---|
| 446 | my $oldVal_lo = int($oldVal % 2 ** 32); |
|---|
| 447 | |
|---|
| 448 | return $self->_doCmd(::CMD_CAS, $key, $val, pack(::CAS_PKT_FMT, $flags, $exp, $oldVal_hi, $oldVal_lo)); |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | sub noop { |
|---|
| 452 | my $self = shift; |
|---|
| 453 | return $self->_doCmd(::CMD_NOOP, '', ''); |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | sub delete { |
|---|
| 457 | my $self = shift; |
|---|
| 458 | my ($key, $when) = @_; |
|---|
| 459 | $when = 0 unless defined $when; |
|---|
| 460 | |
|---|
| 461 | return $self->_doCmd(::CMD_DELETE, $key, '', pack(::DEL_PKT_FMT, $when)); |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | sub version { |
|---|
| 465 | my $self = shift; |
|---|
| 466 | return $self->_doCmd(::CMD_VERSION, '', ''); |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | sub flush { |
|---|
| 470 | my $self = shift; |
|---|
| 471 | return $self->_doCmd(::CMD_FLUSH, '', ''); |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | package MC::Error; |
|---|
| 475 | |
|---|
| 476 | use strict; |
|---|
| 477 | use warnings; |
|---|
| 478 | |
|---|
| 479 | use constant ERR_UNKNOWN_CMD => 0x81; |
|---|
| 480 | use constant ERR_NOT_FOUND => 0x1; |
|---|
| 481 | use constant ERR_EXISTS => 0x2; |
|---|
| 482 | |
|---|
| 483 | use overload '""' => sub { |
|---|
| 484 | my $self = shift; |
|---|
| 485 | |
|---|
| 486 | return "Memcache Error ($self->[0]): $self->[1]"; |
|---|
| 487 | }; |
|---|
| 488 | |
|---|
| 489 | sub new { |
|---|
| 490 | my $class = shift; |
|---|
| 491 | my $error = [@_]; |
|---|
| 492 | |
|---|
| 493 | my $self = bless $error, (ref $class || $class); |
|---|
| 494 | |
|---|
| 495 | return $self; |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | sub not_found { |
|---|
| 499 | my $self = shift; |
|---|
| 500 | |
|---|
| 501 | return $self->[0] == ERR_NOT_FOUND; |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | sub exists { |
|---|
| 505 | my $self = shift; |
|---|
| 506 | |
|---|
| 507 | return $self->[0] == ERR_EXISTS; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | # vim: filetype=perl |
|---|