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
- .get(db: :default, key:, compress: false) ⇒ Object
- .get_deferred(db: :default, key:, compress: false) ⇒ Object
- .set(db: :default, key:, payload:, ttl:, compress: false) ⇒ Object
Class Method Details
.get(db: :default, key:, compress: false) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# 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) ⇒ Object
37 38 39 40 41 |
# 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) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# 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 |