class IknowCache::Cache

Constants

DEBUG

Attributes

cache_group[R]
cache_options[R]
name[R]

Public Class Methods

new(cache_group, name, cache_options) click to toggle source
# File lib/iknow_cache.rb, line 158
def initialize(cache_group, name, cache_options)
  @cache_group   = cache_group
  @name          = name
  @cache_options = IknowCache.merge_options(cache_group.default_options, cache_options).try { |x| x.dup.freeze }
end

Public Instance Methods

delete(key, parent_path: nil, **options) click to toggle source
# File lib/iknow_cache.rb, line 189
def delete(key, parent_path: nil, **options)
  p = path(key, parent_path)
  Rails.logger.debug("Cache Delete: #{p}") if DEBUG
  Rails.cache.delete(p, IknowCache.merge_options(cache_options, options))
end
fetch(key, parent_path: nil, **options, &block) click to toggle source
# File lib/iknow_cache.rb, line 164
def fetch(key, parent_path: nil, **options, &block)
  p = path(key, parent_path)
  Rails.logger.debug("Cache Fetch: #{p}") if DEBUG
  v = Rails.cache.fetch(p, IknowCache.merge_options(cache_options, options), &block)
  Rails.logger.debug("=> #{v.inspect}") if DEBUG
  v
end
read(key, parent_path: nil, **options) click to toggle source
# File lib/iknow_cache.rb, line 172
def read(key, parent_path: nil, **options)
  p = path(key, parent_path)
  Rails.logger.debug("Cache Read: #{p}") if DEBUG
  v = Rails.cache.read(p, IknowCache.merge_options(cache_options, options))
  Rails.logger.debug("=> #{v.inspect}") if DEBUG
  v
end
read_multi(keys) click to toggle source
# File lib/iknow_cache.rb, line 195
def read_multi(keys)
  return {} if keys.blank?

  key_paths = path_multi(keys)
  path_keys = key_paths.invert

  Rails.logger.debug("Cache Multi-Read: #{key_paths.values.inspect}") if DEBUG
  raw = Rails.cache.read_multi(*key_paths.values)
  vs = raw.each_with_object({}) do |(path, value), h|
    h[path_keys[path]] = value
  end
  Rails.logger.debug("=> #{vs.inspect}") if DEBUG
  vs
end
write(key, value, parent_path: nil, **options) click to toggle source
# File lib/iknow_cache.rb, line 180
def write(key, value, parent_path: nil, **options)
  p = path(key, parent_path)
  if DEBUG
    Rails.logger.debug("Cache Store: #{p} (#{IknowCache.merge_options(cache_options, options).inspect})")
    Rails.logger.debug("<= #{value.inspect}")
  end
  Rails.cache.write(p, value, IknowCache.merge_options(cache_options, options))
end
write_multi(entries, options = nil) click to toggle source
# File lib/iknow_cache.rb, line 210
def write_multi(entries, options = nil)
  return {} if entries.blank?

  key_paths = path_multi(entries.keys)
  options = IknowCache.merge_options(cache_options, options)

  entries.each do |key, value|
    Rails.logger.debug("Cache Multi-Write: #{key_paths[key]}") if DEBUG
    Rails.cache.write(key_paths[key], value, options)
  end
end

Private Instance Methods

path(key, parent_path = nil) click to toggle source
# File lib/iknow_cache.rb, line 226
def path(key, parent_path = nil)
  group_path = @cache_group.path(key, parent_path)
  path_string(group_path)
end
path_multi(keys) click to toggle source
# File lib/iknow_cache.rb, line 231
def path_multi(keys)
  @cache_group.path_multi(keys).each_with_object({}) do |(key, group_path), h|
    h[key] = path_string(group_path)
  end
end
path_string(group_path) click to toggle source
# File lib/iknow_cache.rb, line 237
def path_string(group_path)
  "#{group_path}/#{self.name}"
end