Class: IknowCache::CacheGroup
- Inherits:
-
Object
- Object
- IknowCache::CacheGroup
- Defined in:
- lib/iknow_cache.rb
Constant Summary collapse
- ROOT_PATH =
'IknowCache'
Instance Attribute Summary collapse
-
#default_options ⇒ Object
readonly
Returns the value of attribute default_options.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#key_name ⇒ Object
readonly
Returns the value of attribute key_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#delete_all(key, parent_path: nil) ⇒ Object
Clear this key in all Caches in this CacheGroup.
-
#initialize(parent, name, key_name, default_options, static_version) ⇒ CacheGroup
constructor
A new instance of CacheGroup.
-
#invalidate_cache_group(parent_key = nil) ⇒ Object
Clear all keys in this cache group (for the given parent), invalidating all caches in it and its children.
- #parent_path(parent_key = nil) ⇒ Object
-
#parent_path_multi(parent_keys = nil) ⇒ Object
look up multiple parent paths at once, returns { key => parent_path }.
-
#path(key, parent_path = nil) ⇒ Object
Fetch the path for this cache.
-
#path_multi(keys) ⇒ Object
compute multiple paths at once: returns { key => path }.
- #register_cache(name, static_version: nil, cache_options: nil) ⇒ Object
- #register_child_group(name, key_name, default_options: nil, static_version: 1) {|group| ... } ⇒ Object
- #version(parent_path) ⇒ Object
-
#version_multi(parent_paths) ⇒ Object
Look up multiple versions at once, returns { parent_path => version }.
Constructor Details
#initialize(parent, name, key_name, default_options, static_version) ⇒ CacheGroup
Returns a new instance of CacheGroup.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/iknow_cache.rb', line 55 def initialize(parent, name, key_name, , static_version) @parent = parent @name = name @key_name = key_name @key = Struct.new(*parent.try { |p| p.key.members }, key_name) = IknowCache.(parent&., ).try { |x| x.dup.freeze } @static_version = static_version @caches = [] @children = [] end |
Instance Attribute Details
#default_options ⇒ Object (readonly)
Returns the value of attribute default_options.
53 54 55 |
# File 'lib/iknow_cache.rb', line 53 def end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
53 54 55 |
# File 'lib/iknow_cache.rb', line 53 def key @key end |
#key_name ⇒ Object (readonly)
Returns the value of attribute key_name.
53 54 55 |
# File 'lib/iknow_cache.rb', line 53 def key_name @key_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
53 54 55 |
# File 'lib/iknow_cache.rb', line 53 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
53 54 55 |
# File 'lib/iknow_cache.rb', line 53 def parent @parent end |
Instance Method Details
#delete_all(key, parent_path: nil) ⇒ Object
Clear this key in all Caches in this CacheGroup. It is an error to do this if this CacheGroup has an statically versioned child, as that child cannot be invalidated.
82 83 84 85 86 87 88 89 90 |
# File 'lib/iknow_cache.rb', line 82 def delete_all(key, parent_path: nil) @caches.each do |cache| cache.delete(key, parent_path: parent_path) end @children.each do |child_group| child_group.invalidate_cache_group(key) end end |
#invalidate_cache_group(parent_key = nil) ⇒ Object
Clear all keys in this cache group (for the given parent), invalidating all caches in it and its children
94 95 96 97 |
# File 'lib/iknow_cache.rb', line 94 def invalidate_cache_group(parent_key = nil) parent_path = self.parent_path(parent_key) IknowCache.cache.increment(version_path_string(parent_path)) end |
#parent_path(parent_key = nil) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/iknow_cache.rb', line 114 def parent_path(parent_key = nil) if parent.nil? ROOT_PATH else parent.path(parent_key) end end |
#parent_path_multi(parent_keys = nil) ⇒ Object
look up multiple parent paths at once, returns { key => parent_path }
151 152 153 154 155 156 157 |
# File 'lib/iknow_cache.rb', line 151 def parent_path_multi(parent_keys = nil) if parent.nil? parent_keys.each_with_object({}) { |k, h| h[k] = ROOT_PATH } else parent.path_multi(parent_keys) end end |
#path(key, parent_path = nil) ⇒ Object
Fetch the path for this cache. We allow the parent_path to be precomputed to save hitting the cache multiple times for its version.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/iknow_cache.rb', line 101 def path(key, parent_path = nil) if key.nil? || key[self.key_name].nil? raise ArgumentError.new("Missing required key '#{self.key_name}' for cache '#{self.name}'") end key_value = key[self.key_name] parent_path ||= self.parent_path(key) version = self.version(parent_path) path_string(parent_path, version, key_value) end |
#path_multi(keys) ⇒ Object
compute multiple paths at once: returns { key => path }
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/iknow_cache.rb', line 127 def path_multi(keys) # compute parent path for each key parent_paths = self.parent_path_multi(keys) # and versions for each parent path versions = self.version_multi(parent_paths.values.uniq) # update parent_paths with our paths keys.each do |key| parent_path = parent_paths[key] version = versions[parent_path] key_value = key[self.key_name] unless key_value raise ArgumentError.new("Required cache key missing: #{self.key_name}") end parent_paths[key] = path_string(parent_path, version, key_value) end parent_paths end |
#register_cache(name, static_version: nil, cache_options: nil) ⇒ Object
74 75 76 77 78 |
# File 'lib/iknow_cache.rb', line 74 def register_cache(name, static_version: nil, cache_options: nil) c = Cache.new(self, name, static_version, ) @caches << c c end |
#register_child_group(name, key_name, default_options: nil, static_version: 1) {|group| ... } ⇒ Object
67 68 69 70 71 72 |
# File 'lib/iknow_cache.rb', line 67 def register_child_group(name, key_name, default_options: nil, static_version: 1) group = CacheGroup.new(self, name, key_name, , static_version) @children << group yield group if block_given? group end |
#version(parent_path) ⇒ Object
122 123 124 |
# File 'lib/iknow_cache.rb', line 122 def version(parent_path) IknowCache.cache.fetch(version_path_string(parent_path), raw: true) { 1 } end |
#version_multi(parent_paths) ⇒ Object
Look up multiple versions at once, returns { parent_path => version }
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/iknow_cache.rb', line 160 def version_multi(parent_paths) # compute version paths version_by_pp = parent_paths.each_with_object({}) { |pp, h| h[pp] = version_path_string(pp) } version_paths = version_by_pp.values # look up versions in cache versions = IknowCache.cache.read_multi(*version_paths, raw: true) version_paths.each do |vp| next if versions.has_key?(vp) versions[vp] = IknowCache.cache.fetch(vp, raw: true) { 1 } end # swap in the versions parent_paths.each do |pp| vp = version_by_pp[pp] version = versions[vp] version_by_pp[pp] = version end version_by_pp end |