iknow_cache 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14a5fbfe4eb06e835259e354c8a8fdfb9bc82c2d34bfe6ea62fc8269144ca8a5
4
- data.tar.gz: 22da19d2f809d5d092298d47d1b703a146ff6d143d7ac83c0191a8fae98eca36
3
+ metadata.gz: f2f251be058a4e00de4f8dc19eef0cfe2fd35423fe954d74648a51e45b11ee4c
4
+ data.tar.gz: 807534bc5abde0fe64d7d8a6a419d71e21695617b01e58b94d5da5dc90a39746
5
5
  SHA512:
6
- metadata.gz: 80d4ff3de7d25e388217e20922971d4378970591bc6a8c57672e1c4745e1451e57a2105f16a468c7845ce4103dae8b62a5f8abe5cf6ab66713a4fa9ec23aac8f
7
- data.tar.gz: c23861a1464d8e2ea0c6d711eac3b8517253f745cda66bb9c612b4c67be11f669fff637c0e5eeb49c967600d38b050692895104c7d84500d5d8aa78a4f3c9cc0
6
+ metadata.gz: 98c3d4cbb6e4f2dd6d6d0c145c3be61734e562927dca4be79ce2f91226faba0633da677be77459fd7b3c47d5a015808da8ab318fb3182f481e31a43d2b3681ce
7
+ data.tar.gz: c89989310153a661059cd79c58f337263ac711f65f8dc38cd259e2bd5ed0fae03421d1b5e3539fc6a3995c6bb6a8a91169530d8ab37d1042e7fc02a1dcd57b70
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class IknowCache
4
+ class Railtie < Rails::Railtie
5
+ config.after_initialize do
6
+ unless IknowCache.configured?
7
+ IknowCache.configure! do
8
+ logger Rails.logger
9
+ cache Rails.cache
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  class IknowCache
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/iknow_cache.rb CHANGED
@@ -1,6 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class IknowCache
4
+ Config = Struct.new(:logger, :cache)
5
+
6
+ class ConfigWriter
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def logger(logger)
12
+ @config.logger = logger
13
+ end
14
+
15
+ def cache(cache)
16
+ @config.cache = cache
17
+ end
18
+ end
19
+
20
+ def self.configured?
21
+ ! config.nil?
22
+ end
23
+
24
+ def self.configure!(&block)
25
+ raise ArgumentError.new("Already configured!") if configured?
26
+
27
+ config = Config.new
28
+ ConfigWriter.new(config).instance_eval(&block)
29
+ @config = config.freeze
30
+ end
31
+
32
+ class << self
33
+ attr_reader :config
34
+
35
+ def logger
36
+ config.logger
37
+ end
38
+
39
+ def cache
40
+ config.cache
41
+ end
42
+ end
43
+
4
44
  def self.register_group(name, key_name, default_options: nil, static_version: 1)
5
45
  group = CacheGroup.new(nil, name, key_name, default_options, static_version)
6
46
  yield group if block_given?
@@ -51,7 +91,7 @@ class IknowCache
51
91
  # invalidating all caches in it and its children
52
92
  def invalidate_cache_group(parent_key = nil)
53
93
  parent_path = self.parent_path(parent_key)
54
- Rails.cache.increment(version_path_string(parent_path))
94
+ IknowCache.cache.increment(version_path_string(parent_path))
55
95
  end
56
96
 
57
97
  # Fetch the path for this cache. We allow the parent_path to be precomputed
@@ -78,7 +118,7 @@ class IknowCache
78
118
  end
79
119
 
80
120
  def version(parent_path)
81
- Rails.cache.fetch(version_path_string(parent_path), raw: true) { 1 }
121
+ IknowCache.cache.fetch(version_path_string(parent_path), raw: true) { 1 }
82
122
  end
83
123
 
84
124
  # compute multiple paths at once: returns { key => path }
@@ -121,12 +161,12 @@ class IknowCache
121
161
  version_paths = version_by_pp.values
122
162
 
123
163
  # look up versions in cache
124
- versions = Rails.cache.read_multi(*version_paths, raw: true)
164
+ versions = IknowCache.cache.read_multi(*version_paths, raw: true)
125
165
 
126
166
  version_paths.each do |vp|
127
167
  next if versions.has_key?(vp)
128
168
 
129
- versions[vp] = Rails.cache.fetch(vp, raw: true) { 1 }
169
+ versions[vp] = IknowCache.cache.fetch(vp, raw: true) { 1 }
130
170
  end
131
171
 
132
172
  # swap in the versions
@@ -163,33 +203,33 @@ class IknowCache
163
203
 
164
204
  def fetch(key, parent_path: nil, **options, &block)
165
205
  p = path(key, parent_path)
