module ResponseBank

Constants

VERSION

Attributes

cache_store[RW]
logger[W]

Public Class Methods

acquire_lock(_cache_key) click to toggle source
# File lib/response_bank.rb, line 16
def acquire_lock(_cache_key)
  raise NotImplementedError, "Override ResponseBank.acquire_lock in an initializer."
end
cache_key_for(data) click to toggle source
# File lib/response_bank.rb, line 45
def cache_key_for(data)
  case data
  when Hash
    return data.inspect unless data.key?(:key)

    key = hash_value_str(data[:key])

    return key unless data.key?(:version)

    version = hash_value_str(data[:version])

    [key, version].join(":")
  when Array
    data.inspect
  when Time, DateTime
    data.to_i
  when Date
    data.to_time.to_i
  when true, false, Integer, Symbol, String
    data.inspect
  else
    data.to_s.inspect
  end
end
compress(content) click to toggle source
# File lib/response_bank.rb, line 32
def compress(content)
  io = StringIO.new
  gz = Zlib::GzipWriter.new(io)
  gz.write(content)
  io.string
ensure
  gz.close
end
decompress(content) click to toggle source
# File lib/response_bank.rb, line 41
def decompress(content)
  Zlib::GzipReader.new(StringIO.new(content)).read
end
log(message) click to toggle source
# File lib/response_bank.rb, line 12
def log(message)
  @logger.info("[ResponseBank] #{message}")
end
read_from_backing_cache_store(_env, cache_key, backing_cache_store: cache_store) click to toggle source
# File lib/response_bank.rb, line 28
def read_from_backing_cache_store(_env, cache_key, backing_cache_store: cache_store)
  backing_cache_store.read(cache_key, raw: true)
end
write_to_backing_cache_store(_env, key, payload, expires_in: nil) click to toggle source
# File lib/response_bank.rb, line 24
def write_to_backing_cache_store(_env, key, payload, expires_in: nil)
  cache_store.write(key, payload, raw: true, expires_in: expires_in)
end
write_to_cache(_key) { || ... } click to toggle source
# File lib/response_bank.rb, line 20
def write_to_cache(_key)
  yield
end

Private Class Methods

hash_value_str(data) click to toggle source
# File lib/response_bank.rb, line 72
def hash_value_str(data)
  if data.is_a?(Hash)
    data.values.join(",")
  else
    data.to_s
  end
end