ractor-cache 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ed3b5b4ab4307230b9dab1a21f269ac9d3bae8803f11bb209798df4de02e031
4
- data.tar.gz: ed75a15f8035a23955d1e25ce9c88e32a540881a2322ba0267c0c98302d50010
3
+ metadata.gz: 5e9137be1f90de7eed4e93838b8d0035747afd9b3f7be3211ab6777e0eded201
4
+ data.tar.gz: da7ccf5d0d16439ac04d2e8bc5763cc9883672717974fcc744a95bb69e6bb00c
5
5
  SHA512:
6
- metadata.gz: 63984280a9bc9d9530e86cc4d9c69987a4dbcc0128cf3525bf434006bbfc3b00515f5fb1e2aa65c85050cb37691dd666d7b39f1d9d05574078c29713501c8780
7
- data.tar.gz: 4e9725ab3449785e3b87a594c13c4175a083cc5910bf72c5fece2bcdcd4da7ce3c4ac4aa222553c6efceb8525a9490173e0f32274fdee13e6c9dfaeb01a0f878
6
+ metadata.gz: cbdd4316207650c65565de5c8dd6a2534689b9e237ba0b42583812248cef6ba764e218374ee45b256925aef52748436ff7e03d05eda93adba08412236f5882f8
7
+ data.tar.gz: d2fd27ff3e85f403fa8bfd010cd69b600dda81e09593855c48f31de80948e346f1c531101becc2c2b9df5708253b079314c3dc033e5360894141b2c047985f92
@@ -70,3 +70,7 @@ Lint/UselessAssignment:
70
70
  Layout/HashAlignment:
71
71
  Exclude:
72
72
  - 'lib/ractor/cache/cached_method.rb'
73
+
74
+ Style/GlobalVars:
75
+ Exclude:
76
+ - 'spec/**/*.rb'
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 use 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. Internally a `WeakMap` is used to make sure objects are still garbage collected as they should.
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
 
@@ -7,12 +7,13 @@ class Ractor
7
7
  module Cache
8
8
  require_relative_dir
9
9
 
10
- private def cache(method_name)
11
- CachingLayer[self].cache(method_name)
12
- end
10
+ CacheStore = ::Hash # By default, use a Hash
11
+ include CachingLayer
13
12
 
14
13
  refine Module do
15
- include Cache
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::Store.new
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::Store.new
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::Store)
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(:Store, false) || Cache::Store
63
- const_set(:Store, Class.new(substore))
62
+ substore = sublayer&.const_get(:CacheStore, false) || Cache::Store
63
+ const_set(:CacheStore, Class.new(substore))
64
64
 
65
65
  self
66
66
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Ractor
4
4
  module Cache
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ractor-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc-Andre Lafortune