Class: IknowCache::CacheGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/iknow_cache.rb

Constant Summary collapse

ROOT_PATH =
'IknowCache'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, key_name, default_options, static_version) ⇒ CacheGroup

Returns a new instance of CacheGroup.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/iknow_cache.rb', line 13

def initialize(parent, name, key_name, default_options, static_version)
  @parent          = parent
  @name            = name
  @key_name        = key_name
  @key             = Struct.new(*parent.try { |p| p.key.members }, key_name)
  @default_options = IknowCache.merge_options(parent&.default_options, default_options).try { |x| x.dup.freeze }
  @static_version  = static_version

  @caches          = []
  @children        = []
end

Instance Attribute Details

#default_optionsObject (readonly)

Returns the value of attribute default_options.



11
12
13
# File 'lib/iknow_cache.rb', line 11

def default_options
  @default_options
end

#keyObject (readonly)

Returns the value of attribute key.



11
12
13
# File 'lib/iknow_cache.rb', line 11

def key
  @key
end

#key_nameObject (readonly)

Returns the value of attribute key_name.



11
12
13
# File 'lib/iknow_cache.rb', line 11

def key_name
  @key_name
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/iknow_cache.rb', line 11

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



11
12
13
# File 'lib/iknow_cache.rb', line 11

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.



40
41
42
43
44
45
46
47
48
# File 'lib/iknow_cache.rb', line 40

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



52
53
54
55
# File 'lib/iknow_cache.rb', line 52

def invalidate_cache_group(parent_key = nil)
  parent_path = self.parent_path(parent_key)
  Rails.cache.increment(version_path_string(parent_path))
end

#parent_path(parent_key = nil) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/iknow_cache.rb', line 72

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 }



109
110
111
112
113
114
115
# File 'lib/iknow_cache.rb', line 109

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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/iknow_cache.rb', line 59

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 }



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/iknow_cache.rb', line 85

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



32
33
34
35
36
# File 'lib/iknow_cache.rb', line 32

def register_cache(name, cache_options: nil)
  c = Cache.new(self, name, cache_options)
  @caches << c
  c
end

#register_child_group(name, key_name, default_options: nil, static_version: 1) {|group| ... } ⇒ Object

Yields:

  • (group)


25
26
27
28
29
30
# File 'lib/iknow_cache.rb', line 25

def register_child_group(name, key_name, default_options: nil, static_version: 1)
  group = CacheGroup.new(self, name, key_name, default_options, static_version)
  @children << group
  yield group if block_given?
  group
end

#version(parent_path) ⇒ Object



80
81
82
# File 'lib/iknow_cache.rb', line 80

def version(parent_path)
  Rails.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 }



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/iknow_cache.rb', line 118

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 = Rails.cache.read_multi(*version_paths, raw: true)

  version_paths.each do |vp|
    next if versions.has_key?(vp)

    versions[vp] = Rails.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