Class: CacheFailover::Store

Inherits:
ActiveSupport::Cache::Store
  • Object
show all
Defined in:
lib/cache_failover/store.rb

Defined Under Namespace

Classes: BrotliCompressor

Constant Summary collapse

MARK_BR_COMPRESSED =
"\x02".b
DEFAULT_OPTIONS =
{
  timeout: 5,
  compress: true,
  cache_db: 'cache'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_stores) ⇒ Store

Returns a new instance of Store.



22
23
24
25
# File 'lib/cache_failover/store.rb', line 22

def initialize(cache_stores)
  core_stores(cache_stores)
  core_store
end

Class Method Details

.supports_cache_versioning?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/cache_failover/store.rb', line 244

def self.supports_cache_versioning?
  true
end

Instance Method Details

#cache_failover_loggerObject



248
249
250
# File 'lib/cache_failover/store.rb', line 248

def cache_failover_logger
  @cache_failover_logger ||= Logger.new("log/#{CONFIG[:RAILS_ENV]}.log")
end

#clear(init_options = {}) ⇒ Object



211
212
213
214
215
216
217
218
219
220
# File 'lib/cache_failover/store.rb', line 211

def clear(init_options = {})
  retries = 0
  begin
    core_store.clear(**init_options)
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#core_storeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cache_failover/store.rb', line 27

def core_store
  if defined?(Thread.current[:cache_core_store]) && Thread.current[:cache_core_store].present? && caching_up?(Thread.current[:cache_core_store], options)
    return Thread.current[:cache_core_store]
  else
    Thread.current[:cache_core_store] = nil
    core_stores.each do |cs|
      next if defined?(Thread.current[:cache_core_store]) && Thread.current[:cache_core_store].present?
      Thread.current[:cache_core_store] = cs[:store]
      options(cs[:options])
      cache_failover_logger.info("CacheFailover: caching_up?: #{cs[:store].class.name}")
      cache_failover_logger.info("CacheFailover: caching_up?: #{options}")
      _store_up = caching_up?(cs[:store], options)
      cache_failover_logger.info("#{_store_up}")
      if _store_up == true
        break
      else
        Thread.current[:cache_core_store] = nil
        next
      end
    end
  end
end

#core_stores(core_stores = []) ⇒ Object



50
51
52
53
# File 'lib/cache_failover/store.rb', line 50

def core_stores(core_stores = [])
  return @core_stores if defined?(@core_stores) && core_stores.blank?
  @core_stores = core_stores
end

#decrement(name, amount = 1, **init_options) ⇒ Object



233
234
235
236
237
238
239
240
241
242
# File 'lib/cache_failover/store.rb', line 233

def decrement(name, amount = 1, **init_options)
  retries = 0
  begin
    core_store.decrement(expanded_cache_key(name), amount, **init_options)
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#delete(name, init_options = {}) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/cache_failover/store.rb', line 200

def delete(name, init_options = {})
  retries = 0
  begin
    core_store.delete(expanded_cache_key(name), init_options)
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#exist?(name, init_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
196
197
198
# File 'lib/cache_failover/store.rb', line 189

def exist?(name, init_options = {})
  retries = 0
  begin
    core_store.exist?(expanded_cache_key(name), init_options)
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#fetch(name, init_options = nil, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cache_failover/store.rb', line 60

def fetch(name, init_options = nil, &block)
  retries = 0
  begin
  options(init_options)

  if !block_given? && options[:force]
    raise ArgumentError, "Missing block: Calling `Cache#fetch` with `force: true` requires a block."
  end

  get_value(
    core_store.fetch(expanded_cache_key(name), options.merge(compress: false)) do
      if block_given?
        store_value(block.call, options)
      else
        nil
      end
    end,
    options
  )
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#fetch_multi(*names) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cache_failover/store.rb', line 163

def fetch_multi(*names)
  retries = 0
  begin
    options = names.extract_options!
    expanded_names = names.map { |name| expanded_cache_key(name) }
    options(options)

    reads = core_store.send(:read_multi_entries, expanded_names, **options)
    reads.map do |key, val|
      [key, store_value(val, options)]
    end.to_h

    writes = {}
    ordered = names.index_with do |name|
      reads.fetch(name) { writes[name] = yield(name) }
    end

    write_multi(writes)
    ordered
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#increment(name, amount = 1, **init_options) ⇒ Object



222
223
224
225
226
227
228
229
230
231
# File 'lib/cache_failover/store.rb', line 222

def increment(name, amount = 1, **init_options)
  retries = 0
  begin
    core_store.increment(expanded_cache_key(name), amount, **init_options)
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#options(init_options = {}) ⇒ Object



55
56
57
58
# File 'lib/cache_failover/store.rb', line 55

def options(init_options = {})
  return @init_options if defined?(@init_options) && init_options.blank?
  @init_options = init_options.reverse_merge(DEFAULT_OPTIONS)
end

#read(name, init_options = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cache_failover/store.rb', line 105

def read(name, init_options = nil)
  retries = 0
  begin
    options(init_options)

    payload = core_store.read(
      expanded_cache_key(name),
      options
    )

    get_value(payload, options)
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#read_multi(*names) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cache_failover/store.rb', line 146

def read_multi(*names)
  retries = 0
  begin
    options = names.extract_options!
    names = names.map { |name| expanded_cache_key(name) }
    options(options)

    core_store.read_multi(*names, options).map do |key, val|
      [key, get_value(val, options)]
    end.to_h
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#write(name, value, init_options = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cache_failover/store.rb', line 86

def write(name, value, init_options = nil)
  retries = 0
  begin
    options(init_options)

    payload = store_value(value, options)

    core_store.write(
      expanded_cache_key(name),
      payload,
      options.merge(compress: false)
    )
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end

#write_multi(hash, init_options = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cache_failover/store.rb', line 123

def write_multi(hash, init_options = nil)
  retries = 0
  begin
    options(init_options)

    new_hash = hash.map do |key, val|
      [
        expanded_cache_key(key),
        store_value(val, options),
      ]
    end

    core_store.write_multi(
      new_hash,
      options.merge(compress: false)
    )
  rescue => ex
    Thread.current[:cache_core_store] = nil
    core_store
    retry if (retries += 1) < core_stores.length
  end
end