Class: Card::Cache::Shared
- Inherits:
-
Object
- Object
- Card::Cache::Shared
- Extended by:
- SharedClass
- Defined in:
- lib/card/cache/shared.rb
Overview
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 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 cards generally must have set_modules re-included after retrieval from the shared cache.
Instance Method Summary collapse
-
#annihilate ⇒ Object
the nuclear option.
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
- #fetch(key) ⇒ Object
-
#full_key(key) ⇒ String
returns prefix/key.
-
#initialize(opts) ⇒ Shared
constructor
A new instance of Shared.
-
#prefix ⇒ Object
prefix added to cache key to create a system-wide unique key.
- #read(key) ⇒ Object
-
#read_attribute(key, attribute) ⇒ Object
def deep_read key binding.pry # local_cache = @store.send :local_cache # local_cache&.clear read key end.
- #read_multi(keys) ⇒ Object
-
#renew ⇒ Object
renew insures you’re using the most current cache version by reaffirming the stamp and prefix.
-
#reset ⇒ Object
reset effectively clears the cache by setting a new stamp.
-
#stamp ⇒ Object
the current time stamp.
-
#stamp_key ⇒ Object
key for looking up the current stamp.
- #write(key, value) ⇒ Object
-
#write_attribute(key, attribute, value) ⇒ Object
update an attribute of an object already in the cache.
Methods included from SharedClass
Constructor Details
#initialize(opts) ⇒ Shared
Returns a new instance of Shared.
24 25 26 27 28 29 30 |
# File 'lib/card/cache/shared.rb', line 24 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 |
Instance Method Details
#annihilate ⇒ Object
the nuclear option. can affect other applications sharing the same cache engine. keep in mind mutually assured destruction.
49 50 51 |
# File 'lib/card/cache/shared.rb', line 49 def annihilate @store.clear end |
#delete(key) ⇒ Object
124 125 126 |
# File 'lib/card/cache/shared.rb', line 124 def delete key @store.delete full_key(key) end |
#exist?(key) ⇒ Boolean
128 129 130 |
# File 'lib/card/cache/shared.rb', line 128 def exist? key @store.exist? full_key(key) end |
#fetch(key) ⇒ Object
120 121 122 |
# File 'lib/card/cache/shared.rb', line 120 def fetch(key, &) @store.fetch(full_key(key), &) end |
#full_key(key) ⇒ String
returns prefix/key
73 74 75 76 77 |
# File 'lib/card/cache/shared.rb', line 73 def full_key key fk = "#{prefix}/#{key}" fk.tr! "*", "X" if @file_cache # save for windows fs fk end |
#prefix ⇒ Object
prefix added to cache key to create a system-wide unique key
66 67 68 |
# File 'lib/card/cache/shared.rb', line 66 def prefix @prefix ||= "#{@database}-#{@class_key}-#{stamp}:" end |
#read(key) ⇒ Object
79 80 81 |
# File 'lib/card/cache/shared.rb', line 79 def read key @store.read full_key(key) end |
#read_attribute(key, attribute) ⇒ Object
def deep_read key
binding.pry
# local_cache = @store.send :local_cache
# local_cache&.clear
read key
end
110 111 112 113 114 |
# File 'lib/card/cache/shared.rb', line 110 def read_attribute key, attribute # object = deep_read key object = read key object.instance_variable_get "@#{attribute}" end |
#read_multi(keys) ⇒ Object
83 84 85 86 87 |
# File 'lib/card/cache/shared.rb', line 83 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 |
#renew ⇒ Object
renew insures you’re using the most current cache version by reaffirming the stamp and prefix
34 35 36 37 |
# File 'lib/card/cache/shared.rb', line 34 def renew @stamp = nil @prefix = nil end |
#reset ⇒ Object
reset effectively clears the cache by setting a new stamp. However unlike annihilate, it won’t bother other apps using the same cache engine.
41 42 43 44 45 |
# File 'lib/card/cache/shared.rb', line 41 def reset @stamp = self.class.new_stamp @prefix = nil Cardio.cache.write stamp_key, @stamp end |
#stamp ⇒ Object
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.
56 57 58 |
# File 'lib/card/cache/shared.rb', line 56 def stamp @stamp ||= Cardio.cache.fetch(stamp_key) { self.class.new_stamp } end |
#stamp_key ⇒ Object
key for looking up the current stamp
61 62 63 |
# File 'lib/card/cache/shared.rb', line 61 def stamp_key "#{@database}-#{@class_key}-#{self.class.stamp}-stamp" end |
#write(key, value) ⇒ Object
116 117 118 |
# File 'lib/card/cache/shared.rb', line 116 def write key, value @store.write full_key(key), value end |
#write_attribute(key, attribute, value) ⇒ Object
update an attribute of an object already in the cache
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/card/cache/shared.rb', line 92 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 |