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.



158
159
160
161
162
# File 'lib/iknow_cache.rb', line 158

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.



156
157
158
# File 'lib/iknow_cache.rb', line 156

def cache_group
  @cache_group
end

#cache_optionsObject (readonly)

Returns the value of attribute cache_options.



156
157
158
# File 'lib/iknow_cache.rb', line 156

def cache_options
  @cache_options
end

#nameObject (readonly)

Returns the value of attribute name.



156
157
158
# File 'lib/iknow_cache.rb', line 156

def name
  @name
end

Instance Method Details

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



189
190
191
192
193
# File 'lib/iknow_cache.rb', line 189

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

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



164
165
166
167
168
169
170
# File 'lib/iknow_cache.rb', line 164

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

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



172
173
174
175
176
177
178
# File 'lib/iknow_cache.rb', line 172

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

#read_multi(keys) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/iknow_cache.rb', line 195

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

  key_paths = path_multi(keys)
  path_keys = key_paths.invert

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

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



180
181
182
183
184
185
186
187
# File 'lib/iknow_cache.rb', line 180

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

#write_multi(entries, options = nil) ⇒ Object



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

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|
    Rails.logger.debug("Cache Multi-Write: #{key_paths[key]}") if DEBUG
    Rails.cache.write(key_paths[key], value, options)
  end
end