Method: Marty::CacheAdapters::MemoryAndRedis#fetch_item

Defined in:
lib/marty/cache_adapters/memory_and_redis.rb

#fetch_item(klass:, cache_key:, default: nil) ⇒ Object

When cache is found in local memory, we simply return the cached item. Otherwise we look into Redis, if item is cached there, we copy it to local memory to speed up future lookups.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/marty/cache_adapters/memory_and_redis.rb', line 42

def fetch_item(klass:, cache_key:, default: nil)
  memory_item = @memory_adapter.fetch_item(
    klass: klass,
    cache_key: cache_key,
    default: default
  )

  return memory_item if memory_item != default

  redis_item = @redis_adapter.fetch_item(
    klass: klass,
    cache_key: cache_key,
    default: default
  )

  return default if redis_item == default

  @memory_adapter.cache_item(
    klass: klass,
    cache_key: cache_key,
    item: redis_item
  )

  redis_item
end