Class: Memcached::Connection

Inherits:
Object
  • Object
show all
Includes:
Behaviors
Defined in:
lib/memcached/client.rb,
ext/memcached/memcached.c

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Behaviors

#get_behavior, #set_behavior, #set_behaviors

Class Method Details

.check_config!(rb_config) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'ext/memcached/memcached.c', line 98

static VALUE
rb_connection_check_cfg(VALUE klass, VALUE rb_config)
{
	char error_buffer[512];
	memcached_return_t rc;

	Check_Type(rb_config, T_STRING);

	rc = libmemcached_check_configuration(
		RSTRING_PTR(rb_config), RSTRING_LEN(rb_config),
		error_buffer, sizeof(error_buffer));

	if (rc != MEMCACHED_SUCCESS)
		rb_raise(rb_eArgError, "failed to parse config string\nconfig: '%s'\nmessage: %s",
				StringValueCStr(rb_config), error_buffer);

	return Qnil;
}

.new(rb_config) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/memcached/memcached.c', line 117

static VALUE
rb_connection_new(VALUE klass, VALUE rb_config)
{
	memcached_st *mc;
	Check_Type(rb_config, T_STRING);
	mc = memcached(RSTRING_PTR(rb_config), RSTRING_LEN(rb_config));
	if (!mc) {
		rb_connection_check_cfg(rb_cConnection, rb_config);
		rb_raise(rb_eNoMemError, "failed to allocate memcached");
	}
	return Data_Wrap_Struct(klass, NULL, memcached_free, mc);
}

Instance Method Details

#_get_behavior(rb_behavior) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/memcached/behavior.c', line 75

VALUE
rb_connection_get_behavior(VALUE self, VALUE rb_behavior)
{
	memcached_st *mc;
	memcached_behavior_t behavior;
	uint64_t ret;

	UnwrapMemcached(self, mc);
	Check_Type(rb_behavior, T_FIXNUM);

	behavior = FIX2INT(rb_behavior);
	ret = memcached_behavior_get(mc, behavior);

	switch (behavior) {
	case MEMCACHED_BEHAVIOR_HASH:
	case MEMCACHED_BEHAVIOR_DISTRIBUTION:
		return ULL2NUM(ret);
	default:
		return ret ? Qtrue : Qfalse;
	}
}

#_set_behavior(rb_behavior, rb_value) ⇒ Object


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
# File 'ext/memcached/behavior.c', line 97

VALUE
rb_connection_set_behavior(VALUE self, VALUE rb_behavior, VALUE rb_value)
{
	memcached_st *mc;
	memcached_return_t rc;
	memcached_behavior_t behavior;
	uint64_t value = 0;

	UnwrapMemcached(self, mc);
	Check_Type(rb_behavior, T_FIXNUM);

	behavior = FIX2INT(rb_behavior);

	switch (TYPE(rb_value)) {
	case T_TRUE:
		value = 1;
		break;
	case T_FIXNUM:
		value = FIX2INT(rb_value);
		break;
	case T_BIGNUM:
	case T_FLOAT:
		value = NUM2INT(rb_value);
		break;
	case T_NIL:
	case T_FALSE:
		break;
	default:
		rb_raise(rb_eTypeError, "unexpected type for 'behavior'");
		break;
	}

	rc = memcached_behavior_set(mc, behavior, value);
	rb_memcached_error_check(rc);

	return (rc == MEMCACHED_SUCCESS) ? Qtrue : Qfalse;
}

#add(rb_key, rb_value, rb_ttl, rb_flags) ⇒ Object


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'ext/memcached/memcached.c', line 303

static VALUE
rb_connection_add(VALUE self, VALUE rb_key, VALUE rb_value, VALUE rb_ttl, VALUE rb_flags)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);
	Check_Type(rb_value, T_STRING);
	Check_Type(rb_ttl, T_FIXNUM);
	Check_Type(rb_flags, T_FIXNUM);

	rc = memcached_add(mc,
			RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
			RSTRING_PTR(rb_value), RSTRING_LEN(rb_value),
			FIX2INT(rb_ttl), FIX2INT(rb_flags)
	);

	rb_memcached_return(rc);
}

