Class: Card::Cache
- Inherits:
-
Object
- Object
- Card::Cache
- Extended by:
- Prepopulate
- Defined in:
- lib/card/cache.rb,
lib/card/cache/temporary.rb,
lib/card/cache/persistent.rb,
lib/card/cache/view_cache.rb,
lib/card/cache/prepopulate.rb
Overview
The Cache class manages and integrates Temporary and caching. The {Temporary cache is typically process- and request- specific and is often "ahead" of the database; the Persistent cache is typically shared across processes and tends to stay true to the database.
Any ruby Class can declare and/or retrieve its own cache as follows:
Card::Cache[MyClass]
Typically speaking, mod developers do not need to use the Cache classes directly, because caching is automatically handled by Card#fetch
Defined Under Namespace
Modules: Prepopulate, ViewCache Classes: Persistent, Temporary
Constant Summary collapse
- @@cache_by_class =
{}
Instance Attribute Summary collapse
-
#hard ⇒ Object
readonly
Returns the value of attribute hard.
-
#soft ⇒ Object
readonly
Returns the value of attribute soft.
Class Method Summary collapse
-
.[](klass) ⇒ {Card::Cache}
create a new cache for the ruby class provided.
-
.obj_to_key(obj) ⇒ String
generate a cache key from an object.
-
.renew ⇒ Object
establish clean context; clear the temporary caches and ensure we're using the latest stamp on the persistent caches.
-
.reset_all ⇒ Object
reset all caches for all classes.
-
.reset_global ⇒ Object
completely wipe out all caches, often including the Persistent cache of other decks using the same mechanism.
-
.reset_hard ⇒ Object
reset the Persistent cache for all classes.
-
.reset_other ⇒ Object
reset Codename cache and delete tmp files (the non-standard caches).
-
.reset_soft ⇒ Object
reset the Temporary cache for all classes.
Instance Method Summary collapse
-
#delete(key) ⇒ Object
delete specific cache entries by key.
-
#exist?(key) ⇒ true/false
test for the existence of the key in either cache.
-
#fetch(key, &block) ⇒ Object
like read, but also writes to hard cache.
-
#initialize(opts = {}) ⇒ Cache
constructor
Cache#new initializes a Temporary/soft cache, and -- if a :store opt is provided -- a Persistent/hard cache.
-
#read(key) ⇒ Object
read cache value (and write to soft cache if missing).
-
#reset ⇒ Object
reset both caches (for a given Card::Cache instance).
-
#write(key, value) ⇒ Object
write to hard (where applicable) and soft cache.
Methods included from Prepopulate
Constructor Details
#initialize(opts = {}) ⇒ Cache
Cache#new initializes a Temporary/soft cache, and -- if a :store opt is provided -- a Persistent/hard cache
142 143 144 145 146 147 |
# File 'lib/card/cache.rb', line 142 def initialize opts={} @klass = opts[:class] cache_by_class[@klass] = self @hard = Persistent.new opts if opts[:store] @soft = Temporary.new end |
Instance Attribute Details
#hard ⇒ Object (readonly)
Returns the value of attribute hard.
135 136 137 |
# File 'lib/card/cache.rb', line 135 def hard @hard end |
#soft ⇒ Object (readonly)
Returns the value of attribute soft.
135 136 137 |
# File 'lib/card/cache.rb', line 135 def soft @soft end |
Class Method Details
.[](klass) ⇒ {Card::Cache}
create a new cache for the ruby class provided
65 66 67 68 69 70 |
# File 'lib/card/cache.rb', line 65 def [] klass raise "nil klass" if klass.nil? cache_type = (@no_rails_cache ? nil : Cardio.cache) cache_by_class[klass] ||= new class: klass, store: cache_type end |
.obj_to_key(obj) ⇒ String
generate a cache key from an object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/card/cache.rb', line 123 def obj_to_key obj case obj when Hash obj.sort.map { |key, value| "#{key}=>(#{obj_to_key(value)})" } * "," when Array obj.map { |value| obj_to_key(value) } else obj.to_s end end |
.renew ⇒ Object
establish clean context; clear the temporary caches and ensure we're using the latest stamp on the persistent caches.
75 76 77 78 79 80 |
# File 'lib/card/cache.rb', line 75 def renew cache_by_class.each_value do |cache| cache.soft.reset cache.hard.renew if cache.hard end end |
.reset_all ⇒ Object
reset all caches for all classes
83 84 85 86 87 |
# File 'lib/card/cache.rb', line 83 def reset_all reset_hard reset_soft reset_other end |
.reset_global ⇒ Object
completely wipe out all caches, often including the Persistent cache of other decks using the same mechanism. Generally prefer reset_all
93 94 95 96 97 98 99 |
# File 'lib/card/cache.rb', line 93 def reset_global cache_by_class.each_value do |cache| cache.soft.reset cache.hard.annihilate if cache.hard end reset_other end |
.reset_hard ⇒ Object
reset the Persistent cache for all classes
102 103 104 105 106 |
# File 'lib/card/cache.rb', line 102 def reset_hard cache_by_class.each_value do |cache| cache.hard.reset if cache.hard end end |
.reset_other ⇒ Object
reset Codename cache and delete tmp files (the non-standard caches)
115 116 117 118 |
# File 'lib/card/cache.rb', line 115 def reset_other Card::Codename.reset_cache Card.delete_tmp_files end |
.reset_soft ⇒ Object
reset the Temporary cache for all classes
109 110 111 |
# File 'lib/card/cache.rb', line 109 def reset_soft cache_by_class.each_value { |cache| cache.soft.reset } end |
Instance Method Details
#delete(key) ⇒ Object
delete specific cache entries by key
174 175 176 177 |
# File 'lib/card/cache.rb', line 174 def delete key @hard.delete key if @hard @soft.delete key end |
#exist?(key) ⇒ true/false
test for the existence of the key in either cache
187 188 189 |
# File 'lib/card/cache.rb', line 187 def exist? key @soft.exist?(key) || (@hard && @hard.exist?(key)) end |
#fetch(key, &block) ⇒ Object
like read, but also writes to hard cache
166 167 168 169 170 |
# File 'lib/card/cache.rb', line 166 def fetch key, &block @soft.fetch(key) do @hard ? @hard.fetch(key, &block) : yield end end |
#read(key) ⇒ Object
read cache value (and write to soft cache if missing)
151 152 153 154 |
# File 'lib/card/cache.rb', line 151 def read key @soft.read(key) || (@hard && (ret = @hard.read(key)) && @soft.write(key, ret)) end |
#reset ⇒ Object
reset both caches (for a given Card::Cache instance)
180 181 182 183 |
# File 'lib/card/cache.rb', line 180 def reset @hard.reset if @hard @soft.reset end |
#write(key, value) ⇒ Object
write to hard (where applicable) and soft cache
159 160 161 162 |
# File 'lib/card/cache.rb', line 159 def write key, value @hard.write key, value if @hard @soft.write key, value end |