166
- Rails.logger.debug("Cache Fetch: #{p}") if DEBUG
167
- v = Rails.cache.fetch(p, IknowCache.merge_options(cache_options, options), &block)
168
- Rails.logger.debug("=> #{v.inspect}") if DEBUG
206
+ IknowCache.logger.debug("Cache Fetch: #{p}") if DEBUG
207
+ v = IknowCache.cache.fetch(p, IknowCache.merge_options(cache_options, options), &block)
208
+ IknowCache.logger.debug("=> #{v.inspect}") if DEBUG
169
209
  v
170
210
  end
171
211
 
172
212
  def read(key, parent_path: nil, **options)
173
213
  p = path(key, parent_path)
174
- Rails.logger.debug("Cache Read: #{p}") if DEBUG
175
- v = Rails.cache.read(p, IknowCache.merge_options(cache_options, options))
176
- Rails.logger.debug("=> #{v.inspect}") if DEBUG
214
+ IknowCache.logger.debug("Cache Read: #{p}") if DEBUG
215
+ v = IknowCache.cache.read(p, IknowCache.merge_options(cache_options, options))
216
+ IknowCache.logger.debug("=> #{v.inspect}") if DEBUG
177
217
  v
178
218
  end
179
219
 
180
220
  def write(key, value, parent_path: nil, **options)
181
221
  p = path(key, parent_path)
182
222
  if DEBUG
183
- Rails.logger.debug("Cache Store: #{p} (#{IknowCache.merge_options(cache_options, options).inspect})")
184
- Rails.logger.debug("<= #{value.inspect}")
223
+ IknowCache.logger.debug("Cache Store: #{p} (#{IknowCache.merge_options(cache_options, options).inspect})")
224
+ IknowCache.logger.debug("<= #{value.inspect}")
185
225
  end
186
- Rails.cache.write(p, value, IknowCache.merge_options(cache_options, options))
226
+ IknowCache.cache.write(p, value, IknowCache.merge_options(cache_options, options))
187
227
  end
188
228
 
189
229
  def delete(key, parent_path: nil, **options)
190
230
  p = path(key, parent_path)
191
- Rails.logger.debug("Cache Delete: #{p}") if DEBUG
192
- Rails.cache.delete(p, IknowCache.merge_options(cache_options, options))
231
+ IknowCache.logger.debug("Cache Delete: #{p}") if DEBUG
232
+ IknowCache.cache.delete(p, IknowCache.merge_options(cache_options, options))
193
233
  end
194
234
 
195
235
  def read_multi(keys)
@@ -198,12 +238,12 @@ class IknowCache
198
238
  key_paths = path_multi(keys)
199
239
  path_keys = key_paths.invert
200
240
 
201
- Rails.logger.debug("Cache Multi-Read: #{key_paths.values.inspect}") if DEBUG
202
- raw = Rails.cache.read_multi(*key_paths.values)
241
+ IknowCache.logger.debug("Cache Multi-Read: #{key_paths.values.inspect}") if DEBUG
242
+ raw = IknowCache.cache.read_multi(*key_paths.values)
203
243
  vs = raw.each_with_object({}) do |(path, value), h|
204
244
  h[path_keys[path]] = value
205
245
  end
206
- Rails.logger.debug("=> #{vs.inspect}") if DEBUG
246
+ IknowCache.logger.debug("=> #{vs.inspect}") if DEBUG
207
247
  vs
208
248
  end
209
249
 
@@ -214,12 +254,14 @@ class IknowCache
214
254
  options = IknowCache.merge_options(cache_options, options)
215
255
 
216
256
  entries.each do |key, value|
217
- Rails.logger.debug("Cache Multi-Write: #{key_paths[key]}") if DEBUG
218
- Rails.cache.write(key_paths[key], value, options)
257
+ IknowCache.logger.debug("Cache Multi-Write: #{key_paths[key]}") if DEBUG
258
+ IknowCache.cache.write(key_paths[key], value, options)
219
259
  end
220
260
  end
221
261
 
222
- delegate :key, to: :cache_group
262
+ def key
263
+ cache_group.key
264
+ end
223
265
 
224
266
  private
225
267
 
@@ -249,3 +291,5 @@ class IknowCache
249
291
  end
250
292
  end
251
293
  end
294
+
295
+ require "iknow_cache/railtie" if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,23 +1,37 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iknow_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Andreae
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-08 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">"
18
32
  - !ruby/object:Gem::Version
19
33
  version: '5.0'
20
- type: :runtime
34
+ type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
@@ -63,6 +77,7 @@ files:
63
77
  - README.md
64
78
  - Rakefile
65
79
  - lib/iknow_cache.rb
80
+ - lib/iknow_cache/railtie.rb
66
81
  - lib/iknow_cache/version.rb
67
82
  homepage: https://github.com/iknow/iknow_cache
68
83
  licenses:
@@ -83,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
98
  - !ruby/object:Gem::Version
84
99
  version: '0'
85
100
  requirements: []
86
- rubygems_version: 3.0.3
101
+ rubygems_version: 3.1.6
87
102
  signing_key:
88
103
  specification_version: 4
89
104
  summary: iKnow's versioned nested cache