Module: Stockpile::Cache

Defined in:
lib/stockpile/cache.rb

Overview

Stockpile::Cache

Wrapper around Stockpile.redis used for writing and reading from it; handles serialization and deserialization of data upon writes and reads.

Class Method Summary collapse

Class Method Details

.get(key:) ⇒ Object



25
26
27
28
# File 'lib/stockpile/cache.rb', line 25

def get(key:)
  value_from_cache = Stockpile.redis { |r| r.get(key) }
  Oj.load(value_from_cache) if value_from_cache
end

.get_deferred(key:) ⇒ Object



30
31
32
33
34
# File 'lib/stockpile/cache.rb', line 30

def get_deferred(key:)
  sleep(Stockpile::SLUMBER_COOLDOWN) until Stockpile.redis { |r| r.exists(key) }
  value_from_cache = Stockpile.redis { |r| r.get(key) }
  Oj.load(value_from_cache)
end

.set(key:, payload:, ttl:) ⇒ Object



36
37
38
39
40
# File 'lib/stockpile/cache.rb', line 36

def set(key:, payload:, ttl:)
  payload = Oj.dump(payload)
  Stockpile.redis { |r| r.set(key, payload) }
  Stockpile.redis { |r| r.expire(key, ttl) }
end