class Card::Cache::Persistent

Persistent (or Hard) caches closely mirror the database and are intended to be altered only upon database alterations.

Unlike the database, the persistent 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 persistent cache implementations cannot store objects with singleton classes, therefore {Card cards} generally must have set_modules re-included after retrieval from the persistent cache.

Attributes

prefix[RW]

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/persistent.rb, line 25
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/persistent.rb, line 50
def annihilate
  @store.clear
end
deep_read(key) click to toggle source
# File lib/card/cache/persistent.rb, line 97
def deep_read key
  local_cache = @store.send :local_cache
  local_cache&.clear
  read key
end
delete(key) click to toggle source
# File lib/card/cache/persistent.rb, line 116
def delete key
  @store.delete full_key(key)
end
exist?(key) click to toggle source
# File lib/card/cache/persistent.rb, line 120
def exist? key
  @store.exist? full_key(key)
end
fetch(key, &block) click to toggle source
# File lib/card/cache/persistent.rb, line 112
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/persistent.rb, line 74
def full_key key
  fk = "#{prefix}/#{key}"
  fk.tr! "*", "X" if @file_cache # save for windows fs
  fk
end
read(key) click to toggle source
# File lib/card/cache/persistent.rb, line 80
def read key
  @store.read full_key(key)
end
read_attribute(key, attribute) click to toggle source
# File lib/card/cache/persistent.rb, line 103
def read_attribute key, attribute
  object = deep_read key
  object.instance_variable_get "@#{attribute}"
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/persistent.rb, line 35
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/persistent.rb, line 42
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/persistent.rb, line 57
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/persistent.rb, line 62
def stamp_key
  "#{@database}-#{@class_key}-#{self.class.stamp}-stamp"
end
write(key, value) click to toggle source
# File lib/card/cache/persistent.rb, line 108
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/persistent.rb, line 87
def write_attribute key, attribute, value
  return value unless @store

  if (object = deep_read key)
    object.instance_variable_set "@#{attribute}", value
    write key, object
  end
  value
end