Class: ShopifyClient::Cache::Store Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify-client/cache/store.rb

Overview

This class is abstract.

Direct Known Subclasses

RedisStore, ThreadLocalStore

Instance Method Summary collapse

Constructor Details

#initialize(encode: JSON.method(:generate), decode: JSON.method(:parse)) ⇒ Store

Returns a new instance of Store.

Examples:

store = Store.new(
  encode: MessagePack.method(:pack),
  decode: MessagePack.method(:unpack),
)

Parameters:

  • encode (#call) (defaults to: JSON.method(:generate))
  • decode (#call) (defaults to: JSON.method(:parse))


17
18
19
20
# File 'lib/shopify-client/cache/store.rb', line 17

def initialize(encode: JSON.method(:generate), decode: JSON.method(:parse))
  @encode = encode
  @decode = decode
end

Instance Method Details

#call(key, ttl: ShopifyClient.config.cache_ttl, &block) ⇒ Object

Fetch a value from the cache, falling back to the result of the given block when the cache is empty/expired.

Parameters:

  • key (String)
  • ttl (Integer) (defaults to: ShopifyClient.config.cache_ttl)

    in seconds

Returns:

  • (Object)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shopify-client/cache/store.rb', line 29

def call(key, ttl: ShopifyClient.config.cache_ttl, &block)
  value = get(key)

  if value.nil?
    value = block.()

    set(key, value, ttl: ttl)
  end

  value
end

#clear(key) ⇒ Object

Clear cached data.

Parameters:

  • key (String)

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/shopify-client/cache/store.rb', line 62

def clear(key)
  raise NotImplementedError
end

#set(key, value, ttl: ShopifyClient.config.cache_ttl) ⇒ Object

Overwrite cached data and set TTL (if implemented by child class).

Parameters:

  • key (String)
  • value (Object)
  • ttl (Integer) (defaults to: ShopifyClient.config.cache_ttl)

    in seconds

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/shopify-client/cache/store.rb', line 55

def set(key, value, ttl: ShopifyClient.config.cache_ttl)
  raise NotImplementedError
end