Method: Delorean::Cache.with_cache

Defined in:
lib/delorean/cache.rb

.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