Module: CloudConfig::Cache::ClassMethods

Defined in:
lib/cloud-config/cache.rb

Overview

Class methods for CloudConfig::Cache

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



15
16
17
# File 'lib/cloud-config/cache.rb', line 15

def cache
  @cache
end

#Cache client instance(clientinstance) ⇒ Object (readonly)

Returns:

  • (Object)


15
# File 'lib/cloud-config/cache.rb', line 15

attr_reader :cache

Instance Method Details

#cache_client(client) ⇒ Object

Configure the cache client

Parameters:

  • client (Object)

    Cache client instance



20
21
22
# File 'lib/cloud-config/cache.rb', line 20

def cache_client(client)
  @cache = client
end

#with_cache(key, options = {}) { ... } ⇒ Object

Fetch the value of the key from the cache, if the key exists in the cache

Parameters:

  • key (String, Symbol)

    Key to fetch from the cache

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • Cache (Hash<Symbol,Object>)

    options

Yields:

  • Fetch the value of the key if the key does not exist in the cache

Returns:

  • (Object)

    Value of the key



32
33
34
35
36
37
38
39
40
# File 'lib/cloud-config/cache.rb', line 32

def with_cache(key, options = {})
  return cache.get(key) if !options[:reset_cache] && cache&.key?(key)

  value = yield

  cache&.set(key, value, options)

  value
end