Class: Card::Cache
- Inherits:
-
Object
- Object
- Card::Cache
- Extended by:
- Prepopulate
- Defined in:
- lib/card/cache.rb,
lib/card/cache/all.rb,
lib/card/cache/temporary.rb,
lib/card/cache/card_class.rb,
lib/card/cache/persistent.rb,
lib/card/cache/prepopulate.rb,
lib/card/cache/persistent_class.rb
Overview
The Cache class manages and integrates Temporary and Persistent 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 ““
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: All, CardClass, PersistentClass, Prepopulate Classes: Persistent, Temporary
Constant Summary collapse
- @@cache_by_class =
{}
Class Attribute Summary collapse
-
.no_renewal ⇒ Object
Returns the value of attribute no_renewal.
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.
- .persistent_cache ⇒ Object
-
.renew ⇒ Object
clear the temporary caches and ensure we’re using the latest stamp on the persistent caches.
- .renew_persistent ⇒ Object
-
.reset ⇒ Object
reset standard cached for all classes.
-
.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
read and (if not there yet) write.
-
#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
135 136 137 138 139 140 |
# File 'lib/card/cache.rb', line 135 def initialize opts={} @klass = opts[:class] cache_by_class[@klass] = self @hard = Persistent.new opts if opts[:store] @soft = Temporary.new end |
Class Attribute Details
.no_renewal ⇒ Object
Returns the value of attribute no_renewal.
29 30 31 |
# File 'lib/card/cache.rb', line 29 def no_renewal @no_renewal end |
Instance Attribute Details
#hard ⇒ Object (readonly)
Returns the value of attribute hard.
128 129 130 |
# File 'lib/card/cache.rb', line 128 def hard @hard end |
#soft ⇒ Object (readonly)
Returns the value of attribute soft.
128 129 130 |
# File 'lib/card/cache.rb', line 128 def soft @soft end |
Class Method Details
.[](klass) ⇒ {Card::Cache}
create a new cache for the ruby class provided
34 35 36 37 38 39 |
# File 'lib/card/cache.rb', line 34 def [] klass raise "nil klass" if klass.nil? cache_type = persistent_cache || nil cache_by_class[klass] ||= new class: klass, store: cache_type end |
.obj_to_key(obj) ⇒ String
generate a cache key from an object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/card/cache.rb', line 116 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 |
.persistent_cache ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/card/cache.rb', line 41 def persistent_cache return @persistent_cache unless @persistent_cache.nil? @persistent_cache = case when ENV["NO_RAILS_CACHE"] then false when Cardio.config.persistent_cache then Cardio.cache else false end end |
.renew ⇒ Object
clear the temporary caches and ensure we’re using the latest stamp on the persistent caches.
54 55 56 57 58 59 60 61 62 |
# File 'lib/card/cache.rb', line 54 def renew return if no_renewal renew_persistent cache_by_class.each_value do |cache| cache.soft.reset cache.hard&.renew end end |
.renew_persistent ⇒ Object
64 65 66 |
# File 'lib/card/cache.rb', line 64 def renew_persistent Card::Cache::Persistent.renew if persistent_cache end |
.reset ⇒ Object
reset standard cached for all classes
69 70 71 72 |
# File 'lib/card/cache.rb', line 69 def reset reset_hard reset_soft end |
.reset_all ⇒ Object
reset all caches for all classes
75 76 77 78 79 |
# File 'lib/card/cache.rb', line 75 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
85 86 87 88 89 90 91 |
# File 'lib/card/cache.rb', line 85 def reset_global cache_by_class.each_value do |cache| cache.soft.reset cache.hard&.annihilate end reset_other end |
.reset_hard ⇒ Object
reset the Persistent cache for all classes
94 95 96 97 98 99 |
# File 'lib/card/cache.rb', line 94 def reset_hard Card::Cache::Persistent.reset if persistent_cache cache_by_class.each_value do |cache| cache.hard&.reset end end |
.reset_other ⇒ Object
reset Codename cache and delete tmp files (the non-standard caches)
108 109 110 111 |
# File 'lib/card/cache.rb', line 108 def reset_other Card::Codename.reset_cache Cardio::Utils.delete_tmp_files! end |
.reset_soft ⇒ Object
reset the Temporary cache for all classes
102 103 104 |
# File 'lib/card/cache.rb', line 102 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
165 166 167 168 |
# File 'lib/card/cache.rb', line 165 def delete key @hard&.delete key @soft.delete key end |
#exist?(key) ⇒ true/false
test for the existence of the key in either cache
178 179 180 |
# File 'lib/card/cache.rb', line 178 def exist? key @soft.exist?(key) || @hard&.exist?(key) end |
#fetch(key, &block) ⇒ Object
read and (if not there yet) write
159 160 161 |
# File 'lib/card/cache.rb', line 159 def fetch key, &block @soft.fetch(key) { @hard ? @hard.fetch(key, &block) : yield } end |
#read(key) ⇒ Object
read cache value (and write to soft cache if missing)
144 145 146 147 |
# File 'lib/card/cache.rb', line 144 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)
171 172 173 174 |
# File 'lib/card/cache.rb', line 171 def reset @hard&.reset @soft.reset end |
#write(key, value) ⇒ Object
write to hard (where applicable) and soft cache
152 153 154 155 |
# File 'lib/card/cache.rb', line 152 def write key, value @hard&.write key, value @soft.write key, value end |