Class: LucidShopify::Cache

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/lucid_shopify/cache.rb,
lib/lucid_shopify/cache/version.rb

Constant Summary collapse

TTL =
ENV['LUCID_SHOPIFY_CACHE_TTL'] || 3600
VERSION =
'0.9.0'

Instance Method Summary collapse

Instance Method Details

#add_namespace(new_namespace) ⇒ Cache Also known as: +

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

Examples:

cache.add_namespace(myshopify_domain)

Using the #+ operator alias

cache + myshopify_domain

Parameters:

  • new_namespace (String)

Returns:



30
31
32
# File 'lib/lucid_shopify/cache.rb', line 30

def add_namespace(new_namespace)
  self.class.new("#{namespace}:#{new_namespace}", redis_client)
end

#call(key, ttl: TTL) ⇒ Object

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

Parameters:

  • key (String)
  • ttl (Integer) (defaults to: TTL)

Yield Returns:

  • (#to_cbor)

Returns:

  • (Object)


47
48
49
50
51
# File 'lib/lucid_shopify/cache.rb', line 47

def call(key, ttl: TTL)
  key = namespaced_key(key)

  fetch(key) || cache(key, yield, ttl)
end

#clear(key) ⇒ Object

Parameters:

  • key (String)


90
91
92
# File 'lib/lucid_shopify/cache.rb', line 90

def clear(key)
  redis_client.del(namespaced_key(key))
end

#namespaceString

Returns:

  • (String)


13
# File 'lib/lucid_shopify/cache.rb', line 13

param :namespace, default: proc { 'lucid_shopify-cache' }

#redis_clientRedis

Returns:

  • (Redis)


15
# File 'lib/lucid_shopify/cache.rb', line 15

option :redis_client, default: proc { Redis.current }