class Card::Cache::Shared
Shared caches closely mirror the database and are intended to be altered only upon database alterations.
Unlike the database, the shared cache stores records of records that have been requested but are missing or, in the case of some {Card cards}, “virtual”, meaning that they follow known patterns but do not exist in the database.
Most shared cache implementations cannot store objects with singleton classes, therefore {Card cards} generally must have set_modules re-included after retrieval from the shared cache.
Public Class Methods
Source
# File lib/card/cache/shared.rb, line 22 def initialize opts @store = opts[:store] @file_cache = @store.is_a? ActiveSupport::Cache::FileStore @klass = opts[:class] @class_key = @klass.to_s.to_name.key @database = opts[:database] || Cardio.database end
@param opts [Hash] @option opts [Rails::Cache] :store @option opts [ruby Class] :class, typically ActiveRecord descendant @option opts [String] :database
Public Instance Methods
Source
# File lib/card/cache/shared.rb, line 47 def annihilate @store.clear end
the nuclear option. can affect other applications sharing the same cache engine. keep in mind mutually assured destruction.
Source
# File lib/card/cache/shared.rb, line 122 def delete key @store.delete full_key(key) end
Source
# File lib/card/cache/shared.rb, line 126 def exist? key @store.exist? full_key(key) end
Source
# File lib/card/cache/shared.rb, line 118 def fetch key, &block @store.fetch full_key(key), &block end
Source
# File lib/card/cache/shared.rb, line 71 def full_key key fk = "#{prefix}/#{key}" fk.tr! "*", "X" if @file_cache # save for windows fs fk end
returns prefix/key @param key [String] @return [String]
Source
# File lib/card/cache/shared.rb, line 64 def prefix @prefix ||= "#{@database}-#{@class_key}-#{stamp}:" end
prefix added to cache key to create a system-wide unique key
Source
# File lib/card/cache/shared.rb, line 77 def read key @store.read full_key(key) end
Source
# File lib/card/cache/shared.rb, line 108 def read_attribute key, attribute # object = deep_read key object = read key object.instance_variable_get "@#{attribute}" end
def deep_read key
binding.pry # local_cache = @store.send :local_cache # local_cache&.clear read key
end
Source
# File lib/card/cache/shared.rb, line 81 def read_multi keys map = keys.each_with_object({}) { |k, h| h[full_key k] = k } raw = @store.read_multi(*map.keys) raw.transform_keys { |k| map[k] } end
Source
# File lib/card/cache/shared.rb, line 32 def renew @stamp = nil @prefix = nil end
renew insures you’re using the most current cache version by reaffirming the stamp and prefix
Source
# File lib/card/cache/shared.rb, line 39 def reset @stamp = self.class.new_stamp @prefix = nil Cardio.cache.write stamp_key, @stamp end
reset effectively clears the cache by setting a new stamp. However unlike annihilate, it won’t bother other apps using the same cache engine.
Source
# File lib/card/cache/shared.rb, line 54 def stamp @stamp ||= Cardio.cache.fetch(stamp_key) { self.class.new_stamp } end
the current time stamp. changing this value effectively resets the cache. Note that Cardio.cache
is a simple Rails::Cache, not a Card::Cache
object.
Source
# File lib/card/cache/shared.rb, line 59 def stamp_key "#{@database}-#{@class_key}-#{self.class.stamp}-stamp" end
key for looking up the current stamp
Source
# File lib/card/cache/shared.rb, line 114 def write key, value @store.write full_key(key), value end
Source
# File lib/card/cache/shared.rb, line 90 def write_attribute key, attribute, value return value unless @store # if (object = deep_read key) if (object = read key) object.instance_variable_set "@#{attribute}", value write key, object end value end
update an attribute of an object already in the cache @param key [String] @param attribute [String, Symbol]