Class: Memcache::NativeServer
Direct Known Subclasses
Instance Method Summary collapse
- #add(*args) ⇒ Object
- #append(key, value) ⇒ Object
- #cas(*args) ⇒ Object
- #close ⇒ Object
- #decr(*args) ⇒ Object
- #delete(key) ⇒ Object
- #flush_all(*args) ⇒ Object
- #get(*args) ⇒ Object
- #incr(*args) ⇒ Object
- #initialize(opts) ⇒ Object constructor
- #prefix ⇒ Object
- #prefix=(prefix) ⇒ Object
- #prepend(key, value) ⇒ Object
- #replace(*args) ⇒ Object
- #set(*args) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(opts) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'ext/native_server.c', line 96
static VALUE mc_initialize(VALUE self, VALUE opts) {
memcached_st *mc;
VALUE servers_aryv, prefixv, hashv, distributionv;
Data_Get_Struct(self, memcached_st, mc);
hashv = rb_hash_aref(opts, sym_hash);
distributionv = rb_hash_aref(opts, sym_distribution);
prefixv = rb_hash_aref(opts, sym_prefix);
servers_aryv = rb_hash_aref(opts, sym_servers);
if (!NIL_P(hashv)) {
memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_HASH, hash_behavior(hashv));
memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_KETAMA_HASH, hash_behavior(hashv));
}
if (!NIL_P(distributionv))
memcached_behavior_set_distribution(mc, distribution_behavior(distributionv));
if (RTEST( rb_hash_aref(opts, sym_ketama) ))
memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_KETAMA, true);
if (RTEST( rb_hash_aref(opts, sym_ketama_weighted) ))
memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, true);
if (RTEST( rb_hash_aref(opts, sym_hash_with_prefix) ))
memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY, true);
if (RTEST( rb_hash_aref(opts, sym_binary) ))
memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true);
if (!NIL_P(prefixv))
memcached_callback_set(mc, MEMCACHED_CALLBACK_PREFIX_KEY, STR2CSTR(prefixv));
if (!NIL_P(servers_aryv)) {
char* server;
int i;
for (i = 0; i < RARRAY_LEN(servers_aryv); i++) {
server = StringValuePtr(RARRAY_PTR(servers_aryv)[i]);
memcached_server_push(mc, memcached_servers_parse(server));
}
} else {
VALUE hostv, portv, weightv;
char* host;
int port, weight;
hostv = rb_hash_aref(opts, sym_host);
portv = rb_hash_aref(opts, sym_port);
weightv = rb_hash_aref(opts, sym_weight);
host = StringValuePtr(hostv);
port = NIL_P(portv) ? MEMCACHED_DEFAULT_PORT : NUM2INT(portv);
weight = NIL_P(weightv) ? 0 : NUM2INT(weightv);
memcached_server_add_with_weight(mc, StringValuePtr(hostv), port, weight);
}
return self;
}
|
Instance Method Details
#add(*args) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'ext/native_server.c', line 440
VALUE mc_add(int argc, VALUE *argv, VALUE self) {
memcached_st *mc;
VALUE key, value, expiry, flags;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
rb_scan_args(argc, argv, "22", &key, &value, &expiry, &flags);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
value = StringValue(value);
result = memcached_add(mc, RSTRING_PTR(key), RSTRING_LEN(key), RSTRING_PTR(value), RSTRING_LEN(value),
RTEST(expiry) ? NUM2UINT(expiry) : 0,
RTEST(flags) ? NUM2UINT(flags) : 0);
if (result == MEMCACHED_SUCCESS) {
return value;
} else if(result == MEMCACHED_NOTSTORED) {
return Qnil;
} else {
return throw_error(&result);
}
}
|
#append(key, value) ⇒ Object
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'ext/native_server.c', line 490
VALUE mc_append(VALUE self, VALUE key, VALUE value) {
memcached_st *mc;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
value = StringValue(value);
result = memcached_append(mc, RSTRING_PTR(key), RSTRING_LEN(key), RSTRING_PTR(value), RSTRING_LEN(value), 0, 0);
if (result == MEMCACHED_SUCCESS) {
return Qtrue;
} else if(result == MEMCACHED_NOTSTORED) {
return Qfalse;
} else {
return throw_error(&result);
}
}
|
#cas(*args) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'ext/native_server.c', line 345
static VALUE mc_cas(int argc, VALUE *argv, VALUE self) {
memcached_st *mc;
VALUE key, value, cas, expiry, flags;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
rb_scan_args(argc, argv, "32", &key, &value, &cas, &expiry, &flags);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
value = StringValue(value);
result = memcached_cas(mc, RSTRING_PTR(key), RSTRING_LEN(key), RSTRING_PTR(value), RSTRING_LEN(value),
RTEST(expiry) ? NUM2UINT(expiry) : 0,
RTEST(flags) ? NUM2UINT(flags) : 0,
NUM2ULL(cas));
if (result == MEMCACHED_SUCCESS) {
return value;
} else if (result == MEMCACHED_NOTFOUND || result == MEMCACHED_DATA_EXISTS) {
return Qnil;
} else {
return throw_error(&result);
}
}
|
#close ⇒ Object
574 575 576 577 578 579 |
# File 'ext/native_server.c', line 574 VALUE mc_close(VALUE self) { memcached_st *mc; Data_Get_Struct(self, memcached_st, mc); memcached_quit(mc); return Qnil; } |
#decr(*args) ⇒ Object
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'ext/native_server.c', line 396
VALUE mc_decr(int argc, VALUE *argv, VALUE self) {
memcached_st *mc;
VALUE key, amount;
static memcached_return_t result;
unsigned int offset;
uint64_t value;
Data_Get_Struct(self, memcached_st, mc);
rb_scan_args(argc, argv, "11", &key, &amount);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
offset = RTEST(amount) ? NUM2INT(amount) : 1;
result = memcached_decrement(mc, RSTRING_PTR(key), RSTRING_LEN(key), offset, &value);
if (result == MEMCACHED_SUCCESS) {
return LONG2NUM(value);
} else if (result == MEMCACHED_NOTFOUND) {
return Qnil;
} else {
return throw_error(&result);
}
}
|
#delete(key) ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'ext/native_server.c', line 421
VALUE mc_delete(VALUE self, VALUE key) {
memcached_st *mc;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
result = memcached_delete(mc, RSTRING_PTR(key), RSTRING_LEN(key), 0);
if (result == MEMCACHED_SUCCESS) {
return Qtrue;
} else if(result == MEMCACHED_NOTFOUND) {
return Qnil;
} else {
return throw_error(&result);
}
}
|
#flush_all(*args) ⇒ Object
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'ext/native_server.c', line 532
VALUE mc_flush_all(int argc, VALUE *argv, VALUE self) {
memcached_st *mc;
VALUE delay;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
rb_scan_args(argc, argv, "01", &delay);
result = memcached_flush(mc, RTEST(delay) ? NUM2UINT(delay) : 0);
if (result == MEMCACHED_SUCCESS) {
return Qnil;
} else {
return throw_error(&result);
}
}
|
#get(*args) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'ext/native_server.c', line 242
static VALUE mc_get(int argc, VALUE *argv, VALUE self) {
memcached_st *mc;
VALUE cas, keys, results, key, value;
VALUE scalar_key = Qnil;
memcached_return status;
Data_Get_Struct(self, memcached_st, mc);
rb_scan_args(argc, argv, "11", &keys, &cas);
memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, RTEST(cas) ? 1 : 0);
if (RTEST(cas) && TYPE(keys) != T_ARRAY) {
scalar_key = keys;
keys = rb_ary_new4(1, &keys);
}
if (TYPE(keys) != T_ARRAY) {
char* str;
size_t len;
uint32_t flags;
key = use_binary(mc) ? keys : escape_key(keys, NULL);
str = memcached_get(mc, RSTRING_PTR(key), RSTRING_LEN(key), &len, &flags, &status);
if (str == NULL) return Qnil;
if (status == MEMCACHED_SUCCESS) {
value = rb_str_new(str, len);
rb_ivar_set(value, iv_memcache_flags, INT2NUM(flags));
free(str);
return value;
} else {
printf("Memcache read error: %s %u\n", memcached_strerror(mc, status), status);
return Qnil;
}
} else {
memcached_result_st* result;
size_t num_keys, i;
const char** key_strings;
size_t* key_lengths;
bool escaped;
results = rb_hash_new();
num_keys = RARRAY_LEN(keys);
if (num_keys == 0) return results;
key_strings = (const char**) malloc(num_keys * sizeof(char *));
key_lengths = (size_t *) malloc(num_keys * sizeof(size_t));
for (i = 0; i < RARRAY_LEN(keys); i++) {
key = RARRAY_PTR(keys)[i];
if (!use_binary(mc)) key = escape_key(key, &escaped);
key_lengths[i] = RSTRING_LEN(key);
key_strings[i] = RSTRING_PTR(key);
}
memcached_mget(mc, key_strings, key_lengths, num_keys);
while (result = memcached_fetch_result(mc, NULL, &status)) {
if (escaped) {
key = unescape_key(memcached_result_key_value(result), memcached_result_key_length(result));
} else {
key = rb_str_new(memcached_result_key_value(result), memcached_result_key_length(result));
}
if (status == MEMCACHED_SUCCESS) {
value = rb_str_new(memcached_result_value(result), memcached_result_length(result));
rb_ivar_set(value, iv_memcache_flags, INT2NUM(memcached_result_flags(result)));
if (RTEST(cas)) rb_ivar_set(value, iv_memcache_cas, ULL2NUM(memcached_result_cas(result)));
memcached_result_free(result);
rb_hash_aset(results, key, value);
} else {
printf("Memcache read error: %s %u\n", memcached_strerror(mc, status), status);
}
}
free(key_strings);
free(key_lengths);
if (!NIL_P(scalar_key)) return rb_hash_aref(results, scalar_key);
return results;
}
}
|
#incr(*args) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'ext/native_server.c', line 371
VALUE mc_incr(int argc, VALUE *argv, VALUE self) {
memcached_st *mc;
VALUE key, amount;
static memcached_return_t result;
unsigned int offset;
uint64_t value;
Data_Get_Struct(self, memcached_st, mc);
rb_scan_args(argc, argv, "11", &key, &amount);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
offset = RTEST(amount) ? NUM2INT(amount) : 1;
result = memcached_increment(mc, RSTRING_PTR(key), RSTRING_LEN(key), offset, &value);
if (result == MEMCACHED_SUCCESS) {
return LONG2NUM(value);
} else if (result == MEMCACHED_NOTFOUND) {
return Qnil;
} else {
return throw_error(&result);
}
}
|
#prefix ⇒ Object
563 564 565 566 567 568 569 570 571 572 |
# File 'ext/native_server.c', line 563
VALUE mc_get_prefix(VALUE self) {
memcached_st *mc;
static memcached_return_t result;
char* prefix;
Data_Get_Struct(self, memcached_st, mc);
prefix = (char*) memcached_callback_get(mc, MEMCACHED_CALLBACK_PREFIX_KEY, &result);
return prefix ? rb_str_new2(prefix) : Qnil;
}
|
#prefix=(prefix) ⇒ Object
549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'ext/native_server.c', line 549
VALUE mc_set_prefix(VALUE self, VALUE prefix) {
memcached_st *mc;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
if (NIL_P(prefix)) {
result = memcached_callback_set(mc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
} else {
prefix = StringValue(prefix);
result = memcached_callback_set(mc, MEMCACHED_CALLBACK_PREFIX_KEY, STR2CSTR(prefix));
}
return prefix;
}
|
#prepend(key, value) ⇒ Object
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'ext/native_server.c', line 511
VALUE mc_prepend(VALUE self, VALUE key, VALUE value) {
memcached_st *mc;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
value = StringValue(value);
result = memcached_prepend(mc, RSTRING_PTR(key), RSTRING_LEN(key), RSTRING_PTR(value), RSTRING_LEN(value), 0, 0);
if (result == MEMCACHED_SUCCESS) {
return Qtrue;
} else if(result == MEMCACHED_NOTSTORED) {
return Qfalse;
} else {
return throw_error(&result);
}
}
|
#replace(*args) ⇒ Object
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'ext/native_server.c', line 465
VALUE mc_replace(int argc, VALUE *argv, VALUE self) {
memcached_st *mc;
VALUE key, value, expiry, flags;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
rb_scan_args(argc, argv, "22", &key, &value, &expiry, &flags);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
value = StringValue(value);
result = memcached_replace(mc, RSTRING_PTR(key), RSTRING_LEN(key), RSTRING_PTR(value), RSTRING_LEN(value),
RTEST(expiry) ? NUM2UINT(expiry) : 0,
RTEST(flags) ? NUM2UINT(flags) : 0);
if (result == MEMCACHED_SUCCESS) {
return value;
} else if(result == MEMCACHED_NOTSTORED) {
return Qnil;
} else {
return throw_error(&result);
}
}
|
#set(*args) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'ext/native_server.c', line 322
VALUE mc_set(int argc, VALUE *argv, VALUE self) {
memcached_st *mc;
VALUE key, value, expiry, flags;
static memcached_return_t result;
Data_Get_Struct(self, memcached_st, mc);
rb_scan_args(argc, argv, "22", &key, &value, &expiry, &flags);
key = StringValue(key);
if (!use_binary(mc)) key = escape_key(key, NULL);
value = StringValue(value);
result = memcached_set(mc, RSTRING_PTR(key), RSTRING_LEN(key), RSTRING_PTR(value), RSTRING_LEN(value),
RTEST(expiry) ? NUM2UINT(expiry) : 0,
RTEST(flags) ? NUM2UINT(flags) : 0);
if (result == MEMCACHED_SUCCESS) {
return value;
} else {
return throw_error(&result);
}
}
|