Class: VolatileMap
- Inherits:
-
Object
- Object
- VolatileMap
- Defined in:
- lib/volatile_map/version.rb,
ext/volatile_map/volatile_map.c
Constant Summary collapse
- VERSION =
"0.3.0"
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #has_key?(key) ⇒ Boolean
- #include?(key) ⇒ Boolean
- #initialize(*args) ⇒ Object constructor
- #key?(key) ⇒ Boolean
- #length ⇒ Object
- #member?(key) ⇒ Boolean
- #size ⇒ Object
- #strict? ⇒ Boolean
- #ttl ⇒ Object
Constructor Details
#initialize(*args) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'ext/volatile_map/volatile_map.c', line 148
static VALUE vm_initialize(int argc, VALUE *argv, VALUE self) {
VolatileMap *vm = vm_get(self);
VALUE opts;
VALUE kwargs[2];
ID keys[2] = {ttl_id, strict_id};
rb_scan_args(argc, argv, ":", &opts);
rb_get_kwargs(opts, keys, 1, 1, kwargs);
double ttl_value = NUM2DBL(kwargs[0]);
VALUE is_strict = kwargs[1];
if(ttl_value <= 0.0) rb_raise(rb_eArgError, "TTL must be positive");
vm->ttl = ttl_value;
vm->is_strict = (is_strict != Qundef && RTEST(is_strict));
return self;
}
|
Instance Method Details
#[](key) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'ext/volatile_map/volatile_map.c', line 186
static VALUE vm_aref(VALUE self, VALUE key) {
VolatileMap *vm = vm_get(self);
VALUE entry = rb_hash_lookup2(vm->storage, key, Qundef);
if(entry == Qundef) return Qnil;
double now = now_seconds();
if(vm->is_strict && is_stale(vm->ttl, entry, now)) {
rb_hash_delete(vm->storage, key);
return Qnil;
}
rb_ary_store(entry, 1, DBL2NUM(now));
return RARRAY_AREF(entry, 0);
}
|
#[]=(key, value) ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'ext/volatile_map/volatile_map.c', line 177
static VALUE vm_aset(VALUE self, VALUE key, VALUE value) {
VolatileMap *vm = vm_get(self);
VALUE entry = rb_ary_new_from_args(2, value, DBL2NUM(now_seconds()));
rb_hash_aset(vm->storage, key, entry);
return value;
}
|
#delete(key) ⇒ Object
205 206 207 208 209 210 211 212 |
# File 'ext/volatile_map/volatile_map.c', line 205
static VALUE vm_delete(VALUE self, VALUE key) {
VolatileMap *vm = vm_get(self);
VALUE entry = rb_hash_delete(vm->storage, key);
if(NIL_P(entry) || !RB_TYPE_P(entry, T_ARRAY) || RARRAY_LEN(entry) < 1) return Qnil;
return RARRAY_AREF(entry, 0);
}
|
#has_key?(key) ⇒ Boolean
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'ext/volatile_map/volatile_map.c', line 222
static VALUE vm_key(VALUE self, VALUE key) {
VolatileMap *vm = vm_get(self);
VALUE entry = rb_hash_lookup2(vm->storage, key, Qundef);
if(entry == Qundef) return Qfalse;
double now = now_seconds();
if(is_stale(vm->ttl, entry, now)) {
rb_hash_delete(vm->storage, key);
return Qfalse;
}
rb_ary_store(entry, 1, DBL2NUM(now));
return Qtrue;
}
|
#include?(key) ⇒ Boolean
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'ext/volatile_map/volatile_map.c', line 222
static VALUE vm_key(VALUE self, VALUE key) {
VolatileMap *vm = vm_get(self);
VALUE entry = rb_hash_lookup2(vm->storage, key, Qundef);
if(entry == Qundef) return Qfalse;
double now = now_seconds();
if(is_stale(vm->ttl, entry, now)) {
rb_hash_delete(vm->storage, key);
return Qfalse;
}
rb_ary_store(entry, 1, DBL2NUM(now));
return Qtrue;
}
|
#key?(key) ⇒ Boolean
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'ext/volatile_map/volatile_map.c', line 222
static VALUE vm_key(VALUE self, VALUE key) {
VolatileMap *vm = vm_get(self);
VALUE entry = rb_hash_lookup2(vm->storage, key, Qundef);
if(entry == Qundef) return Qfalse;
double now = now_seconds();
if(is_stale(vm->ttl, entry, now)) {
rb_hash_delete(vm->storage, key);
return Qfalse;
}
rb_ary_store(entry, 1, DBL2NUM(now));
return Qtrue;
}
|
#length ⇒ Object
214 215 216 217 218 219 220 |
# File 'ext/volatile_map/volatile_map.c', line 214
static VALUE vm_size(VALUE self) {
VolatileMap *vm = vm_get(self);
if(NIL_P(vm->storage)) return INT2FIX(0);
return ULONG2NUM(RHASH_SIZE(vm->storage));
}
|
#member?(key) ⇒ Boolean
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'ext/volatile_map/volatile_map.c', line 222
static VALUE vm_key(VALUE self, VALUE key) {
VolatileMap *vm = vm_get(self);
VALUE entry = rb_hash_lookup2(vm->storage, key, Qundef);
if(entry == Qundef) return Qfalse;
double now = now_seconds();
if(is_stale(vm->ttl, entry, now)) {
rb_hash_delete(vm->storage, key);
return Qfalse;
}
rb_ary_store(entry, 1, DBL2NUM(now));
return Qtrue;
}
|
#size ⇒ Object
214 215 216 217 218 219 220 |
# File 'ext/volatile_map/volatile_map.c', line 214
static VALUE vm_size(VALUE self) {
VolatileMap *vm = vm_get(self);
if(NIL_P(vm->storage)) return INT2FIX(0);
return ULONG2NUM(RHASH_SIZE(vm->storage));
}
|
#strict? ⇒ Boolean
173 174 175 |
# File 'ext/volatile_map/volatile_map.c', line 173
static VALUE vm_strict(VALUE self) {
return vm_get(self)->is_strict ? Qtrue : Qfalse;
}
|
#ttl ⇒ Object
169 170 171 |
# File 'ext/volatile_map/volatile_map.c', line 169
static VALUE vm_ttl(VALUE self) {
return DBL2NUM(vm_get(self)->ttl);
}
|