Class: IknowCache::Cache
- Inherits:
-
Object
- Object
- IknowCache::Cache
- Defined in:
- lib/iknow_cache.rb
Constant Summary collapse
- DEBUG =
false
Instance Attribute Summary collapse
-
#cache_group ⇒ Object
readonly
Returns the value of attribute cache_group.
-
#cache_options ⇒ Object
readonly
Returns the value of attribute cache_options.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#static_version ⇒ Object
readonly
Returns the value of attribute static_version.
Instance Method Summary collapse
- #delete(key, parent_path: nil, **options) ⇒ Object
- #delete_multi(keys, **options) ⇒ Object
- #fetch(key, parent_path: nil, **options, &block) ⇒ Object
- #fetch_multi(keys, write_options = nil) ⇒ Object
-
#initialize(cache_group, name, static_version, cache_options) ⇒ Cache
constructor
A new instance of Cache.
- #key ⇒ Object
- #read(key, parent_path: nil, **options) ⇒ Object
- #read_multi(keys) ⇒ Object
- #write(key, value, parent_path: nil, **options) ⇒ Object
- #write_multi(entries, options = nil) ⇒ Object
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_group = cache_group @name = name @static_version = static_version @cache_options = IknowCache.(cache_group., ).try { |x| x.dup.freeze } end |
Instance Attribute Details
#cache_group ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute cache_options.
198 199 200 |
# File 'lib/iknow_cache.rb', line 198 def @cache_options end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
198 199 200 |
# File 'lib/iknow_cache.rb', line 198 def name @name end |
#static_version ⇒ Object (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, **) p = path(key, parent_path) IknowCache.logger.debug("Cache Delete: #{p}") if DEBUG IknowCache.cache.delete(p, IknowCache.(, )) 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, **) 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.(, )) 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, **, &block) p = path(key, parent_path) IknowCache.logger.debug("Cache Fetch: #{p}") if DEBUG v = IknowCache.cache.fetch(p, IknowCache.(, ), &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, = nil) results = read_multi(keys) missing_keys = keys - results.keys if missing_keys.present? loaded_results = yield(missing_keys) write_multi(loaded_results, ) results.merge!(loaded_results) end results end |
#key ⇒ Object
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, **) p = path(key, parent_path) IknowCache.logger.debug("Cache Read: #{p}") if DEBUG v = IknowCache.cache.read(p, IknowCache.(, )) 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, **) p = path(key, parent_path) if DEBUG IknowCache.logger.debug("Cache Store: #{p} (#{IknowCache.(, ).inspect})") IknowCache.logger.debug("<= #{value.inspect}") end IknowCache.cache.write(p, value, IknowCache.(, )) 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, = nil) return {} if entries.blank? key_paths = path_multi(entries.keys) = IknowCache.(, ) entries.each do |key, value| IknowCache.logger.debug("Cache Multi-Write: #{key_paths[key]}") if DEBUG IknowCache.cache.write(key_paths[key], value, ) end end |