#append(rb_key, rb_value, rb_ttl, rb_flags) ⇒ Object


417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'ext/memcached/memcached.c', line 417

static VALUE
rb_connection_append(VALUE self, VALUE rb_key, VALUE rb_value, VALUE rb_ttl, VALUE rb_flags)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);
	Check_Type(rb_value, T_STRING);
	Check_Type(rb_ttl, T_FIXNUM);
	Check_Type(rb_flags, T_FIXNUM);

	rc = memcached_append(mc,
		RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
		RSTRING_PTR(rb_value), RSTRING_LEN(rb_value),
		FIX2INT(rb_ttl), FIX2INT(rb_flags)
	);
	rb_memcached_return(rc);
}

#cloneObject


130
131
132
133
134
135
136
# File 'ext/memcached/memcached.c', line 130

static VALUE
rb_connection_clone(VALUE self)
{
	memcached_st *mc;
	UnwrapMemcached(self, mc);
	return Data_Wrap_Struct(rb_cConnection, NULL, memcached_free, memcached_clone(NULL, mc));
}

#closeObject


499
500
501
502
503
504
505
506
# File 'ext/memcached/memcached.c', line 499

static VALUE
rb_connection_close(VALUE self)
{
	memcached_st *mc;
	UnwrapMemcached(self, mc);
	memcached_quit(mc);
	return Qnil;
}

#decrement(rb_key, rb_value) ⇒ Object


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'ext/memcached/memcached.c', line 343

static VALUE
rb_connection_dec(VALUE self, VALUE rb_key, VALUE rb_value)
{
	memcached_st *mc;
	memcached_return_t rc;
	uint64_t result;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);
	Check_Type(rb_value, T_FIXNUM);

	rc = memcached_decrement(mc,
		RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
		(uint32_t)FIX2ULONG(rb_value), &result
	);
	rb_memcached_error_check(rc);
	return LL2NUM(result);
}

#delete(rb_key) ⇒ Object


290
291
292
293
294
295
296
297
298
299
300
301
# File 'ext/memcached/memcached.c', line 290

static VALUE
rb_connection_delete(VALUE self, VALUE rb_key)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);

	rc = memcached_delete(mc, RSTRING_PTR(rb_key), RSTRING_LEN(rb_key), (time_t)0);
	rb_memcached_return(rc);
}

#dupObject


130
131
132
133
134
135
136
# File 'ext/memcached/memcached.c', line 130

static VALUE
rb_connection_clone(VALUE self)
{
	memcached_st *mc;
	UnwrapMemcached(self, mc);
	return Data_Wrap_Struct(rb_cConnection, NULL, memcached_free, memcached_clone(NULL, mc));
}

#exist(rb_key) ⇒ Object


362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'ext/memcached/memcached.c', line 362

static VALUE
rb_connection_exist(VALUE self, VALUE rb_key)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);

	rc = memcached_exist(mc, RSTRING_PTR(rb_key), RSTRING_LEN(rb_key));
	rb_memcached_error_check(rc);
	return (rc == MEMCACHED_SUCCESS) ? Qtrue : Qfalse;
}

#exist?(rb_key) ⇒ Boolean

Returns:

  • (Boolean)

362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'ext/memcached/memcached.c', line 362

static VALUE
rb_connection_exist(VALUE self, VALUE rb_key)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);

	rc = memcached_exist(mc, RSTRING_PTR(rb_key), RSTRING_LEN(rb_key));
	rb_memcached_error_check(rc);
	return (rc == MEMCACHED_SUCCESS) ? Qtrue : Qfalse;
}

#flushObject


165
166
167
168
169
170
171
172
173
174
175
176
# File 'ext/memcached/memcached.c', line 165

