class Lucid::Shopify::Cache

Constants

TTL
VERSION

Public Instance Methods

+(new_namespace)
Alias for: add_namespace
add_namespace(new_namespace) click to toggle source

Create a new instance with a new namespace appended to the current one.

@param new_namespace [String]

@return [Cache]

@example

cache.add_namespace(myshopify_domain)

@example Using the {+} operator alias

cache + myshopify_domain
# File lib/lucid/shopify/cache.rb, line 30
def add_namespace(new_namespace)
  self.class.new("#{namespace}:#{new_namespace}", redis_client)
end
Also aliased as: +
call(key, ttl: TTL) { || ... } click to toggle source

Fetch value from the cache, falling back to the given block when the cache is empty.

@param key [String] @param ttl [Integer]

@yieldreturn [#to_cbor]

@return [Object]

# File lib/lucid/shopify/cache.rb, line 45
def call(key, ttl: TTL)
  key = namespaced_key(key)

  fetch(key) || cache(key, yield, ttl)
end
clear(key) click to toggle source

@param key [String]

# File lib/lucid/shopify/cache.rb, line 80
def clear(key)
  redis_client.del(namespaced_key(key))
end

Private Instance Methods

cache(key, val, ttl) click to toggle source

@param key [String] @param val [#to_cbor] @param ttl [Integer]

@return [Object]

# File lib/lucid/shopify/cache.rb, line 65
        def cache(key, val, ttl)
  redis_client.set(key, CBOR.encode(val))
  redis_client.expire(key, ttl)

  val
end
fetch(key) click to toggle source

@param key [String]

@return [Object, nil]

# File lib/lucid/shopify/cache.rb, line 54
        def fetch(key)
  val = redis_client.get(key)

  val && CBOR.decode(val)
end
namespaced_key(key) click to toggle source

@param key [String]

@return [String]

# File lib/lucid/shopify/cache.rb, line 75
        def namespaced_key(key)
  "#{namespace}:#{key}"
end