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, 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.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/iknow_cache.rb', line 53 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.
51 52 53 |
# File 'lib/iknow_cache.rb', line 51 def end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
51 52 53 |
# File 'lib/iknow_cache.rb', line 51 def key @key end |
#key_name ⇒ Object (readonly)
Returns the value of attribute key_name.
51 52 53 |
# File 'lib/iknow_cache.rb', line 51 def key_name @key_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
51 52 53 |
# File 'lib/iknow_cache.rb', line 51 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
51 52 53 |
# File 'lib/iknow_cache.rb', line 51 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.
80 81 82 83 84 85 86 87 88 |
# File 'lib/iknow_cache.rb', line 80 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
92 93 94 95 |
# File 'lib/iknow_cache.rb', line 92 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
112 113 114 115 116 117 118 |
# File 'lib/iknow_cache.rb', line 112 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 }
149 150 151 152 153 154 155 |
# File 'lib/iknow_cache.rb', line 149 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.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/iknow_cache.rb', line 99 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 }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/iknow_cache.rb', line 125 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, cache_options: nil) ⇒ Object
72 73 74 75 76 |
# File 'lib/iknow_cache.rb', line 72 def register_cache(name, cache_options: nil) c = Cache.new(self, name, ) @caches << c c end |
#register_child_group(name, key_name, default_options: nil, static_version: 1) {|group| ... } ⇒ Object
65 66 67 68 69 70 |
# File 'lib/iknow_cache.rb', line 65 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
120 121 122 |
# File 'lib/iknow_cache.rb', line 120 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 }
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/iknow_cache.rb', line 158 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 |