class Estated::RequestCache

Public Class Methods

read(path, params) click to toggle source
# File lib/estated/request_cache.rb, line 6
def read(path, params)
  document = cache_store.
    find(cache_key: cache_key(path, params)).
    limit(1).
    first

  document['payload'] if document
end
setup() click to toggle source
# File lib/estated/request_cache.rb, line 30
def setup
  cache_store.indexes.create_one({ cache_key: 1 }, unique: true)
  cache_store.indexes.create_one(
    { created_at: 1 },
    expire_after_seconds: Estated.config.cache_ttl)
end
write(path, params, payload) click to toggle source
# File lib/estated/request_cache.rb, line 15
def write(path, params, payload)
  cache_store.update_one(
    {
      cache_key: cache_key(path, params)
    },
    {
      cache_key: cache_key(path, params),
      payload: payload,
      created_at: Time.now
    },
    upsert: true
  )
  true
end

Private Class Methods

cache_key(path, params) click to toggle source
# File lib/estated/request_cache.rb, line 43
def cache_key(path, params)
  query = params.
    sort_by { |k, _| k }.
    map {|k, v| "#{k}=#{v}"}.
    join('|')

  "path=#{path}|" + query
end
cache_store() click to toggle source
# File lib/estated/request_cache.rb, line 39
def cache_store
  Estated.config.mongo_client[Estated.config.mongo_collection]
end