iknow_cache 1.1.1 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/iknow_cache/railtie.rb +14 -0
- data/lib/iknow_cache/version.rb +1 -1
- data/lib/iknow_cache.rb +109 -32
- metadata +20 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b80c0ec650d3fd5801512fefc162526b6502da2553e1640a3495defe4f7c5195
|
4
|
+
data.tar.gz: e5aa1520f3ffe851ca6bf37fd71946e44dd9706b1cf38b781c88cdfd11494607
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50155988a7d4926758e063c06d01e0791e646ce0177ee33652384079fb88e403dc62718cc648f14115b8596f776f619f6e64ea64d2b3a9189a67c764720f083a
|
7
|
+
data.tar.gz: 8e4464752708f4e966822a08b05c68d514fa31b90ecad296748f2fdacb1e735ab379e2a135b0c7a50f338de88ce2e128c408f17046455a4b1fd565cedcc84ec1
|
data/lib/iknow_cache/version.rb
CHANGED
data/lib/iknow_cache.rb
CHANGED
@@ -1,6 +1,48 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support/version'
|
4
|
+
|
3
5
|
class IknowCache
|
6
|
+
Config = Struct.new(:logger, :cache)
|
7
|
+
|
8
|
+
class ConfigWriter
|
9
|
+
def initialize(config)
|
10
|
+
@config = config
|
11
|
+
end
|
12
|
+
|
13
|
+
def logger(logger)
|
14
|
+
@config.logger = logger
|
15
|
+
end
|
16
|
+
|
17
|
+
def cache(cache)
|
18
|
+
@config.cache = cache
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.configured?
|
23
|
+
!config.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configure!(&block)
|
27
|
+
raise ArgumentError.new('Already configured!') if configured?
|
28
|
+
|
29
|
+
config = Config.new
|
30
|
+
ConfigWriter.new(config).instance_eval(&block)
|
31
|
+
@config = config.freeze
|
32
|
+
end
|
33
|
+
|
34
|
+
class << self
|
35
|
+
attr_reader :config
|
36
|
+
|
37
|
+
def logger
|
38
|
+
config.logger
|
39
|
+
end
|
40
|
+
|
41
|
+
def cache
|
42
|
+
config.cache
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
4
46
|
def self.register_group(name, key_name, default_options: nil, static_version: 1)
|
5
47
|
group = CacheGroup.new(nil, name, key_name, default_options, static_version)
|
6
48
|
yield group if block_given?
|
@@ -29,8 +71,8 @@ class IknowCache
|
|
29
71
|
group
|
30
72
|
end
|
31
73
|
|
32
|
-
def register_cache(name, cache_options: nil)
|
33
|
-
c = Cache.new(self, name, cache_options)
|
74
|
+
def register_cache(name, static_version: nil, cache_options: nil)
|
75
|
+
c = Cache.new(self, name, static_version, cache_options)
|
34
76
|
@caches << c
|
35
77
|
c
|
36
78
|
end
|
@@ -51,7 +93,7 @@ class IknowCache
|
|
51
93
|
# invalidating all caches in it and its children
|
52
94
|
def invalidate_cache_group(parent_key = nil)
|
53
95
|
parent_path = self.parent_path(parent_key)
|
54
|
-
|
96
|
+
IknowCache.cache.increment(version_path_string(parent_path))
|
55
97
|
end
|
56
98
|
|
57
99
|
# Fetch the path for this cache. We allow the parent_path to be precomputed
|
@@ -78,7 +120,7 @@ class IknowCache
|
|
78
120
|
end
|
79
121
|
|
80
122
|
def version(parent_path)
|
81
|
-
|
123
|
+
IknowCache.cache.fetch(version_path_string(parent_path), raw: true) { 1 }
|
82
124
|
end
|
83
125
|
|
84
126
|
# compute multiple paths at once: returns { key => path }
|
@@ -121,12 +163,12 @@ class IknowCache
|
|
121
163
|
version_paths = version_by_pp.values
|
122
164
|
|
123
165
|
# look up versions in cache
|
124
|
-
versions =
|
166
|
+
versions = IknowCache.cache.read_multi(*version_paths, raw: true)
|
125
167
|
|
126
168
|
version_paths.each do |vp|
|
127
169
|
next if versions.has_key?(vp)
|
128
170
|
|
129
|
-
versions[vp] =
|
171
|
+
versions[vp] = IknowCache.cache.fetch(vp, raw: true) { 1 }
|
130
172
|
end
|
131
173
|
|
132
174
|
# swap in the versions
|
@@ -153,43 +195,57 @@ class IknowCache
|
|
153
195
|
class Cache
|
154
196
|
DEBUG = false
|
155
197
|
|
156
|
-
attr_reader :name, :cache_options, :cache_group
|
198
|
+
attr_reader :name, :static_version, :cache_options, :cache_group
|
157
199
|
|
158
|
-
def initialize(cache_group, name, cache_options)
|
159
|
-
@cache_group
|
160
|
-
@name
|
161
|
-
@
|
200
|
+
def initialize(cache_group, name, static_version, cache_options)
|
201
|
+
@cache_group = cache_group
|
202
|
+
@name = name
|
203
|
+
@static_version = static_version
|
204
|
+
@cache_options = IknowCache.merge_options(cache_group.default_options, cache_options).try { |x| x.dup.freeze }
|
162
205
|
end
|
163
206
|
|
164
207
|
def fetch(key, parent_path: nil, **options, &block)
|
165
208
|
p = path(key, parent_path)
|
166
|
-
|
167
|
-
v =
|
168
|
-
|
209
|
+
IknowCache.logger.debug("Cache Fetch: #{p}") if DEBUG
|
210
|
+
v = IknowCache.cache.fetch(p, IknowCache.merge_options(cache_options, options), &block)
|
211
|
+
IknowCache.logger.debug("=> #{v.inspect}") if DEBUG
|
169
212
|
v
|
170
213
|
end
|
171
214
|
|
172
215
|
def read(key, parent_path: nil, **options)
|
173
216
|
p = path(key, parent_path)
|
174
|
-
|
175
|
-
v =
|
176
|
-
|
217
|
+
IknowCache.logger.debug("Cache Read: #{p}") if DEBUG
|
218
|
+
v = IknowCache.cache.read(p, IknowCache.merge_options(cache_options, options))
|
219
|
+
IknowCache.logger.debug("=> #{v.inspect}") if DEBUG
|
177
220
|
v
|
178
221
|
end
|
179
222
|
|
180
223
|
def write(key, value, parent_path: nil, **options)
|
181
224
|
p = path(key, parent_path)
|
182
225
|
if DEBUG
|
183
|
-
|
184
|
-
|
226
|
+
IknowCache.logger.debug("Cache Store: #{p} (#{IknowCache.merge_options(cache_options, options).inspect})")
|
227
|
+
IknowCache.logger.debug("<= #{value.inspect}")
|
185
228
|
end
|
186
|
-
|
229
|
+
IknowCache.cache.write(p, value, IknowCache.merge_options(cache_options, options))
|
187
230
|
end
|
188
231
|
|
189
232
|
def delete(key, parent_path: nil, **options)
|
190
233
|
p = path(key, parent_path)
|
191
|
-
|
192
|
-
|
234
|
+
IknowCache.logger.debug("Cache Delete: #{p}") if DEBUG
|
235
|
+
IknowCache.cache.delete(p, IknowCache.merge_options(cache_options, options))
|
236
|
+
end
|
237
|
+
|
238
|
+
def fetch_multi(keys, write_options = nil)
|
239
|
+
results = read_multi(keys)
|
240
|
+
|
241
|
+
missing_keys = keys - results.keys
|
242
|
+
if missing_keys.present?
|
243
|
+
loaded_results = yield(missing_keys)
|
244
|
+
write_multi(loaded_results, write_options)
|
245
|
+
results.merge!(loaded_results)
|
246
|
+
end
|
247
|
+
|
248
|
+
results
|
193
249
|
end
|
194
250
|
|
195
251
|
def read_multi(keys)
|
@@ -198,12 +254,10 @@ class IknowCache
|
|
198
254
|
key_paths = path_multi(keys)
|
199
255
|
path_keys = key_paths.invert
|
200
256
|
|
201
|
-
|
202
|
-
raw =
|
203
|
-
vs = raw.
|
204
|
-
|
205
|
-
end
|
206
|
-
Rails.logger.debug("=> #{vs.inspect}") if DEBUG
|
257
|
+
IknowCache.logger.debug("Cache Multi-Read: #{key_paths.values.inspect}") if DEBUG
|
258
|
+
raw = IknowCache.cache.read_multi(*key_paths.values)
|
259
|
+
vs = raw.transform_keys { |path| path_keys[path] }
|
260
|
+
IknowCache.logger.debug("=> #{vs.inspect}") if DEBUG
|
207
261
|
vs
|
208
262
|
end
|
209
263
|
|
@@ -214,12 +268,31 @@ class IknowCache
|
|
214
268
|
options = IknowCache.merge_options(cache_options, options)
|
215
269
|
|
216
270
|
entries.each do |key, value|
|
217
|
-
|
218
|
-
|
271
|
+
IknowCache.logger.debug("Cache Multi-Write: #{key_paths[key]}") if DEBUG
|
272
|
+
IknowCache.cache.write(key_paths[key], value, options)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
if Gem::Version.new(ActiveSupport::VERSION::STRING) >= Gem::Version.new('6.1')
|
277
|
+
def delete_multi(keys, **options)
|
278
|
+
return if keys.blank?
|
279
|
+
|
280
|
+
key_paths = path_multi(keys)
|
281
|
+
|
282
|
+
IknowCache.logger.debug("Cache Delete Multi: #{key_paths}") if DEBUG
|
283
|
+
IknowCache.cache.delete_multi(key_paths.values, IknowCache.merge_options(cache_options, options))
|
284
|
+
end
|
285
|
+
else
|
286
|
+
def delete_multi(keys, **options)
|
287
|
+
keys.each do |key|
|
288
|
+
delete(key, **options)
|
289
|
+
end
|
219
290
|
end
|
220
291
|
end
|
221
292
|
|
222
|
-
|
293
|
+
def key
|
294
|
+
cache_group.key
|
295
|
+
end
|
223
296
|
|
224
297
|
private
|
225
298
|
|
@@ -235,7 +308,9 @@ class IknowCache
|
|
235
308
|
end
|
236
309
|
|
237
310
|
def path_string(group_path)
|
238
|
-
"#{group_path}/#{self.name}"
|
311
|
+
path = "#{group_path}/#{self.name}"
|
312
|
+
path = "#{path}/#{self.static_version}" if static_version
|
313
|
+
path
|
239
314
|
end
|
240
315
|
end
|
241
316
|
|
@@ -249,3 +324,5 @@ class IknowCache
|
|
249
324
|
end
|
250
325
|
end
|
251
326
|
end
|
327
|
+
|
328
|
+
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.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Andreae
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
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: :
|
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.
|
101
|
+
rubygems_version: 3.1.6
|
87
102
|
signing_key:
|
88
103
|
specification_version: 4
|
89
104
|
summary: iKnow's versioned nested cache
|