ractor-cache 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/README.md +11 -2
- data/lib/ractor/cache.rb +5 -4
- data/lib/ractor/cache/caching_layer.rb +6 -6
- data/lib/ractor/cache/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: 5e9137be1f90de7eed4e93838b8d0035747afd9b3f7be3211ab6777e0eded201
|
4
|
+
data.tar.gz: da7ccf5d0d16439ac04d2e8bc5763cc9883672717974fcc744a95bb69e6bb00c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbdd4316207650c65565de5c8dd6a2534689b9e237ba0b42583812248cef6ba764e218374ee45b256925aef52748436ff7e03d05eda93adba08412236f5882f8
|
7
|
+
data.tar.gz: d2fd27ff3e85f403fa8bfd010cd69b600dda81e09593855c48f31de80948e346f1c531101becc2c2b9df5708253b079314c3dc033e5360894141b2c047985f92
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -18,12 +18,21 @@ class Foo
|
|
18
18
|
do_long_calculation
|
19
19
|
end
|
20
20
|
end
|
21
|
+
|
22
|
+
# Lower level "manual" usage (discussed later):
|
23
|
+
class Foo
|
24
|
+
include Ractor::Cache # => defines a `ractor_cache` method that returns a hash
|
25
|
+
|
26
|
+
def long_calc
|
27
|
+
ractor_cache[:long_calc] ||= do_long_calculation
|
28
|
+
end
|
29
|
+
end
|
21
30
|
```
|
22
31
|
|
23
32
|
## Why?
|
24
33
|
|
25
34
|
0) It's pretty
|
26
|
-
1) Handles `nil` / `false` results
|
35
|
+
1) Handles `nil` / `false` results ("manual" usage: DIY)
|
27
36
|
2) Works even for frozen instances
|
28
37
|
3) Works even for deeply frozen instances (`Ractor`-shareable).
|
29
38
|
|
@@ -71,7 +80,7 @@ foo.long_calc # => `FrozenError`, @cache is frozen
|
|
71
80
|
|
72
81
|
## How to resolve this
|
73
82
|
|
74
|
-
This gem will
|
83
|
+
This gem will associate a mutable data structure to the instance. Even if deeply-frozen it can still mutate the data structure. The data is Ractor-local, so it won't be shared and won't cause issues. A `WeakMap` is used to make sure objects are still garbage collected as they should.
|
75
84
|
|
76
85
|
Implementation details [explained here](hacker_guide.md)
|
77
86
|
|
data/lib/ractor/cache.rb
CHANGED
@@ -7,12 +7,13 @@ class Ractor
|
|
7
7
|
module Cache
|
8
8
|
require_relative_dir
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
end
|
10
|
+
CacheStore = ::Hash # By default, use a Hash
|
11
|
+
include CachingLayer
|
13
12
|
|
14
13
|
refine Module do
|
15
|
-
|
14
|
+
def cache(method_name)
|
15
|
+
CachingLayer[self].cache(method_name)
|
16
|
+
end
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
@@ -12,16 +12,16 @@ class Ractor
|
|
12
12
|
|
13
13
|
if use_ractor_storage
|
14
14
|
private def ractor_cache
|
15
|
-
CachingLayer.ractor_storage[self] ||= self.class::
|
15
|
+
CachingLayer.ractor_storage[self] ||= self.class::CacheStore.new
|
16
16
|
end
|
17
17
|
else
|
18
18
|
def freeze
|
19
|
-
ractor_cache
|
19
|
+
ractor_cache # make sure cache is initialized before freezing
|
20
20
|
super
|
21
21
|
end
|
22
22
|
|
23
23
|
private def ractor_cache
|
24
|
-
@ractor_cache ||= self.class::
|
24
|
+
@ractor_cache ||= self.class::CacheStore.new
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -38,7 +38,7 @@ class Ractor
|
|
38
38
|
file, line = cm.method(:compile_method).source_location
|
39
39
|
module_eval(cm.compile_method, file, line + 2)
|
40
40
|
@cached << cm
|
41
|
-
update_store_methods(self::
|
41
|
+
update_store_methods(self::CacheStore)
|
42
42
|
end
|
43
43
|
|
44
44
|
private def update_store_methods(store)
|
@@ -59,8 +59,8 @@ class Ractor
|
|
59
59
|
@cached = []
|
60
60
|
@parent = mod
|
61
61
|
mod.prepend self
|
62
|
-
substore = sublayer&.const_get(:
|
63
|
-
const_set(:
|
62
|
+
substore = sublayer&.const_get(:CacheStore, false) || Cache::Store
|
63
|
+
const_set(:CacheStore, Class.new(substore))
|
64
64
|
|
65
65
|
self
|
66
66
|
end
|
data/lib/ractor/cache/version.rb
CHANGED