Class: CompositeCacheStore
- Inherits:
-
Object
- Object
- CompositeCacheStore
- Defined in:
- lib/composite_cache_store.rb,
lib/composite_cache_store/version.rb
Constant Summary collapse
- VERSION =
"0.0.4"
Instance Attribute Summary collapse
-
#layers ⇒ Object
readonly
Returns the value of attribute layers.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #clear ⇒ Object
- #decrement(name, amount = 1, options = nil) ⇒ Object
- #delete ⇒ Object
- #delete_matched ⇒ Object
- #delete_multi ⇒ Object
- #exist? ⇒ Boolean
- #fetch(name, options = nil, &block) ⇒ Object
- #fetch_multi(*names, &block) ⇒ Object
- #increment(name, amount = 1, options = nil) ⇒ Object
-
#initialize(options = {}) ⇒ CompositeCacheStore
constructor
Returns a new CompositeCacheStore instance.
- #mute ⇒ Object
- #read(name, options = nil) {|value, warm_layer| ... } ⇒ Object
- #read_multi(*names) {|value, warm_layer| ... } ⇒ Object
- #silence! ⇒ Object
- #write(name, value, options = nil) ⇒ Object
- #write_multi(hash, options = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CompositeCacheStore
Returns a new CompositeCacheStore instance
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/composite_cache_store.rb', line 11 def initialize( = {}) = .dup || {} layers = .delete(:layers) || [] raise ArgumentError.new("A layered cache requires more than 1 layer!") unless layers.size > 1 unless layers.all? { |layer| layer.is_a? ActiveSupport::Cache::Store } raise ArgumentError.new("All layers must be instances of ActiveSupport::Cache::Store!") end @layers = layers.freeze @logger = [:logger] @options = end |
Instance Attribute Details
#layers ⇒ Object (readonly)
Returns the value of attribute layers.
7 8 9 |
# File 'lib/composite_cache_store.rb', line 7 def layers @layers end |
#logger ⇒ Object
Returns the value of attribute logger.
8 9 10 |
# File 'lib/composite_cache_store.rb', line 8 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/composite_cache_store.rb', line 7 def @options end |
Instance Method Details
#cleanup ⇒ Object
122 123 124 |
# File 'lib/composite_cache_store.rb', line 122 def cleanup(...) layers.map { |layer| layer.cleanup(...) }.last end |
#clear ⇒ Object
126 127 128 |
# File 'lib/composite_cache_store.rb', line 126 def clear(...) layers.map { |layer| layer.clear(...) }.last end |
#decrement(name, amount = 1, options = nil) ⇒ Object
117 118 119 120 |
# File 'lib/composite_cache_store.rb', line 117 def decrement(name, amount = 1, = nil) provisional_layers.each { |layer| layer.delete(name, ) } layers.last.decrement(name, amount, ) end |
#delete ⇒ Object
100 101 102 |
# File 'lib/composite_cache_store.rb', line 100 def delete(...) layers.map { |layer| layer.delete(...) }.last end |
#delete_matched ⇒ Object
108 109 110 |
# File 'lib/composite_cache_store.rb', line 108 def delete_matched(...) layers.map { |layer| layer.delete_matched(...) }.last end |
#delete_multi ⇒ Object
104 105 106 |
# File 'lib/composite_cache_store.rb', line 104 def delete_multi(...) layers.map { |layer| layer.delete_multi(...) }.last end |
#exist? ⇒ Boolean
130 131 132 |
# File 'lib/composite_cache_store.rb', line 130 def exist?(...) layers.any? { |layer| layer.exist?(...) } end |
#fetch(name, options = nil, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/composite_cache_store.rb', line 40 def fetch(name, = nil, &block) ||= {} if [:force] raise ArgumentError, "Missing block: Calling `Cache#fetch` with `force: true` requires a block." unless block value = block&.call(name) layers.each { |layer| layer.write(name, value, ) } return value end read(name, ) do |value, warm_layer| value ||= block&.call(name) unless warm_layer layers.each do |layer| break if layer == warm_layer layer.write(name, value, ) unless value.nil? && [:skip_nil] end return value end end |
#fetch_multi(*names, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/composite_cache_store.rb', line 62 def fetch_multi(*names, &block) raise ArgumentError, "Missing block: `Cache#fetch_multi` requires a block." unless block keys = names.dup = keys. if [:force] value = keys.each_with_object({}) { |key, memo| memo[key] = block&.call(key) } layers.each { |layer| layer.write_multi(value, ) } return value end read_multi(*names) do |value, warm_layer| unless warm_layer missing_keys = keys - value.keys missing_keys.each { |key| value[key] = block&.call(key) } end value.compact! if [:skip_nil] layers.each do |layer| break if layer == warm_layer layer.write_multi(value, ) end # return ordered hash value return keys.each_with_object({}) { |key, memo| memo[key] = value[key] } end end |
#increment(name, amount = 1, options = nil) ⇒ Object
112 113 114 115 |
# File 'lib/composite_cache_store.rb', line 112 def increment(name, amount = 1, = nil) provisional_layers.each { |layer| layer.delete(name, ) } layers.last.increment(name, amount, ) end |
#mute ⇒ Object
134 135 136 |
# File 'lib/composite_cache_store.rb', line 134 def mute layers.map { |layer| layer.mute { yield } }.last end |
#read(name, options = nil) {|value, warm_layer| ... } ⇒ Object
26 27 28 29 30 31 |
# File 'lib/composite_cache_store.rb', line 26 def read(name, = nil) value = nil warm_layer = layers.find { |layer| layer_read?(layer, name, ) { |val| value = val } } yield(value, warm_layer) if block_given? value end |
#read_multi(*names) {|value, warm_layer| ... } ⇒ Object
33 34 35 36 37 38 |
# File 'lib/composite_cache_store.rb', line 33 def read_multi(*names) value = {} warm_layer = layers.find { |layer| layer_read_multi?(layer, *names) { |val| value.merge!(val) } } yield(value, warm_layer) if block_given? value end |
#silence! ⇒ Object
138 139 140 |
# File 'lib/composite_cache_store.rb', line 138 def silence! layers.map { |layer| layer.silence! }.last end |
#write(name, value, options = nil) ⇒ Object
92 93 94 |
# File 'lib/composite_cache_store.rb', line 92 def write(name, value, = nil) layers.map { |layer| layer.write(name, value, ) }.last end |
#write_multi(hash, options = nil) ⇒ Object
96 97 98 |
# File 'lib/composite_cache_store.rb', line 96 def write_multi(hash, = nil) layers.map { |layer| layer.write_multi(hash, ) }.last end |