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

new(opts) click to toggle source

@param opts [Hash] @option opts [Rails::Cache] :store @option opts [ruby Class] :class, typically ActiveRecord descendant @option opts [String] :database

# 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

Public Instance Methods

annihilate() click to toggle source

the nuclear option. can affect other applications sharing the same cache engine. keep in mind mutually assured destruction.

# File lib/card/cache/shared.rb, line 47
def annihilate
  @store.clear
end
delete(key) click to toggle source
# File lib/card/cache/shared.rb, line 122
def delete key
  @store.delete full_key(key)
end
exist?(key) click to toggle source
# File lib/card/cache/shared.rb, line 126
def exist? key
  @store.exist? full_key(key)
end
fetch(key, &block) click to toggle source
# File lib/card/cache/shared.rb, line 118
def fetch key, &block
  @store.fetch full_key(key), &block
end
full_key(key) click to toggle source

returns prefix/key @param key [String] @return [String]

# 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
prefix() click to toggle source

prefix added to cache key to create a system-wide unique key

# File lib/card/cache/shared.rb, line 64
def prefix
  @prefix ||= "#{@database}-#{@class_key}-#{stamp}:"
end
read(key) click to toggle source
# File lib/card/cache/shared.rb, line 77
def read key
  @store.read full_key(key)
end
read_attribute(key, attribute) click to toggle source

def deep_read key

binding.pry
# local_cache = @store.send :local_cache
# local_cache&.clear
read key

end

# 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
read_multi(keys) click to toggle 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.each_with_object({}) { |(k, v), h| h[map[k]] = v }
end
renew() click to toggle source

renew insures you’re using the most current cache version by reaffirming the stamp and prefix

# File lib/card/cache/shared.rb, line 32
def renew
  @stamp = nil
  @prefix = nil
end
reset() click to toggle source

reset effectively clears the cache by setting a new stamp. However unlike annihilate, it won’t bother other apps using the same cache engine.

# File lib/card/cache/shared.rb, line 39
def reset
  @stamp = self.class.new_stamp
  @prefix = nil
  Cardio.cache.write stamp_key, @stamp
end
stamp() click to toggle source

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.

# File lib/card/cache/shared.rb, line 54
def stamp
  @stamp ||= Cardio.cache.fetch(stamp_key) { self.class.new_stamp }
end
stamp_key() click to toggle source

key for looking up the current stamp

# File lib/card/cache/shared.rb, line 59
def stamp_key
  "#{@database}-#{@class_key}-#{self.class.stamp}-stamp"
end
write(key, value) click to toggle source
# File lib/card/cache/shared.rb, line 114
def write key, value
  @store.write full_key(key), value
end
write_attribute(key, attribute, value) click to toggle source

update an attribute of an object already in the cache @param key [String] @param attribute [String, Symbol]

# 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