module Cachengue::Caching

Constants

ALL_KEYS

Reduce memory use and its faster.

NONE_KEYS

Public Instance Methods

clear(namespace = nil, strategy = :all) click to toggle source
# File lib/cachengue/caching.rb, line 61
def clear(namespace = nil, strategy = :all)
  keys_to_clear = keys(namespace, strategy)

  clear_keys(keys_to_clear)
end
clear_by(options) click to toggle source
# File lib/cachengue/caching.rb, line 67
def clear_by(options)
  key = format_key(
    options.fetch(:namespace),
    options[:key] || options.fetch(:method),
    options.fetch(:args),
    options[:args_hash]
  )

  clear_keys([key])
end
clear_keys(keys_to_clear) click to toggle source
# File lib/cachengue/caching.rb, line 78
def clear_keys(keys_to_clear)
  return if Rails.env.test?

  keys_to_clear.sum do |k|
    Rails.cache.delete(k)
  end
end
fetch(namespace, method_name, args, options = {}, &block) click to toggle source
# File lib/cachengue/caching.rb, line 96
def fetch(namespace, method_name, args, options = {}, &block)
  if options[:multi]
    fetch_multi(namespace, method_name, args, options, &block)
  else
    fetch_one(namespace, method_name, args, options, &block)
  end
end
fetch_multi(namespace, method_name, args_list, options = {}) click to toggle source
# File lib/cachengue/caching.rb, line 113
def fetch_multi(namespace, method_name, args_list, options = {})
  return {} if args_list.empty?

  args_keys = {}

  args_list.each do |args|
    key = format_key_by_options(namespace, method_name, args, options)

    args_keys[key] = args
  end

  options = format_options(options)
  results = read_multi(args_keys.keys)

  computed_keys = args_keys.keys - results.keys
  computed_values = computed_keys.map { |key| args_keys.fetch(key) }
  computed = write_multi(computed_keys, computed_values, options, &Proc.new)

  results.merge!(computed)
  results.transform_keys! { |key| args_keys.fetch(key) }
  results
end
fetch_one(namespace, method_name, args, options = {}, &block) click to toggle source
# File lib/cachengue/caching.rb, line 104
def fetch_one(namespace, method_name, args, options = {}, &block)
  key = format_key_by_options(namespace, method_name, args, options)
  options = format_options(options)

  Rails.cache.fetch(key, options) do
    get_value_by(args, &block)
  end
end
format_key(namespace, method_name, args, args_hash) click to toggle source
# File lib/cachengue/caching.rb, line 20
def format_key(namespace, method_name, args, args_hash)
  args = args.is_a?(Array) ? args.map(&:as_json) : [args.as_json]
  args = Digest::SHA256.base64digest(String(args)) if args_hash
  namespace = key_prefix(namespace)

  "#{namespace}:#{method_name}:#{args}"
end
format_key_by_options(namespace, method_name, args, options) click to toggle source
# File lib/cachengue/caching.rb, line 11
def format_key_by_options(namespace, method_name, args, options)
  format_key(
    options[:namespace] || namespace,
    options[:key] || options[:method] || method_name,
    options[:args] || args,
    options[:args_hash]
  )
end
format_options(options) click to toggle source
# File lib/cachengue/caching.rb, line 28
def format_options(options)
  options.without(
    :args,
    :key,
    :args_hash,
    :method,
    :multi,
    :namespace
  )
end
get_value_by(args) { |*args| ... } click to toggle source
# File lib/cachengue/caching.rb, line 86
def get_value_by(args)
  value = yield(*args)

  if defined?(::ActiveRecord) && value.is_a?(::ActiveRecord::Relation)
    value.to_a
  else
    value
  end
end
key_prefix(namespace = nil) click to toggle source
# File lib/cachengue/caching.rb, line 39
def key_prefix(namespace = nil)
  if namespace.present?
    "cachengue:#{namespace.to_s.underscore}"
  else
    'cachengue'
  end
end
key_suffix(strategy) click to toggle source
# File lib/cachengue/caching.rb, line 47
def key_suffix(strategy)
  strategy == :all ? ALL_KEYS : NONE_KEYS
end
keys(namespace = nil, strategy = :all) click to toggle source
# File lib/cachengue/caching.rb, line 51
def keys(namespace = nil, strategy = :all)
  return [] if Rails.env.test?

  prefix = key_prefix(namespace)

  key_suffix(strategy).flat_map do |suffix|
    Rails.cache.data.keys("#{prefix}:#{suffix}")
  end
end
read_multi(keys) click to toggle source
# File lib/cachengue/caching.rb, line 136
def read_multi(keys)
  Rails.cache.read_multi(*keys)
end
write_multi(keys, values, options) click to toggle source
# File lib/cachengue/caching.rb, line 140
def write_multi(keys, values, options)
  results = {}

  keys.each_with_index do |key, index|
    results[key] = get_value_by(values.fetch(index), &Proc.new)
  end

  Rails.cache.write_multi(results, options)

  results
end