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, cache_options) ⇒ Cache

Returns a new instance of Cache.



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

def initialize(cache_group, name, cache_options)
  @cache_group   = cache_group
  @name          = name
  @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.



196
197
198
# File 'lib/iknow_cache.rb', line 196

def cache_group
  @cache_group
end

#cache_optionsObject (readonly)

Returns the value of attribute cache_options.



196
197
198
# File 'lib/iknow_cache.rb', line 196

def cache_options
  @cache_options
end

#nameObject (readonly)

Returns the value of attribute name.



196
197
198
# File 'lib/iknow_cache.rb', line 196

def name
  @name
end

Instance Method Details

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



229
230
231
232
233
# File 'lib/iknow_cache.rb', line 229

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

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



204
205
206
207
208
209
210
# File 'lib/iknow_cache.rb', line 204

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

#keyObject



262
263
264
# File 'lib/iknow_cache.rb', line 262

def key
  cache_group.key
end

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



212
213
214
215
216
217
218
# File 'lib/iknow_cache.rb', line 212

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



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

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.each_with_object({}) do |(path, value), h|
    h[path_keys[path]] = value
  end
  IknowCache.logger.debug("=> #{vs.inspect}") if DEBUG
  vs
end

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



220
221
222
223
224
225
226
227
# File 'lib/iknow_cache.rb', line 220

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



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

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