Module: Ractor::Cache::CachingLayer

Defined in:
lib/ractor/cache/caching_layer.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.cachedObject (readonly)

Returns the value of attribute cached.



16
17
18
# File 'lib/ractor/cache/caching_layer.rb', line 16

def cached
  @cached
end

.parentObject (readonly)

Returns the value of attribute parent.



16
17
18
# File 'lib/ractor/cache/caching_layer.rb', line 16

def parent
  @parent
end

.sublayerObject (readonly)

Returns the value of attribute sublayer.



16
17
18
# File 'lib/ractor/cache/caching_layer.rb', line 16

def sublayer
  @sublayer
end

Class Method Details

.[](mod) ⇒ Object

Return CachingLayer in mod, creating it if need be.



55
56
57
58
59
60
61
62
# File 'lib/ractor/cache/caching_layer.rb', line 55

def [](mod)
  if mod.const_defined?(:RactorCacheLayer, false)
    mod::RactorCacheLayer
  else
    sublayer = mod::RactorCacheLayer if mod.const_defined?(:RactorCacheLayer, true)
    mod.const_set(:RactorCacheLayer, dup.attach(mod, sublayer))
  end
end

.attach(mod, sublayer) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/ractor/cache/caching_layer.rb', line 18

def attach(mod, sublayer)
  @sublayer = sublayer
  @cached = []
  @parent = mod
  mod.prepend self
  substore = sublayer&.const_get(:Store, false) || Cache::Store
  const_set(:Store, Class.new(substore))

  self
end

.cache(method, strategy:) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ractor/cache/caching_layer.rb', line 29

def cache(method, strategy:)
  strat = build_stragegy(method, strategy)
  file, line = strat.method(:compile_accessor).source_location
  module_eval(strat.compile_accessor, file, line + 2)
  @cached << strat
  self::Store.update(@cached)
end

.deep_freeze_callback(instance) ⇒ Object



37
38
39
40
41
42
# File 'lib/ractor/cache/caching_layer.rb', line 37

def deep_freeze_callback(instance)
  @cached.each do |strategy|
    strategy.deep_freeze_callback(instance)
  end
  sublayer&.deep_freeze_callback(instance)
end

Instance Method Details

#freezeObject



6
7
8
9
# File 'lib/ractor/cache/caching_layer.rb', line 6

def freeze
  ractor_cache # Make sure the instance variable is created beforehand
  super
end