static VALUE
rb_connection_flush(VALUE self)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	rc = memcached_flush(mc, 0);
	rb_memcached_error_check(rc);

	return (rc == MEMCACHED_SUCCESS) ? Qtrue : Qfalse;
}

#get(rb_key) ⇒ Object


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'ext/memcached/memcached.c', line 199

static VALUE
rb_connection_get(VALUE self, VALUE rb_key)
{
	VALUE rb_val;

	memcached_st *mc;
	memcached_return_t rc;
	size_t ret_len;
	uint32_t ret_flags;
	char *ret;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);

	ret = memcached_get(mc,
		RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
		&ret_len, &ret_flags, &rc);

	if (ret == NULL) {
		rb_memcached_error_check(rc);
		return Qnil;
	}

	rb_val = rb_str_new(ret, ret_len);
	free(ret);

	return rb_ary_new3(2, rb_val, INT2FIX(ret_flags));
}

#get_multi(rb_keys) ⇒ Object


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
# File 'ext/memcached/memcached.c', line 244

static VALUE
rb_connection_get_multi(VALUE self, VALUE rb_keys)
{
	memcached_execute_fn callbacks[1] = { &rb_connection__mget_callback };
	memcached_st *mc;
	memcached_return_t rc;

	long i, keys_len;
	const char **c_keys;
	size_t *c_lengths;

	VALUE rb_result;

	UnwrapMemcached(self, mc);

	Check_Type(rb_keys, T_ARRAY);
	keys_len = RARRAY_LEN(rb_keys);

	c_keys = alloca(keys_len * sizeof(const char *));
	c_lengths = alloca(keys_len * sizeof(size_t));

	for (i = 0; i < keys_len; i++) {
		VALUE rb_key = rb_ary_entry(rb_keys, i);
		Check_Type(rb_key, T_STRING);

		c_keys[i] = RSTRING_PTR(rb_key);
		c_lengths[i] = RSTRING_LEN(rb_key);
	}

	rb_result = rb_hash_new();

	if (memcached_behavior_get(mc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) {
		rc = memcached_mget_execute(mc, c_keys, c_lengths, (size_t)keys_len,
				callbacks, (void *)rb_result, 1);
		rb_memcached_error_check(rc);
	} else {
		rc = memcached_mget(mc, c_keys, c_lengths, (size_t)keys_len);
		rb_memcached_error_check(rc);
	}

	rc = memcached_fetch_execute(mc, callbacks, (void *)rb_result, 1);
	rb_memcached_error_check(rc);

	return rb_result;
}

#get_prefixObject


455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'ext/memcached/memcached.c', line 455

static VALUE
rb_connection_get_prefix(VALUE self)
{
	memcached_st *mc;
	memcached_return_t rc;
	const char *value;

	UnwrapMemcached(self, mc);

	value = memcached_callback_get(mc, MEMCACHED_CALLBACK_NAMESPACE, &rc);
	rb_memcached_error_check(rc);

	return value ? rb_str_new2(value) : Qnil;
}

#increment(rb_key, rb_value) ⇒ Object


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'ext/memcached/memcached.c', line 324

static VALUE
rb_connection_inc(VALUE self, VALUE rb_key, VALUE rb_value)
{
	memcached_st *mc;
	memcached_return_t rc;
	uint64_t result;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);
	Check_Type(rb_value, T_FIXNUM);

	rc = memcached_increment(mc,
		RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
		(uint32_t)FIX2ULONG(rb_value), &result
	);
	rb_memcached_error_check(rc);
	return LL2NUM(result);
}

#prepend(rb_key, rb_value, rb_ttl, rb_flags) ⇒ Object


397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'ext/memcached/memcached.c', line 397

static VALUE
rb_connection_prepend(VALUE self, VALUE rb_key, VALUE rb_value, VALUE rb_ttl, VALUE rb_flags)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);
	Check_Type(rb_value, T_STRING);
	Check_Type(rb_ttl, T_FIXNUM);
	Check_Type(rb_flags, T_FIXNUM);

	rc = memcached_prepend(mc,
		RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
		RSTRING_PTR(rb_value), RSTRING_LEN(rb_value),
		FIX2INT(rb_ttl), FIX2INT(rb_flags)
	);
	rb_memcached_return(rc);
}

