class Card::Cache

The {Cache} class manages and integrates {Temporary} and {Persistent} caching. The {Temporary} cache is typically process- and request- specific and is often “ahead” of the database; the {Persistent} 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

no_renewal[RW]
hard[R]
soft[R]

Public Class Methods

[](klass) click to toggle source

create a new cache for the ruby class provided @param klass [Class] @return [{Card::Cache}]

# File lib/card/cache.rb, line 33
def [] klass
  raise "nil klass" if klass.nil?

  cache_type = persistent_cache || nil
  cache_by_class[klass] ||= new class: klass, store: cache_type
end
new(opts={}) click to toggle source

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

# File lib/card/cache.rb, line 134
def initialize opts={}
  @klass = opts[:class]
  cache_by_class[@klass] = self
  @hard = Persistent.new opts if opts[:store]
  @soft = Temporary.new
end
obj_to_key(obj) click to toggle source

generate a cache key from an object @param obj [Object] @return [String]

# File lib/card/cache.rb, line 115
def obj_to_key obj
  case obj
  when Hash
    obj.sort.map { |key, value| "#{key}=>(#{obj_to_key(value)})" } * ","
  when Array
    obj.map { |value| obj_to_key(value) }
  else
    obj.to_s
  end
end
persistent_cache() click to toggle source
# File lib/card/cache.rb, line 40
def persistent_cache
  return @persistent_cache unless @persistent_cache.nil?

  @persistent_cache =
    case
    when ENV["NO_RAILS_CACHE"]          then false
    when Cardio.config.persistent_cache then Cardio.cache
    else                                     false
    end
end
renew() click to toggle source

clear the temporary caches and ensure we’re using the latest stamp on the persistent caches.

# File lib/card/cache.rb, line 53
def renew
  return if no_renewal

  renew_persistent
  cache_by_class.each_value do |cache|
    cache.soft.reset
    cache.hard&.renew
  end
end
renew_persistent() click to toggle source
# File lib/card/cache.rb, line 63
def renew_persistent
  Card::Cache::Persistent.renew if persistent_cache
end
reset() click to toggle source

reset standard cached for all classes

# File lib/card/cache.rb, line 68
def reset
  reset_hard
  reset_soft
end
reset_all() click to toggle source

reset all caches for all classes

# File lib/card/cache.rb, line 74
def reset_all
  reset_hard
  reset_soft
  reset_other
end
reset_global() click to toggle source

completely wipe out all caches, often including the Persistent cache of other decks using the same mechanism. Generally prefer {.reset_all} @see .reset_all

# File lib/card/cache.rb, line 84
def reset_global
  cache_by_class.each_value do |cache|
    cache.soft.reset
    cache.hard&.annihilate
  end
  reset_other
end
reset_hard() click to toggle source

reset the Persistent cache for all classes

# File lib/card/cache.rb, line 93
def reset_hard
  Card::Cache::Persistent.reset if persistent_cache
  cache_by_class.each_value do |cache|
    cache.hard&.reset
  end
end
reset_other() click to toggle source

reset Codename cache and delete tmp files (the non-standard caches)

# File lib/card/cache.rb, line 107
def reset_other
  Card::Codename.reset_cache
  Cardio::Utils.delete_tmp_files!
end
reset_soft() click to toggle source

reset the Temporary cache for all classes

# File lib/card/cache.rb, line 101
def reset_soft
  cache_by_class.each_value { |cache| cache.soft.reset }
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 164
def delete key
  @hard&.delete key
  @soft.delete key
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 177
def exist? key
  @soft.exist?(key) || @hard&.exist?(key)
end
fetch(key) { || ... } click to toggle source

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

# File lib/card/cache.rb, line 158
def fetch key, &block
  @soft.fetch(key) { @hard ? @hard.fetch(key, &block) : yield }
end
read(key) click to toggle source

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

# File lib/card/cache.rb, line 143
def read key
  @soft.read(key) ||
    (@hard && (ret = @hard.read(key)) && @soft.write(key, ret))
end
reset() click to toggle source

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

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

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

# File lib/card/cache.rb, line 151
def write key, value
  @hard&.write key, value
  @soft.write key, value
end