class ArtirixDataModels::CachedActionAdaptor

Constants

STATUSES
STATUS_NOT_FOUND
STATUS_OK

Attributes

cache[R]
logger[R]

Public Class Methods

new(logger: nil, cache: nil, **ignored_options) click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 8
def initialize(logger: nil, cache: nil, **ignored_options)
  @logger  = logger || ArtirixDataModels.logger
  @cache   = cache || ArtirixDataModels.cache
  @enabled = true
end

Public Instance Methods

cached?() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 14
def cached?
  return false unless enabled?

  cache_exist?
end
call(&block)
Alias for: fetch
delete() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 44
def delete
  return true unless enabled?
  cache_delete
end
disable() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 36
def disable
  @enabled = false
end
enable() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 32
def enable
  @enabled = true
end
enabled?() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 40
def enabled?
  @enabled
end
fetch(&block) click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 20
def fetch(&block)
  if cached?
    get_cached_result
  elsif block_given?
    perform &block
  else
    nil
  end
end
Also aliased as: call

Private Instance Methods

cache_delete() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 122
def cache_delete
  logger.debug "DELETE CACHE with key #{cache_key.inspect}"
  return true unless cache

  cache.delete cache_key, cache_options
end
cache_exist?() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 100
def cache_exist?
  logger.debug "EXIST CACHE with key #{cache_key.inspect}"
  return false unless cache

  cache.exist? cache_key, cache_options
end
cache_key() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 92
def cache_key
  @cache_key ||= load_cache_key
end
cache_options() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 96
def cache_options
  @cache_options ||= load_cache_options
end
cache_read() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 107
def cache_read
  logger.debug "READ CACHE with key #{cache_key.inspect}"
  return nil unless cache

  cache.read cache_key, cache_options
end
cache_result(result) click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 80
def cache_result(result)
  cache_write result
end
cache_write(value) click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 114
def cache_write(value)
  logger.debug "WRITE CACHE with key #{cache_key.inspect}"
  return value unless cache

  cache.write cache_key, value, cache_options
  value
end
get_cached_result() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 51
def get_cached_result
  return nil unless enabled?

  c = cache_read
  return nil unless c.present?
  return c unless c.respond_to?(:size) && c.respond_to?(:first) && c.size == 2 && STATUSES.include?(c.first)

  status = c.first
  result = c.last

  case status
  when STATUS_NOT_FOUND
    raise ArtirixDataModels::DataGateway::NotFound, result
  else
    result
  end
end
load_cache_key() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 84
def load_cache_key
  raise NotImplementedError
end
load_cache_options() click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 88
def load_cache_options
  raise NotImplementedError
end
perform() { || ... } click to toggle source
# File lib/artirix_data_models/cached_action_adaptor.rb, line 69
def perform
  return yield unless enabled?

  result = yield
  cache_result [STATUS_OK, result]
  result
rescue ArtirixDataModels::DataGateway::NotFound => e
  cache_result [STATUS_NOT_FOUND, e.data_hash]
  raise e
end