Class: IknowCache::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/iknow_cache.rb

Constant Summary collapse

DEBUG =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_group, name, static_version, cache_options) ⇒ Cache

Returns a new instance of Cache.



200
201
202
203
204
205
# File 'lib/iknow_cache.rb', line 200

def initialize(cache_group, name, static_version, cache_options)
  @cache_group    = cache_group
  @name           = name
  @static_version = static_version
  @cache_options  = IknowCache.merge_options(cache_group.default_options, cache_options).try { |x| x.dup.freeze }
end

Instance Attribute Details

#cache_groupObject (readonly)

Returns the value of attribute cache_group.



198
199
200
# File 'lib/iknow_cache.rb', line 198

def cache_group
  @cache_group
end

#cache_optionsObject (readonly)

Returns the value of attribute cache_options.



198
199
200
# File 'lib/iknow_cache.rb', line 198

def cache_options
  @cache_options
end

#nameObject (readonly)

Returns the value of attribute name.



198
199
200
# File 'lib/iknow_cache.rb', line 198

def name
  @name
end

#static_versionObject (readonly)

Returns the value of attribute static_version.



198
199
200
# File 'lib/iknow_cache.rb', line 198

def static_version
  @static_version
end

Instance Method Details

#delete(key, parent_path: nil, **options) ⇒ Object



232
233
234
235
236
# File 'lib/iknow_cache.rb', line 232

def delete(key, parent_path: nil, **options)
  p = path(key, parent_path)
  IknowCache.logger.debug("Cache Delete: #{p}") if DEBUG
  IknowCache.cache.delete(p, IknowCache.merge_options(cache_options, options))
end

#delete_multi(keys, **options) ⇒ Object



277
278
279
280
281
282
283
284
# File 'lib/iknow_cache.rb', line 277

def delete_multi(keys, **options)
  return if keys.blank?

  key_paths = path_multi(keys)

  IknowCache.logger.debug("Cache Delete Multi: #{key_paths}") if DEBUG
  IknowCache.cache.delete_multi(key_paths.values, IknowCache.merge_options(cache_options, options))
end

#fetch(key, parent_path: nil, **options, &block) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/iknow_cache.rb', line 207

def fetch(key, parent_path: nil, **options, &block)
  p = path(key, parent_path)
  IknowCache.logger.debug("Cache Fetch: #{p}") if DEBUG
  v = IknowCache.cache.fetch(p, IknowCache.merge_options(cache_options, options), &block)
  IknowCache.logger.debug("=> #{v.inspect}") if DEBUG
  v
end

#fetch_multi(keys, write_options = nil) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/iknow_cache.rb', line 238

def fetch_multi(keys, write_options = nil)
  results = read_multi(keys)

  missing_keys = keys - results.keys
  if missing_keys.present?
    loaded_results = yield(missing_keys)
    write_multi(loaded_results, write_options)
    results.merge!(loaded_results)
  end

  results
end

#keyObject



293
294
295
# File 'lib/iknow_cache.rb', line 293

def key
  cache_group.key
end

#read(key, parent_path: nil, **options) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/iknow_cache.rb', line 215

def read(key, parent_path: nil, **options)
  p = path(key, parent_path)
  IknowCache.logger.debug("Cache Read: #{p}") if DEBUG
  v = IknowCache.cache.read(p, IknowCache.merge_options(cache_options, options))
  IknowCache.logger.debug("=> #{v.inspect}") if DEBUG
  v
end

#read_multi(keys) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/iknow_cache.rb', line 251

def read_multi(keys)
  return {} if keys.blank?

  key_paths = path_multi(keys)
  path_keys = key_paths.invert

  IknowCache.logger.debug("Cache Multi-Read: #{key_paths.values.inspect}") if DEBUG
  raw = IknowCache.cache.read_multi(*key_paths.values)
  vs = raw.transform_keys { |path| path_keys[path] }
  IknowCache.logger.debug("=> #{vs.inspect}") if DEBUG
  vs
end

#write(key, value, parent_path: nil, **options) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/iknow_cache.rb', line 223

def write(key, value, parent_path: nil, **options)
  p = path(key, parent_path)
  if DEBUG
    IknowCache.logger.debug("Cache Store: #{p} (#{IknowCache.merge_options(cache_options, options).inspect})")
    IknowCache.logger.debug("<= #{value.inspect}")
  end
  IknowCache.cache.write(p, value, IknowCache.merge_options(cache_options, options))
end

#write_multi(entries, options = nil) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/iknow_cache.rb', line 264

def write_multi(entries, options = nil)
  return {} if entries.blank?

  key_paths = path_multi(entries.keys)
  options = IknowCache.merge_options(cache_options, options)

  entries.each do |key, value|
    IknowCache.logger.debug("Cache Multi-Write: #{key_paths[key]}") if DEBUG
    IknowCache.cache.write(key_paths[key], value, options)
  end
end