module Stockpile::Cache

Stockpile::Cache

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

Public Instance Methods

get(db: :default, key:, compress: false) click to toggle source
# File lib/stockpile/cache.rb, line 25
def get(db: :default, key:, compress: false)
  value_from_cache = Stockpile.redis(db: db) { |r| r.get(key) }

  return unless value_from_cache

  if compress && value_from_cache
    Oj.load(Zlib::Inflate.inflate(Base64.decode64(value_from_cache)))
  else
    Oj.load(value_from_cache)
  end
end
get_deferred(db: :default, key:, compress: false) click to toggle source
# File lib/stockpile/cache.rb, line 37
def get_deferred(db: :default, key:, compress: false)
  sleep(Stockpile::SLUMBER_COOLDOWN) until Stockpile.redis(db: db) { |r| r.exists(key) }

  get(db: db, key: key, compress: compress)
end
set(db: :default, key:, payload:, ttl:, compress: false) click to toggle source
# File lib/stockpile/cache.rb, line 43
def set(db: :default, key:, payload:, ttl:, compress: false)
  payload = if compress
              Base64.encode64(Zlib::Deflate.deflate(Oj.dump(payload)))
            else
              Oj.dump(payload)
            end

  Stockpile.redis(db: db) { |r| r.setex(key, ttl, payload) }
end