Class: Pogocache::Extension

Inherits:
Object
  • Object
show all
Defined in:
ext/pogocache_ruby/pogocache_ruby.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

Pogocache.new(opts = {})



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 95

static VALUE pogocache_initialize(int argc, VALUE *argv, VALUE self) {
    VALUE opts_hash;
    rb_scan_args(argc, argv, "01", &opts_hash);
    
    struct pogocache_opts *opts = hash_to_opts(opts_hash);
    struct pogocache *cache = pogocache_new(opts);
    
    if (!cache) {
        rb_raise(ePogoError, "Failed to create pogocache instance");
    }
    
    pogocache_wrapper_t *wrapper = malloc(sizeof(pogocache_wrapper_t));
    wrapper->cache = cache;
    
    DATA_PTR(self) = wrapper;
    return self;
}

Instance Method Details

#clear(*args) ⇒ Object

extension.clear(opts = {})



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 320

static VALUE pogocache_clear_rb(int argc, VALUE *argv, VALUE self) {
    VALUE opts_hash;
    rb_scan_args(argc, argv, "01", &opts_hash);
    
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    
    struct pogocache_clear_opts opts;
    memset(&opts, 0, sizeof(opts));
    
    if (!NIL_P(opts_hash)) {
        Check_Type(opts_hash, T_HASH);
        
        VALUE val;
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshard")));
        if (!NIL_P(val)) opts.oneshard = RTEST(val);
        
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshardidx")));
        if (!NIL_P(val)) opts.oneshardidx = NUM2INT(val);
    }
    
    pogocache_clear(wrapper->cache, &opts);
    return Qnil;
}

#count(*args) ⇒ Object Also known as: length

extension.count(opts = {})



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 265

static VALUE pogocache_count_rb(int argc, VALUE *argv, VALUE self) {
    VALUE opts_hash;
    rb_scan_args(argc, argv, "01", &opts_hash);
    
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    
    struct pogocache_count_opts opts;
    memset(&opts, 0, sizeof(opts));
    
    if (!NIL_P(opts_hash)) {
        Check_Type(opts_hash, T_HASH);
        
        VALUE val;
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshard")));
        if (!NIL_P(val)) opts.oneshard = RTEST(val);
        
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshardidx")));
        if (!NIL_P(val)) opts.oneshardidx = NUM2INT(val);
    }
    
    size_t count = pogocache_count(wrapper->cache, &opts);
    return SIZET2NUM(count);
}

#delete(*args) ⇒ Object

extension.delete(key, opts = {})



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 246

static VALUE pogocache_delete_rb(int argc, VALUE *argv, VALUE self) {
    VALUE key, opts_hash;
    rb_scan_args(argc, argv, "11", &key, &opts_hash);
    
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    
    Check_Type(key, T_STRING);
    const char *key_ptr = RSTRING_PTR(key);
    size_t key_len = RSTRING_LEN(key);
    
    struct pogocache_delete_opts opts;
    memset(&opts, 0, sizeof(opts));
    
    int result = pogocache_delete(wrapper->cache, key_ptr, key_len, &opts);
    return INT2NUM(result);
}

#each(*args) ⇒ Object

extension.each(opts = {}) # returns array



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 411

static VALUE pogocache_each_rb(int argc, VALUE *argv, VALUE self) {
    VALUE opts_hash, proc;
    rb_scan_args(argc, argv, "01&", &opts_hash, &proc);
    
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    
    struct pogocache_iter_opts opts;
    memset(&opts, 0, sizeof(opts));
    
    iter_callback_data_t callback_data;
    callback_data.result_array = rb_ary_new();
    callback_data.proc = proc;
    
    opts.entry = iter_entry_callback;
    opts.udata = &callback_data;
    
    if (!NIL_P(opts_hash)) {
        Check_Type(opts_hash, T_HASH);
        
        VALUE val;
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshard")));
        if (!NIL_P(val)) opts.oneshard = RTEST(val);
        
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshardidx")));
        if (!NIL_P(val)) opts.oneshardidx = NUM2INT(val);
    }
    
    pogocache_iter(wrapper->cache, &opts);
    
    if (!NIL_P(proc)) {
        return self;
    } else {
        return callback_data.result_array;
    }
}

#load(*args) ⇒ Object Also known as: []

extension.load(key, opts = {})



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 205

static VALUE pogocache_load_rb(int argc, VALUE *argv, VALUE self) {
    VALUE key, opts_hash;
    rb_scan_args(argc, argv, "11", &key, &opts_hash);
    
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    
    Check_Type(key, T_STRING);
    const char *key_ptr = RSTRING_PTR(key);
    size_t key_len = RSTRING_LEN(key);
    
    struct pogocache_load_opts opts;
    memset(&opts, 0, sizeof(opts));
    
    load_callback_data_t callback_data;
    callback_data.result_hash = Qnil;
    callback_data.found = false;
    
    opts.entry = load_entry_callback;
    opts.udata = &callback_data;
    
    if (!NIL_P(opts_hash)) {
        Check_Type(opts_hash, T_HASH);
        
        VALUE val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("notouch")));
        if (!NIL_P(val)) opts.notouch = RTEST(val);
    }
    
    int result = pogocache_load(wrapper->cache, key_ptr, key_len, &opts);
    if (result != POGOCACHE_FOUND) {
        return Qnil;
    }
    
    if (callback_data.found) {
        return callback_data.result_hash;
    } else {
        return Qnil;
    }
}

