Class: Card::Cache::Persistent
- Inherits:
-
Object
- Object
- Card::Cache::Persistent
- Defined in:
- lib/card/cache/persistent.rb
Overview
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 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 cards generally must have set_modules re-included after retrieval from the persistent cache.
Instance Attribute Summary collapse
-
#prefix ⇒ Object
prefix added to cache key to create a system-wide unique key.
Class Method Summary collapse
-
.database_name ⇒ Object
name of current database; used here to insure that different databases are cached separately.
Instance Method Summary collapse
-
#annihilate ⇒ Object
the nuclear option.
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
- #fetch(key, &block) ⇒ Object
-
#full_key(key) ⇒ String
returns prefix/key.
-
#initialize(opts) ⇒ Persistent
constructor
A new instance of Persistent.
-
#new_stamp ⇒ Object
stamp generator.
- #read(key) ⇒ 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.
Constructor Details
#initialize(opts) ⇒ Persistent
Returns a new instance of Persistent.
35 36 37 38 39 40 |
# File 'lib/card/cache/persistent.rb', line 35 def initialize opts @store = opts[:store] @klass = opts[:class] @class_key = @klass.to_s.to_name.key @database = opts[:database] || self.class.database_name end |
Instance Attribute Details
#prefix ⇒ Object
prefix added to cache key to create a system-wide unique key
80 81 82 |
# File 'lib/card/cache/persistent.rb', line 80 def prefix @prefix end |
Class Method Details
.database_name ⇒ Object
find better home for this method
name of current database; used here to insure that different databases are cached separately
24 25 26 27 28 |
# File 'lib/card/cache/persistent.rb', line 24 def database_name @database_name ||= (cfg = Cardio.config) && (dbcfg = cfg.database_configuration) && dbcfg[Rails.env]["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.
58 59 60 |
# File 'lib/card/cache/persistent.rb', line 58 def annihilate @store.clear end |
#delete(key) ⇒ Object
114 115 116 |
# File 'lib/card/cache/persistent.rb', line 114 def delete key @store.delete full_key(key) end |
#exist?(key) ⇒ Boolean
118 119 120 |
# File 'lib/card/cache/persistent.rb', line 118 def exist? key @store.exist? full_key(key) end |
#fetch(key, &block) ⇒ Object
110 111 112 |
# File 'lib/card/cache/persistent.rb', line 110 def fetch key, &block @store.fetch full_key(key), &block end |
#full_key(key) ⇒ String
returns prefix/key
87 88 89 |
# File 'lib/card/cache/persistent.rb', line 87 def full_key key "#{prefix}/#{key}" end |
#new_stamp ⇒ Object
stamp generator
75 76 77 |
# File 'lib/card/cache/persistent.rb', line 75 def new_stamp Time.now.to_i.to_s 32 end |
#read(key) ⇒ Object
91 92 93 |
# File 'lib/card/cache/persistent.rb', line 91 def read key @store.read full_key(key) end |
#renew ⇒ Object
renew insures you're using the most current cache version by reaffirming the stamp and prefix
44 45 46 47 |
# File 'lib/card/cache/persistent.rb', line 44 def renew @stamp = nil @prefix = nil end |
#reset ⇒ Object
reset effectively clears the cache by setting a new stamp
50 51 52 53 54 |
# File 'lib/card/cache/persistent.rb', line 50 def reset @stamp = 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.
65 66 67 |
# File 'lib/card/cache/persistent.rb', line 65 def stamp @stamp ||= Cardio.cache.fetch stamp_key { new_stamp } end |
#stamp_key ⇒ Object
key for looking up the current stamp
70 71 72 |
# File 'lib/card/cache/persistent.rb', line 70 def stamp_key "#{@database}/#{@class_key}/stamp" end |
#write(key, value) ⇒ Object
106 107 108 |
# File 'lib/card/cache/persistent.rb', line 106 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
98 99 100 101 102 103 104 |
# File 'lib/card/cache/persistent.rb', line 98 def write_attribute key, attribute, value if @store && (object = read key) object.instance_variable_set "@#{attribute}", value write key, object end value end |