Class: Card::Cache
- Inherits:
-
Object
- Object
- Card::Cache
- Extended by:
- ClassMethods
- Defined in:
- lib/card/cache.rb,
lib/card/cache/all.rb,
lib/card/cache/shared.rb,
lib/card/cache/populate.rb,
lib/card/cache/temporary.rb,
lib/card/cache/card_class.rb,
lib/card/cache/shared_class.rb,
lib/card/cache/class_methods.rb
Overview
The Cache class manages and integrates Temporary and Shared caching. The Temporary cache is typically process- and request- specific and is often “ahead” of the database; the Shared 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, ClassMethods, Populate, SharedClass Classes: Shared, Temporary
Instance Attribute Summary collapse
-
#shared ⇒ Object
readonly
Returns the value of attribute shared.
-
#temp ⇒ Object
readonly
Returns the value of attribute temp.
Attributes included from ClassMethods
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
-
#read(key) ⇒ Object
read cache value (and write to temp cache if missing).
- #read_multi(keys) ⇒ Object
-
#reset ⇒ Object
reset both caches (for a given Card::Cache instance).
-
#write(key, value) ⇒ Object
write to hard (where applicable) and temp cache.
Methods included from ClassMethods
[], cache_by_class, renew, renew_shared, reset_all, reset_global, reset_other, reset_shared, reset_temp, restore, shared_cache, shared_on!, tallies
Methods included from Populate
#populate_fields, #populate_ids, #populate_names
Constructor Details
Instance Attribute Details
#shared ⇒ Object (readonly)
Returns the value of attribute shared.
19 20 21 |
# File 'lib/card/cache.rb', line 19 def shared @shared end |
#temp ⇒ Object (readonly)
Returns the value of attribute temp.
19 20 21 |
# File 'lib/card/cache.rb', line 19 def temp @temp end |
Instance Method Details
#delete(key) ⇒ Object
delete specific cache entries by key
74 75 76 77 78 79 |
# File 'lib/card/cache.rb', line 74 def delete key track :delete, key do @shared&.delete key @temp.delete key end end |
#exist?(key) ⇒ true/false
test for the existence of the key in either cache
89 90 91 |
# File 'lib/card/cache.rb', line 89 def exist? key @temp.exist?(key) || @shared&.exist?(key) end |
#fetch(key, &block) ⇒ Object
read and (if not there yet) write
64 65 66 67 68 69 70 |
# File 'lib/card/cache.rb', line 64 def fetch key, &block @temp.fetch(key) do track :fetch, key do @shared ? @shared.fetch(key, &block) : yield(key) end end end |
#read(key) ⇒ Object
read cache value (and write to temp cache if missing)
34 35 36 37 38 39 40 |
# File 'lib/card/cache.rb', line 34 def read key @temp.fetch(key) do track :read, key do @shared&.read key end end end |
#read_multi(keys) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/card/cache.rb', line 42 def read_multi keys with_multiple_keys keys do @temp.fetch_multi keys do |missing_keys| track :read_multi, missing_keys do @shared ? @shared.read_multi(missing_keys) : {} end end end end |
#reset ⇒ Object
reset both caches (for a given Card::Cache instance)
82 83 84 85 |
# File 'lib/card/cache.rb', line 82 def reset @shared&.reset @temp.reset end |
#write(key, value) ⇒ Object
write to hard (where applicable) and temp cache
55 56 57 58 59 60 |
# File 'lib/card/cache.rb', line 55 def write key, value track :write, key do @shared&.write key, value @temp.write key, value end end |