module ArtirixDataModels::CacheService

Public Class Methods

build_service() click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 20
def self.build_service
  ArtirixCacheService::Service.new.tap do |service|
    set_key_prefix_from_config(service)
    set_options_from_config(service)
  end
end
digest_element(element) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 48
def self.digest_element(element)
  service.digest element
end
expire_cache(pattern = nil, add_wildcard: true, add_prefix: true) click to toggle source

we use ‘delete_matched` method -> it will work fine with Redis but it seems that it won’t with Memcached

# File lib/artirix_data_models/cache_service.rb, line 74
def self.expire_cache(pattern = nil, add_wildcard: true, add_prefix: true)
  return false unless ArtirixDataModels.cache.present?

  p = final_pattern(pattern, add_wildcard: add_wildcard, add_prefix: add_prefix)

  ArtirixDataModels.cache.delete_matched p
end
final_pattern(pattern, add_wildcard: true, add_prefix: true) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 82
def self.final_pattern(pattern, add_wildcard: true, add_prefix: true)
  p = pattern
  p = p.present? ? "#{p}*" : '' if add_wildcard
  p = "*#{service.key_prefix}*#{p}" if add_prefix
  p
end
first_options(*options, return_if_missing: :default, **opts) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 52
def self.first_options(*options, return_if_missing: :default, **opts)
  if opts.key? :return_if_none
    ActiveSupport::Deprecation.warn('use `return_if_missing` instead of `return_if_none`')
    return_if_missing = opts[:return_if_none]
  end

  service.options *options, return_if_missing: return_if_missing
end
key(*given_args) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 61
def self.key(*given_args)
  service.key *given_args
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/artirix_data_models/cache_service.rb, line 90
def self.method_missing(m, *args, &block)
  method = m.to_s

  if method.end_with? '_key'
    ActiveSupport::Deprecation.warn('using method_missing with `service.some_key("1", "2")` is deprecated, use this instead: `service.key(:some, "1", "2")`')
    key = method.gsub(/_key$/, '')
    self.key key, *args

  elsif method.end_with? '_options'
    ActiveSupport::Deprecation.warn('using method_missing with `service.some_options` is deprecated, use this instead: `service.options(:some)`')
    options_name = method.gsub(/_options$/, '')
    self.options options_name

  else
    super
  end
end
options(options_name) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 65
def self.options(options_name)
  service.registered_options options_name
end
options?(options_name) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 69
def self.options?(options_name)
  service.registered_options? options_name
end
reload_service() click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 15
def self.reload_service
  @service = nil
  service
end
respond_to_missing?(m, include_all = false) click to toggle source
Calls superclass method
# File lib/artirix_data_models/cache_service.rb, line 108
def self.respond_to_missing?(m, include_all = false)
  method = m.to_s

  if method.end_with? '_key'
    ActiveSupport::Deprecation.warn('using method_missing with `service.some_key("1", "2")` is deprecated, use this instead: `service.key(:some, "1", "2")`')
    true

  elsif method.end_with? '_options'
    ActiveSupport::Deprecation.warn('using method_missing with `service.some_options` is deprecated, use this instead: `service.options(:some)`')
    options_name = method.gsub(/_options$/, '')
    self.options options_name

  else
    super
  end
end
service() click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 3
def self.service
  @service ||= build_service
end
set_key_prefix_from_config(service) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 27
def self.set_key_prefix_from_config(service)
  prefix = ArtirixDataModels.configuration.try(:cache_app_prefix)
  if prefix
    service.register_key_prefix "#{prefix}__"
  end
end
set_options_from_config(service) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 34
def self.set_options_from_config(service)
  options = ArtirixDataModels.configuration.try(:cache_options)
  if options
    options = options.to_hash if options.respond_to?(:to_hash) && !options.respond_to?(:each)
    options.each do |name, opts|
      if name.to_s == 'default_options'
        service.register_default_options opts
      else
        service.register_options name, opts
      end
    end
  end
end
use_cache_service(artirix_cache_service) click to toggle source
# File lib/artirix_data_models/cache_service.rb, line 7
def self.use_cache_service(artirix_cache_service)
  unless artirix_cache_service.kind_of? ArtirixCacheService::Service
    raise InvalidServiceError, "expected ArtirixCacheService::Service, given #{artirix_cache_service.inspect}"
  end

  @service = artirix_cache_service
end