volatile_map 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/ext/volatile_map/volatile_map.c +27 -8
- data/ext/volatile_map/volatile_map.h +1 -0
- data/lib/volatile_map/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1a2771a72fbf86ca6d707d5e73d7ec3bc413f6f87b2c240a2dce1d89ccaf3a27
|
|
4
|
+
data.tar.gz: c03992f176a51f3aee3bac6a6fe31fbc4c9d35154451350c4706fc0d5f91955c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3b53c5a983029d81b43c7b61e03b978d9c12af401e187d9e41e041d84a2eabb0ea8198ab9c0d46615f7bf333d9e25108586be86e2ddfbfe00ec03354bb9630fe
|
|
7
|
+
data.tar.gz: f1235f6e0088e7ac8c4dca138741e32f65f07aaa8228d2ed1f266382bf4c14d6bbda0539d610d8dcea98c0d970ccea1728b457536d984ca886785e767747acb9
|
data/README.md
CHANGED
|
@@ -26,7 +26,7 @@ gem install volatile_map
|
|
|
26
26
|
require "volatile_map"
|
|
27
27
|
|
|
28
28
|
# TTL is in seconds (Float or Integer)
|
|
29
|
-
cache = VolatileMap.new(0.5)
|
|
29
|
+
cache = VolatileMap.new(ttl: 0.5)
|
|
30
30
|
|
|
31
31
|
cache["session"] = { user_id: 42 }
|
|
32
32
|
cache["session"] # => { user_id: 42 }
|
|
@@ -42,7 +42,10 @@ cache["session"] # => nil
|
|
|
42
42
|
|
|
43
43
|
### API
|
|
44
44
|
|
|
45
|
-
- `VolatileMap.new(ttl)` — construct a map with the given TTL
|
|
45
|
+
- `VolatileMap.new(ttl:, strict: false)` — construct a map with the given TTL
|
|
46
|
+
in seconds. The optional `strict:` keyword is stored on the instance and
|
|
47
|
+
exposed via `strict?`; when `true`, the keys will return `nil` when they
|
|
48
|
+
expire even if they are not claimed by the VM.
|
|
46
49
|
- `map[key] = value` — store a value; resets the entry's timestamp.
|
|
47
50
|
- `map[key]` — retrieve a value; returns `nil` if missing or expired.
|
|
48
51
|
Refreshes the timestamp on a hit.
|
|
@@ -51,6 +54,7 @@ cache["session"] # => nil
|
|
|
51
54
|
Also evicts and refreshes like `[]`.
|
|
52
55
|
- `map.size` — number of entries currently stored.
|
|
53
56
|
- `map.ttl` — the configured TTL in seconds.
|
|
57
|
+
- `map.strict?` — whether the map was constructed with `strict: true`.
|
|
54
58
|
|
|
55
59
|
### Semantics
|
|
56
60
|
|
|
@@ -18,6 +18,8 @@ static size_t vm_live_count = 0;
|
|
|
18
18
|
static rb_postponed_job_handle_t vm_drain_job_handle;
|
|
19
19
|
#endif
|
|
20
20
|
|
|
21
|
+
static ID ttl_id, strict_id;
|
|
22
|
+
|
|
21
23
|
struct vm_drain_ctx {
|
|
22
24
|
double now;
|
|
23
25
|
double ttl;
|
|
@@ -96,10 +98,8 @@ static void rb_compact_volatile_map(void *ptr) {
|
|
|
96
98
|
vm->storage = rb_gc_location(vm->storage);
|
|
97
99
|
}
|
|
98
100
|
|
|
99
|
-
static size_t rb_size_volatile_map(const void *
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return sizeof(VolatileMap) + rb_funcall(vm->storage, rb_intern("size"), 0);
|
|
101
|
+
static size_t rb_size_volatile_map(const void *_ptr) {
|
|
102
|
+
return sizeof(VolatileMap);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
const rb_data_type_t volatile_map_type = {
|
|
@@ -130,6 +130,7 @@ VALUE alloc_volatile_map(VALUE klass) {
|
|
|
130
130
|
|
|
131
131
|
vm->storage = rb_hash_new();
|
|
132
132
|
vm->ttl = 0.0;
|
|
133
|
+
vm->is_strict = false;
|
|
133
134
|
vm_live_count++;
|
|
134
135
|
|
|
135
136
|
ensure_dirty_capacity();
|
|
@@ -144,13 +145,23 @@ static VolatileMap *vm_get(VALUE self) {
|
|
|
144
145
|
return vm;
|
|
145
146
|
}
|
|
146
147
|
|
|
147
|
-
static VALUE vm_initialize(VALUE
|
|
148
|
+
static VALUE vm_initialize(int argc, VALUE *argv, VALUE self) {
|
|
148
149
|
VolatileMap *vm = vm_get(self);
|
|
149
|
-
|
|
150
|
+
VALUE opts;
|
|
151
|
+
VALUE kwargs[2];
|
|
152
|
+
ID keys[2] = {ttl_id, strict_id};
|
|
153
|
+
|
|
154
|
+
rb_scan_args(argc, argv, ":", &opts);
|
|
155
|
+
|
|
156
|
+
rb_get_kwargs(opts, keys, 1, 1, kwargs);
|
|
157
|
+
|
|
158
|
+
double ttl_value = NUM2DBL(kwargs[0]);
|
|
159
|
+
VALUE is_strict = kwargs[1];
|
|
150
160
|
|
|
151
161
|
if(ttl_value <= 0.0) rb_raise(rb_eArgError, "TTL must be positive");
|
|
152
162
|
|
|
153
163
|
vm->ttl = ttl_value;
|
|
164
|
+
vm->is_strict = (is_strict != Qundef && RTEST(is_strict));
|
|
154
165
|
|
|
155
166
|
return self;
|
|
156
167
|
}
|
|
@@ -159,6 +170,10 @@ static VALUE vm_ttl(VALUE self) {
|
|
|
159
170
|
return DBL2NUM(vm_get(self)->ttl);
|
|
160
171
|
}
|
|
161
172
|
|
|
173
|
+
static VALUE vm_strict(VALUE self) {
|
|
174
|
+
return vm_get(self)->is_strict ? Qtrue : Qfalse;
|
|
175
|
+
}
|
|
176
|
+
|
|
162
177
|
static VALUE vm_aset(VALUE self, VALUE key, VALUE value) {
|
|
163
178
|
VolatileMap *vm = vm_get(self);
|
|
164
179
|
VALUE entry = rb_ary_new_from_args(2, value, DBL2NUM(now_seconds()));
|
|
@@ -176,7 +191,7 @@ static VALUE vm_aref(VALUE self, VALUE key) {
|
|
|
176
191
|
|
|
177
192
|
double now = now_seconds();
|
|
178
193
|
|
|
179
|
-
if(is_stale(vm->ttl, entry, now)) {
|
|
194
|
+
if(vm->is_strict && is_stale(vm->ttl, entry, now)) {
|
|
180
195
|
rb_hash_delete(vm->storage, key);
|
|
181
196
|
|
|
182
197
|
return Qnil;
|
|
@@ -226,12 +241,16 @@ static VALUE vm_key(VALUE self, VALUE key) {
|
|
|
226
241
|
void Init_volatile_map(void) {
|
|
227
242
|
rb_require("volatile_map/version");
|
|
228
243
|
|
|
244
|
+
ttl_id = rb_intern("ttl");
|
|
245
|
+
strict_id = rb_intern("strict");
|
|
246
|
+
|
|
229
247
|
VALUE volatile_map_class = rb_define_class("VolatileMap", rb_cObject);
|
|
230
248
|
|
|
231
249
|
rb_define_alloc_func(volatile_map_class, alloc_volatile_map);
|
|
232
250
|
|
|
233
|
-
rb_define_method(volatile_map_class, "initialize", vm_initialize, 1);
|
|
251
|
+
rb_define_method(volatile_map_class, "initialize", vm_initialize, -1);
|
|
234
252
|
rb_define_method(volatile_map_class, "ttl", vm_ttl, 0);
|
|
253
|
+
rb_define_method(volatile_map_class, "strict?", vm_strict, 0);
|
|
235
254
|
rb_define_method(volatile_map_class, "[]=", vm_aset, 2);
|
|
236
255
|
rb_define_method(volatile_map_class, "[]", vm_aref, 1);
|
|
237
256
|
rb_define_method(volatile_map_class, "delete", vm_delete, 1);
|
data/lib/volatile_map/version.rb
CHANGED