#nshardsObject

extension.nshards



449
450
451
452
453
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 449

static VALUE pogocache_nshards_rb(VALUE self) {
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    int nshards = pogocache_nshards(wrapper->cache);
    return INT2NUM(nshards);
}

#size(*args) ⇒ Object

extension.size(opts = {})



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

static VALUE pogocache_size_rb(int argc, VALUE *argv, VALUE self) {
    VALUE opts_hash;
    rb_scan_args(argc, argv, "01", &opts_hash);
    
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    
    struct pogocache_size_opts opts;
    memset(&opts, 0, sizeof(opts));
    
    if (!NIL_P(opts_hash)) {
        Check_Type(opts_hash, T_HASH);
        
        VALUE val;
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshard")));
        if (!NIL_P(val)) opts.oneshard = RTEST(val);
        
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshardidx")));
        if (!NIL_P(val)) opts.oneshardidx = NUM2INT(val);
        
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("entriesonly")));
        if (!NIL_P(val)) opts.entriesonly = RTEST(val);
    }
    
    size_t size = pogocache_size(wrapper->cache, &opts);
    return SIZET2NUM(size);
}

#store(*args) ⇒ Object Also known as: []=

extension.store(key, value, opts = {})



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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 115

static VALUE pogocache_store_rb(int argc, VALUE *argv, VALUE self) {
    VALUE key, value, opts_hash;
    rb_scan_args(argc, argv, "21", &key, &value, &opts_hash);
    
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    
    // Convert Ruby strings to C strings
    Check_Type(key, T_STRING);
    Check_Type(value, T_STRING);
    
    const char *key_ptr = RSTRING_PTR(key);
    size_t key_len = RSTRING_LEN(key);
    const char *value_ptr = RSTRING_PTR(value);
    size_t value_len = RSTRING_LEN(value);
    
    // Parse options
    struct pogocache_store_opts opts;
    memset(&opts, 0, sizeof(opts));
    
    if (!NIL_P(opts_hash)) {
        Check_Type(opts_hash, T_HASH);
        
        VALUE val;
        
        // ttl
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("ttl")));
        if (!NIL_P(val)) opts.ttl = NUM2LL(val);
        
        // expires
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("expires")));
        if (!NIL_P(val)) opts.expires = NUM2LL(val);
        
        // flags
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("flags")));
        if (!NIL_P(val)) opts.flags = NUM2UINT(val);
        
        // cas
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("cas")));
        if (!NIL_P(val)) opts.cas = NUM2ULL(val);
        
        // nx (only set if not exists)
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("nx")));
        if (!NIL_P(val)) opts.nx = RTEST(val);
        
        // xx (only set if exists)
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("xx")));
        if (!NIL_P(val)) opts.xx = RTEST(val);
        
        // keepttl
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("keepttl")));
        if (!NIL_P(val)) opts.keepttl = RTEST(val);
        
        // casop
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("casop")));
        if (!NIL_P(val)) opts.casop = RTEST(val);
    }
    
    int result = pogocache_store(wrapper->cache, key_ptr, key_len, value_ptr, value_len, &opts);
    return INT2NUM(result);
}

#sweep(*args) ⇒ Object

extension.sweep(opts = {})



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'ext/pogocache_ruby/pogocache_ruby.c', line 346

static VALUE pogocache_sweep_rb(int argc, VALUE *argv, VALUE self) {
    VALUE opts_hash;
    rb_scan_args(argc, argv, "01", &opts_hash);
    
    pogocache_wrapper_t *wrapper = get_pogocache_wrapper(self);
    
    struct pogocache_sweep_opts opts;
    memset(&opts, 0, sizeof(opts));
    
    if (!NIL_P(opts_hash)) {
        Check_Type(opts_hash, T_HASH);
        
        VALUE val;
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshard")));
        if (!NIL_P(val)) opts.oneshard = RTEST(val);
        
        val = rb_hash_aref(opts_hash, ID2SYM(rb_intern("oneshardidx")));
        if (!NIL_P(val)) opts.oneshardidx = NUM2INT(val);
    }
    
    size_t swept = 0, kept = 0;
    pogocache_sweep(wrapper->cache, &swept, &kept, &opts);
    
    VALUE result = rb_hash_new();
    rb_hash_aset(result, ID2SYM(rb_intern("swept")), SIZET2NUM(swept));
    rb_hash_aset(result, ID2SYM(rb_intern("kept")), SIZET2NUM(kept));
    
    return result;
}