Class: CompositeCacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_cache_store.rb,
lib/composite_cache_store/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CompositeCacheStore

Returns a new CompositeCacheStore instance

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/composite_cache_store.rb', line 11

def initialize(options = {})
  options = options.dup || {}
  layers = options.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 = options[:logger]
  @options = options
end

Instance Attribute Details

#layersObject (readonly)

Returns the value of attribute layers.



7
8
9
# File 'lib/composite_cache_store.rb', line 7

def layers
  @layers
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/composite_cache_store.rb', line 8

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/composite_cache_store.rb', line 7

def options
  @options
end

Instance Method Details

#cleanupObject



122
123
124
# File 'lib/composite_cache_store.rb', line 122

def cleanup(...)
  layers.map { |layer| layer.cleanup(...) }.last
end

#clearObject



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, options = nil)
  provisional_layers.each { |layer| layer.delete(name, options) }
  layers.last.decrement(name, amount, options)
end

#deleteObject



100
101
102
# File 'lib/composite_cache_store.rb', line 100

def delete(...)
  layers.map { |layer| layer.delete(...) }.last
end

#delete_matchedObject



108
109
110
# File 'lib/composite_cache_store.rb', line 108

def delete_matched(...)
  layers.map { |layer| layer.delete_matched(...) }.last
end

#delete_multiObject



104
105
106
# File 'lib/composite_cache_store.rb', line 104

def delete_multi(...)
  layers.map { |layer| layer.delete_multi(...) }.last
end

#exist?Boolean

Returns:

  • (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, options = nil, &block)
  options ||= {}

  if options[: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, options) }
    return value
  end

  read(name, options) do |value, warm_layer|
    value ||= block&.call(name) unless warm_layer

    layers.each do |layer|
      break if layer == warm_layer
      layer.write(name, value, options) unless value.nil? && options[:skip_nil]
    end

    return value
  end
end

#fetch_multi(*names, &block) ⇒ Object

Raises:

  • (ArgumentError)


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
  options = keys.extract_options!

  if options[:force]
    value = keys.each_with_object({}) { |key, memo| memo[key] = block&.call(key) }
    layers.each { |layer| layer.write_multi(value, options) }
    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 options[:skip_nil]

    layers.each do |layer|
      break if layer == warm_layer
      layer.write_multi(value, options)
    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, options = nil)
  provisional_layers.each { |layer| layer.delete(name, options) }
  layers.last.increment(name, amount, options)
end

#muteObject



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

Yields:

  • (value, warm_layer)


26
27
28
29
30
31
# File 'lib/composite_cache_store.rb', line 26

def read(name, options = nil)
  value = nil
  warm_layer = layers.find { |layer| layer_read?(layer, name, options) { |val| value = val } }
  yield(value, warm_layer) if block_given?
  value
end

#read_multi(*names) {|value, warm_layer| ... } ⇒ Object

Yields:

  • (value, warm_layer)


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, options = nil)
  layers.map { |layer| layer.write(name, value, options) }.last
end

#write_multi(hash, options = nil) ⇒ Object



96
97
98
# File 'lib/composite_cache_store.rb', line 96

def write_multi(hash, options = nil)
  layers.map { |layer| layer.write_multi(hash, options) }.last
end