#replace(rb_key, rb_value, rb_ttl, rb_flags) ⇒ Object


376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'ext/memcached/memcached.c', line 376

static VALUE
rb_connection_replace(VALUE self, VALUE rb_key, VALUE rb_value, VALUE rb_ttl, VALUE rb_flags)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);
	Check_Type(rb_value, T_STRING);
	Check_Type(rb_ttl, T_FIXNUM);
	Check_Type(rb_flags, T_FIXNUM);

	rc = memcached_replace(mc,
		RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
		RSTRING_PTR(rb_value), RSTRING_LEN(rb_value),
		FIX2INT(rb_ttl), FIX2INT(rb_flags)
	);
	rb_memcached_error_check(rc);
	return (rc == MEMCACHED_SUCCESS) ? Qtrue : Qfalse;
}

#serversObject


150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'ext/memcached/memcached.c', line 150

static VALUE
rb_connection_servers(VALUE self)
{
	memcached_server_fn callbacks[1] = { &iterate_server_function };
	memcached_st *mc;

	VALUE rb_result;

	UnwrapMemcached(self, mc);
	rb_result = rb_ary_new();

	memcached_server_cursor(mc, callbacks, (void *)rb_result, 1);
	return rb_result;
}

#set(rb_key, rb_value, rb_ttl, rb_flags) ⇒ Object


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'ext/memcached/memcached.c', line 178

static VALUE
rb_connection_set(VALUE self, VALUE rb_key, VALUE rb_value, VALUE rb_ttl, VALUE rb_flags)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_key, T_STRING);
	Check_Type(rb_value, T_STRING);
	Check_Type(rb_ttl, T_FIXNUM);
	Check_Type(rb_flags, T_FIXNUM);

	rc = memcached_set(mc,
		RSTRING_PTR(rb_key), RSTRING_LEN(rb_key),
		RSTRING_PTR(rb_value), RSTRING_LEN(rb_value),
		FIX2INT(rb_ttl), FIX2INT(rb_flags)
	);

	rb_memcached_return(rc);
}

#set_credentials(rb_username, rb_password) ⇒ Object


485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'ext/memcached/memcached.c', line 485

static VALUE
rb_set_credentials(VALUE self, VALUE rb_username, VALUE rb_password)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);
	Check_Type(rb_username, T_STRING);
	Check_Type(rb_password, T_STRING);

	rc = memcached_set_sasl_auth_data(mc, RSTRING_PTR(rb_username), RSTRING_PTR(rb_password));
	rb_memcached_return(rc);
}

#set_prefix(rb_prefix) ⇒ Object


437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'ext/memcached/memcached.c', line 437

static VALUE
rb_connection_set_prefix(VALUE self, VALUE rb_prefix)
{
	memcached_st *mc;
	memcached_return_t rc;
	const char *prefix = NULL;

	UnwrapMemcached(self, mc);

	if (!NIL_P(rb_prefix)) {
		Check_Type(rb_prefix, T_STRING);
		prefix = StringValueCStr(rb_prefix);
	}

	rc = memcached_callback_set(mc, MEMCACHED_CALLBACK_NAMESPACE, prefix);
	rb_memcached_return(rc);
}

#touch(rb_key, rb_ttl) ⇒ Object


470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'ext/memcached/memcached.c', line 470

static VALUE
rb_connection_touch(VALUE self, VALUE rb_key, VALUE rb_ttl)
{
	memcached_st *mc;
	memcached_return_t rc;

	UnwrapMemcached(self, mc);

	Check_Type(rb_key, T_STRING);
	Check_Type(rb_ttl, T_FIXNUM);

	rc = memcached_touch(mc, RSTRING_PTR(rb_key), RSTRING_LEN(rb_key), FIX2INT(rb_ttl));
	rb_memcached_return(rc);
}