Module: Delorean::Cache

Defined in:
lib/delorean/cache.rb,
lib/delorean/cache/adapters.rb,
lib/delorean/cache/adapters/base.rb,
lib/delorean/cache/adapters/no_cache.rb,
lib/delorean/cache/adapters/ruby_cache.rb

Defined Under Namespace

Modules: Adapters

Constant Summary collapse

NODE_CACHE_DEFAULT_CALLBACK =
lambda do |klass:, method:, params:|
  {
    cache: true,
  }
end

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adapterObject

Returns the value of attribute adapter.



14
15
16
# File 'lib/delorean/cache.rb', line 14

def adapter
  @adapter
end

.node_cache_callbackObject

Returns the value of attribute node_cache_callback.



50
51
52
# File 'lib/delorean/cache.rb', line 50

def node_cache_callback
  @node_cache_callback
end

Class Method Details

.with_cache(klass:, method:, mutable_params:, params:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/delorean/cache.rb', line 16

def with_cache(klass:, method:, mutable_params:, params:)
  delorean_cache_adapter = ::Delorean::Cache.adapter

  klass_name = "#{klass.name}#{::Delorean::POST}"

  cache_options = node_cache_callback.call(
    klass: klass,
    method: method,
    params: mutable_params
  )

  return yield unless cache_options[:cache]

  cache_key = delorean_cache_adapter.cache_key(
    klass: klass_name, method_name: method, args: [params]
  )

  cached_item = delorean_cache_adapter.fetch_item(
    klass: klass_name, cache_key: cache_key, default: :NF
  )

  return cached_item if cached_item != :NF

  res = yield

  delorean_cache_adapter.cache_item(
    klass: klass_name,
    cache_key: cache_key,
    item: res,
  )

  res
end