class Card::Cache

The {Cache} class manages and integrates {Temporary} and {Shared} caching. The {Temporary} cache is typically process- and request- specific and is often “ahead” of the database; the {Shared} cache is typically shared across processes and tends to stay true to the database.

Any ruby Class can declare and/or retrieve its own cache as follows:

““ Card::Cache ““

Typically speaking, mod developers do not need to use the Cache classes directly, because caching is automatically handled by Card#fetch

Attributes

shared[R]
temp[R]

Public Class Methods

new(opts={}) click to toggle source

Cache#new initializes a {Temporary} cache, and – if a :store opt is provided – a {Shared} cache @param opts [Hash] @option opts [Rails::Cache] :store @option opts [Constant] :class

# File lib/card/cache.rb, line 25
def initialize opts={}
  @klass = opts[:class]
  @shared = Shared.new opts if opts[:store]
  @temp = Temporary.new @klass
end

Public Instance Methods

delete(key) click to toggle source

delete specific cache entries by key @param key [String]

# File lib/card/cache.rb, line 73
def delete key
  track :delete, key do
    @shared&.delete key
    @temp.delete key
  end
end
exist?(key) click to toggle source

test for the existence of the key in either cache @return [true/false]

# File lib/card/cache.rb, line 88
def exist? key
  @temp.exist?(key) || @shared&.exist?(key)
end
fetch(key) { |key| ... } click to toggle source

read and (if not there yet) write @param key [String]

# File lib/card/cache.rb, line 63
def fetch key, &block
  @temp.fetch(key) do
    track :fetch, key do
      @shared ? @shared.fetch(key, &block) : yield(key)
    end
  end
end
read(key) click to toggle source

read cache value (and write to temp cache if missing) @param key [String]

# File lib/card/cache.rb, line 33
def read key
  @temp.fetch(key) do
    track :read, key do
      @shared&.read key
    end
  end
end
read_multi(keys) click to toggle source
# File lib/card/cache.rb, line 41
def read_multi keys
  with_multiple_keys keys do
    @temp.fetch_multi keys do |missing_keys|
      track :read_multi, missing_keys do
        @shared ? @shared.read_multi(missing_keys) : {}
      end
    end
  end
end
reset() click to toggle source

reset both caches (for a given Card::Cache instance)

# File lib/card/cache.rb, line 81
def reset
  @shared&.reset
  @temp.reset
end
write(key, value) click to toggle source

write to hard (where applicable) and temp cache @param key [String] @param value

# File lib/card/cache.rb, line 54
def write key, value
  track :write, key do
    @shared&.write key, value
    @temp.write key, value
  end
end

Private Instance Methods

tally(type) click to toggle source
# File lib/card/cache.rb, line 109
def tally type
  h = Card::Cache.counter ||= {}
  t = h[@klass] ||= {}
  t[type] ||= 0
  t[type] += 1
end
track(action, key) { || ... } click to toggle source
# File lib/card/cache.rb, line 98
def track action, key
  return yield unless (log_action = Cardio.config.cache_log_level)

  key = key.size if key.is_a? Array
  unless log_action == :tally
    Rails.logger.send log_action, "#{action.to_s.upcase} (#{@klass}): #{key}"
  end
  tally action
  yield
end
with_multiple_keys(keys) { |: {}| ... } click to toggle source
# File lib/card/cache.rb, line 94
def with_multiple_keys keys
  keys.size > 1 ? yield : {}
end