Module: JustimmoClient::Caching Private

Extended by:
Logging
Included in:
V1::JustimmoRequest
Defined in:
lib/justimmo_client/core/caching.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Caching support

Defined Under Namespace

Classes: NullCache

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

default_logger, logger, rails_logger

Class Attribute Details

.cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the current cache



20
21
22
# File 'lib/justimmo_client/core/caching.rb', line 20

def cache
  @cache ||= default_cache
end

Class Method Details

.default_cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
31
32
# File 'lib/justimmo_client/core/caching.rb', line 28

def default_cache
  cache = JustimmoClient::Config.cache || NullCache.new
  log.info("Using default cache #{cache.class}")
  cache
end

Instance Method Details

#cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
# File 'lib/justimmo_client/core/caching.rb', line 35

def cache
  JustimmoClient::Caching.cache
end

#with_cache(key, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: JSON serialize/deserialize the cached value



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/justimmo_client/core/caching.rb', line 40

def with_cache(key, **options)
  log.debug("Looking up cache key #{key}")
  data = cache.read(key)

  if data.nil?
    log.debug("Cache miss for #{key}")
    data = yield
    cache.write(key, data, options)
  else
    log.debug("Cache hit for #{key}")
  end

  data
end