redisize 0.1.5 → 0.3.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 +4 -4
- data/README.md +87 -0
- data/lib/redisize/adapter/inline.rb +8 -0
- data/lib/redisize/adapter/resque.rb +24 -0
- data/lib/redisize/adapter/sidekiq/codec.rb +274 -0
- data/lib/redisize/adapter/sidekiq.rb +20 -0
- data/lib/redisize/cache/rails.rb +32 -0
- data/lib/redisize/coder/base.rb +63 -0
- data/lib/redisize/coder/json.rb +42 -0
- data/lib/redisize/coder/message_pack.rb +80 -0
- data/lib/redisize/coder.rb +54 -0
- data/lib/redisize/log/rails.rb +21 -0
- data/lib/redisize/orm/active_record.rb +39 -0
- data/lib/redisize/support/enumerable.rb +34 -0
- data/lib/redisize/version.rb +1 -1
- data/lib/redisize.rb +406 -13
- data/redisize.gemspec +4 -3
- metadata +27 -7
data/lib/redisize.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require "redisize/version"
|
|
2
|
+
require 'redisize/coder'
|
|
2
3
|
|
|
3
4
|
module Redisize
|
|
4
5
|
# autoloading is required for the proper module loading inside the sidekiq
|
|
@@ -16,10 +17,85 @@ module Redisize
|
|
|
16
17
|
module ORM
|
|
17
18
|
autoload :ActiveRecord, File.join(__dir__, 'redisize/orm/active_record.rb')
|
|
18
19
|
end
|
|
20
|
+
module Support
|
|
21
|
+
autoload :Enumerable, File.join(__dir__, 'redisize/support/enumerable.rb')
|
|
22
|
+
end
|
|
19
23
|
|
|
20
24
|
class << self
|
|
25
|
+
# +adapter=+ directly assigns the queue backend without loading or validating it.
|
|
26
|
+
#
|
|
27
|
+
# +adapter+:: An object responding to enqueue, normally an adapter Class;
|
|
28
|
+
# nil clears the override and allows automatic detection again.
|
|
29
|
+
#
|
|
30
|
+
# Returns:: The assigned backend object or nil.
|
|
31
|
+
#
|
|
32
|
+
# Redisize.adapter = Redisize.acquire_adapter(:inline)
|
|
33
|
+
# # => Redisize::Adapter::Inline
|
|
21
34
|
attr_writer :adapter
|
|
22
35
|
|
|
36
|
+
# +json_coder+ returns the requested format, not its resolved implementation.
|
|
37
|
+
# Takes no arguments.
|
|
38
|
+
#
|
|
39
|
+
# Returns:: A Symbol: +:auto+ (the default) or +:message_pack+.
|
|
40
|
+
#
|
|
41
|
+
# Redisize.json_coder # => :auto
|
|
42
|
+
def json_coder
|
|
43
|
+
@json_coder || :auto
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# +json_coder=+ configures the format without loading a queue adapter.
|
|
47
|
+
#
|
|
48
|
+
# +value+:: A Symbol: +:auto+ selects an available coder; +:message_pack+
|
|
49
|
+
# requires MessagePack. Other values raise ArgumentError.
|
|
50
|
+
#
|
|
51
|
+
# Returns:: The assigned Symbol. Missing forced dependencies raise LoadError.
|
|
52
|
+
#
|
|
53
|
+
# Redisize.json_coder = :auto # => :auto
|
|
54
|
+
def json_coder= value
|
|
55
|
+
unless [:auto, :message_pack].include?(value)
|
|
56
|
+
raise ArgumentError, 'Redisize.json_coder must be :auto or :message_pack'
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
Coder::CODERS.each_key { |name| Coder.fetch(name).reset_availability! }
|
|
60
|
+
require_coder! :message_pack if value == :message_pack
|
|
61
|
+
@json_coder = value
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# +coder_available?+ checks the library associated with a coder name.
|
|
65
|
+
#
|
|
66
|
+
# +name+:: A Symbol: +:json+ or +:message_pack+. Unknown names return false.
|
|
67
|
+
#
|
|
68
|
+
# Returns:: +true+ when the library loads, or +false+ when unavailable.
|
|
69
|
+
#
|
|
70
|
+
# Redisize.coder_available?(:json) # => true
|
|
71
|
+
# Redisize.coder_available?(:unknown) # => false
|
|
72
|
+
def coder_available? name
|
|
73
|
+
coder = Coder.find(name)
|
|
74
|
+
coder ? coder.available? : false
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# +require_coder!+ loads a coder without depending on a queue backend.
|
|
78
|
+
#
|
|
79
|
+
# +name+:: A Symbol: +:json+ or +:message_pack+. Unknown names raise
|
|
80
|
+
# ArgumentError; unavailable libraries raise LoadError.
|
|
81
|
+
#
|
|
82
|
+
# Returns:: The loaded coder Class, not an encoded value.
|
|
83
|
+
#
|
|
84
|
+
# Redisize.require_coder!(:json) # => Redisize::Coder::Json
|
|
85
|
+
def require_coder! name
|
|
86
|
+
Coder.fetch(name).require!
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# +has_message_pack?+ reports MessagePack availability, not the selected mode.
|
|
90
|
+
# Takes no arguments.
|
|
91
|
+
#
|
|
92
|
+
# Returns:: +true+ when msgpack can be loaded, otherwise +false+.
|
|
93
|
+
#
|
|
94
|
+
# Redisize.has_message_pack? # => true, when msgpack is available
|
|
95
|
+
def has_message_pack?
|
|
96
|
+
coder_available?(:message_pack)
|
|
97
|
+
end
|
|
98
|
+
|
|
23
99
|
ADAPTERS = {
|
|
24
100
|
sidekiq: :Sidekiq, # req: Sidekiq::LimitFetch
|
|
25
101
|
resque: :Resque,
|
|
@@ -30,6 +106,10 @@ module Redisize
|
|
|
30
106
|
ActiveRecord: 'active_record'
|
|
31
107
|
}
|
|
32
108
|
|
|
109
|
+
SUPPORTS = {
|
|
110
|
+
Enumerable: 'enumerable'
|
|
111
|
+
}
|
|
112
|
+
|
|
33
113
|
LOGS = {
|
|
34
114
|
Rails: 'rails'
|
|
35
115
|
}
|
|
@@ -38,16 +118,33 @@ module Redisize
|
|
|
38
118
|
Rails: 'rails'
|
|
39
119
|
}
|
|
40
120
|
|
|
41
|
-
#
|
|
121
|
+
# +included+ installs class helpers and detected ORM, logging, cache and collection integrations.
|
|
122
|
+
#
|
|
123
|
+
# +kls+:: A Class or Module including Redisize; Rails/ActiveRecord integrations are used when detected.
|
|
124
|
+
#
|
|
125
|
+
# Returns:: The support initializer result: the extended Enumerable module when found, otherwise nil. Ruby include itself returns the including class.
|
|
126
|
+
#
|
|
127
|
+
# class Article < ActiveRecord::Base
|
|
128
|
+
# include Redisize
|
|
129
|
+
# end
|
|
130
|
+
# Article.respond_to?(:redisize_model) # => true
|
|
42
131
|
def included kls
|
|
43
132
|
kls.extend(Redisize::ClassMethods)
|
|
44
133
|
|
|
45
|
-
# init orm, log, and
|
|
134
|
+
# init orm, log, cache, and support classes
|
|
46
135
|
init_orm_for(kls)
|
|
47
136
|
init_log_for(kls)
|
|
48
137
|
init_cache_for(kls)
|
|
138
|
+
init_support_for(kls)
|
|
49
139
|
end
|
|
50
140
|
|
|
141
|
+
# +init_orm_for+ includes the ORM integration detected in the receiver ancestry.
|
|
142
|
+
#
|
|
143
|
+
# +kls+:: A Class or Module whose ancestors are checked against ORMS, currently ActiveRecord.
|
|
144
|
+
#
|
|
145
|
+
# Returns:: The including class when matched, otherwise nil.
|
|
146
|
+
#
|
|
147
|
+
# Redisize.init_orm_for(Article) # => Article (an ActiveRecord model)
|
|
51
148
|
def init_orm_for kls
|
|
52
149
|
orm = kls.ancestors.reduce(nil) { |res, anc| res || ORMS.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
|
53
150
|
|
|
@@ -57,6 +154,31 @@ module Redisize
|
|
|
57
154
|
end
|
|
58
155
|
end
|
|
59
156
|
|
|
157
|
+
# +init_support_for+ installs collection helpers on the detected support module.
|
|
158
|
+
#
|
|
159
|
+
# +kls+:: The including Class or Module; accepted for initializer compatibility but not read.
|
|
160
|
+
#
|
|
161
|
+
# Returns:: Enumerable when its integration is detected, otherwise nil. Detection examines Object constants.
|
|
162
|
+
#
|
|
163
|
+
# Redisize.init_support_for(Article) # => Enumerable
|
|
164
|
+
def init_support_for kls
|
|
165
|
+
sup = Object.constants.reduce(nil) { |res, anc| res || SUPPORTS.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
|
166
|
+
|
|
167
|
+
if sup
|
|
168
|
+
require("redisize/support/#{SUPPORTS[sup]}")
|
|
169
|
+
cls = sup.to_s.constantize
|
|
170
|
+
cls.include(Redisize::Support.const_get(sup))
|
|
171
|
+
cls.include(Redisize::ClassMethods)
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# +init_log_for+ installs the detected logging integration on Redisize and the including class.
|
|
176
|
+
#
|
|
177
|
+
# +kls+:: A Class or Module receiving the Rails helpers when Rails is detected.
|
|
178
|
+
#
|
|
179
|
+
# Returns:: The extended class when a LOGS integration is detected, otherwise nil. Detection examines Object constants.
|
|
180
|
+
#
|
|
181
|
+
# Redisize.init_log_for(Article) # => Article (with Rails loaded)
|
|
60
182
|
def init_log_for kls
|
|
61
183
|
log = Object.constants.reduce(nil) { |res, anc| res || LOGS.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
|
62
184
|
|
|
@@ -68,6 +190,13 @@ module Redisize
|
|
|
68
190
|
end
|
|
69
191
|
end
|
|
70
192
|
|
|
193
|
+
# +init_cache_for+ installs the detected cache integration on Redisize and the including class.
|
|
194
|
+
#
|
|
195
|
+
# +kls+:: A Class or Module receiving the Rails helpers when Rails is detected.
|
|
196
|
+
#
|
|
197
|
+
# Returns:: The extended class when a CACHES integration is detected, otherwise nil. Detection examines Object constants.
|
|
198
|
+
#
|
|
199
|
+
# Redisize.init_cache_for(Article) # => Article (with Rails loaded)
|
|
71
200
|
def init_cache_for kls
|
|
72
201
|
cache = Object.constants.reduce(nil) { |res, anc| res || CACHES.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
|
73
202
|
|
|
@@ -75,19 +204,42 @@ module Redisize
|
|
|
75
204
|
require("redisize/cache/#{CACHES[cache]}")
|
|
76
205
|
Redisize.include(Redisize::Cache.const_get(cache))
|
|
77
206
|
Redisize.extend(Redisize::Cache.const_get(cache))
|
|
207
|
+
Enumerable.include(Redisize::Cache.const_get(cache))
|
|
78
208
|
kls.extend(Redisize::Cache.const_get(cache))
|
|
79
209
|
end
|
|
80
210
|
end
|
|
81
211
|
|
|
212
|
+
# +adapter_kind=+ selects a queue adapter explicitly.
|
|
213
|
+
#
|
|
214
|
+
# +value+:: A Symbol: :sidekiq, :resque or :inline. Unknown values select :inline. The backend library must already be loaded.
|
|
215
|
+
#
|
|
216
|
+
# Returns:: The adapter Class when called as a method; Ruby assignment syntax returns the supplied value.
|
|
217
|
+
#
|
|
218
|
+
# Redisize.adapter_kind = :inline # => :inline
|
|
219
|
+
# Redisize.adapter # => Redisize::Adapter::Inline
|
|
82
220
|
def adapter_kind= value
|
|
83
221
|
self.adapter = acquire_adapter(ADAPTERS[value] && value || :inline)
|
|
84
222
|
end
|
|
85
223
|
|
|
224
|
+
# +acquire_adapter+ loads and resolves an adapter without assigning it.
|
|
225
|
+
#
|
|
226
|
+
# +kind+:: A Symbol or String naming an existing adapter file, normally :inline, :resque or :sidekiq.
|
|
227
|
+
#
|
|
228
|
+
# Returns:: The adapter Class. Missing files raise LoadError; missing constants raise NameError.
|
|
229
|
+
#
|
|
230
|
+
# Redisize.acquire_adapter(:inline) # => Redisize::Adapter::Inline
|
|
86
231
|
def acquire_adapter kind
|
|
87
232
|
require("redisize/adapter/#{kind}")
|
|
88
233
|
Redisize::Adapter.const_get(kind.capitalize)
|
|
89
234
|
end
|
|
90
235
|
|
|
236
|
+
# +adapter+ returns the assigned adapter or detects one from loaded top-level constants.
|
|
237
|
+
# Takes no arguments.
|
|
238
|
+
#
|
|
239
|
+
# Returns:: An adapter Class, or nil when no detection entry matches. Explicitly select :inline when automatic detection is unsuitable.
|
|
240
|
+
#
|
|
241
|
+
# Redisize.adapter_kind = :inline
|
|
242
|
+
# Redisize.adapter # => Redisize::Adapter::Inline
|
|
91
243
|
def adapter
|
|
92
244
|
@adapter ||=
|
|
93
245
|
ADAPTERS.reduce(nil) do |res, (kind, prc)|
|
|
@@ -95,14 +247,41 @@ module Redisize
|
|
|
95
247
|
end
|
|
96
248
|
end
|
|
97
249
|
|
|
250
|
+
# +enqueue+ dispatches a Redisize command through the selected adapter.
|
|
251
|
+
#
|
|
252
|
+
# +method+:: A Symbol or String naming a Redisize singleton method; converted to String.
|
|
253
|
+
# +args+:: Zero or more command arguments; asynchronous backends require serializable values.
|
|
254
|
+
#
|
|
255
|
+
# Returns:: The adapter result: the command result inline, a Sidekiq job ID/nil, or the Resque enqueue result.
|
|
256
|
+
#
|
|
257
|
+
# Redisize.adapter_kind = :inline
|
|
258
|
+
# Redisize.enqueue(:rekey, ['json', 'Article', 'id', '42'])
|
|
259
|
+
# # => ['meta', 'Article', 'id', '42']
|
|
98
260
|
def enqueue method, *args
|
|
99
261
|
adapter.enqueue(method.to_s, *args)
|
|
100
262
|
end
|
|
101
263
|
|
|
264
|
+
# +filtered_for+ keeps attribute names recognized by a model.
|
|
265
|
+
#
|
|
266
|
+
# +attrs+:: An Array of attribute names; matching uses the same type as attribute_types keys, normally String.
|
|
267
|
+
# +klass+:: A model Class exposing attribute_types.
|
|
268
|
+
#
|
|
269
|
+
# Returns:: An Array of the recognized names, preserving their input order.
|
|
270
|
+
#
|
|
271
|
+
# Redisize.filtered_for(['id', 'unknown'], Article) # => ['id']
|
|
102
272
|
def filtered_for attrs, klass
|
|
103
273
|
filtered = attrs.select {|attr| klass.attribute_types.keys.include?(attr) }
|
|
104
274
|
end
|
|
105
275
|
|
|
276
|
+
# +assign_reverse_key+ registers a cache key in a reverse dependency list for one day.
|
|
277
|
+
#
|
|
278
|
+
# +key+:: A Rails cache key, normally an Array beginning with meta.
|
|
279
|
+
# +host_key+:: The dependent cache key to add; existing entries are deduplicated with Array union.
|
|
280
|
+
#
|
|
281
|
+
# Returns:: The debug logger result, backend-dependent (for example true with a standard Logger); not the cached Array.
|
|
282
|
+
#
|
|
283
|
+
# Redisize.assign_reverse_key(['meta', 'Article'], ['sql', 'Article', 'id', 'SELECT * FROM articles'])
|
|
284
|
+
# # => true (with a standard enabled Rails logger)
|
|
106
285
|
def assign_reverse_key key, host_key
|
|
107
286
|
# binding.pry
|
|
108
287
|
if value = redisize_cache_read(key)
|
|
@@ -114,6 +293,13 @@ module Redisize
|
|
|
114
293
|
end
|
|
115
294
|
end
|
|
116
295
|
|
|
296
|
+
# +drop_key+ deletes a cache entry and recursively deletes dependencies of meta entries.
|
|
297
|
+
#
|
|
298
|
+
# +key+:: A cache-key Array; a first element of "meta" enables recursive dependency removal.
|
|
299
|
+
#
|
|
300
|
+
# Returns:: The cache delete result when the entry exists (usually true/false), or nil when its cached value is falsey.
|
|
301
|
+
#
|
|
302
|
+
# Redisize.drop_key(['meta', 'MissingModel']) # => nil (uncached key)
|
|
117
303
|
def drop_key key
|
|
118
304
|
# binding.pry
|
|
119
305
|
if value = redisize_cache_read(key)
|
|
@@ -128,15 +314,41 @@ module Redisize
|
|
|
128
314
|
end
|
|
129
315
|
end
|
|
130
316
|
|
|
317
|
+
# +rekey+ replaces the first component of a structured cache key.
|
|
318
|
+
#
|
|
319
|
+
# +key+:: A nonempty Array; remaining components are preserved.
|
|
320
|
+
# +type+:: A String or Symbol converted to String; defaults to "meta".
|
|
321
|
+
#
|
|
322
|
+
# Returns:: A new Array containing the new type followed by the original tail.
|
|
323
|
+
#
|
|
324
|
+
# Redisize.rekey(['json', 'Article', 'id', '42'], :instance)
|
|
325
|
+
# # => ['instance', 'Article', 'id', '42']
|
|
131
326
|
def rekey key, type = "meta"
|
|
132
327
|
[type.to_s] + key[1..-1]
|
|
133
328
|
end
|
|
134
329
|
|
|
330
|
+
# +key_name_for+ builds a model cache key from serialized attributes.
|
|
331
|
+
#
|
|
332
|
+
# +model_name+:: A constantizable model-name String exposing primary_key.
|
|
333
|
+
# +attrs+:: A Hash indexed by the model primary-key name; its first key is a fallback when primary_key is nil.
|
|
334
|
+
# +type+:: The key prefix, normally "meta", "instance" or "json"; defaults to "meta" and is preserved as supplied.
|
|
335
|
+
#
|
|
336
|
+
# Returns:: A four-element Array; the attribute value is converted to String, including nil as an empty String.
|
|
337
|
+
#
|
|
338
|
+
# Redisize.key_name_for('Article', {'id' => 42}) # => ['meta', 'Article', 'id', '42']
|
|
135
339
|
def key_name_for model_name, attrs, type = "meta"
|
|
136
340
|
primary_key = model_name.constantize.primary_key || attrs.keys.first
|
|
137
341
|
[type, model_name, primary_key, attrs[primary_key].to_s]
|
|
138
342
|
end
|
|
139
343
|
|
|
344
|
+
# +parse_sql_key+ registers dependencies on model tables found after SQL FROM/JOIN tokens.
|
|
345
|
+
#
|
|
346
|
+
# +key+:: A structured cache-key Array whose last component is an SQL String. This is a simple token parser, not a general SQL parser.
|
|
347
|
+
#
|
|
348
|
+
# Returns:: An Array of resolved model Classes; unresolvable table names are omitted.
|
|
349
|
+
#
|
|
350
|
+
# Redisize.parse_sql_key(['sql', 'Article', 'id', 'SELECT * FROM articles'])
|
|
351
|
+
# # => [Article] (when the model is defined)
|
|
140
352
|
def parse_sql_key key
|
|
141
353
|
key[-1].split(/\s(join|from)\s/i)[1..-1].map do |part|
|
|
142
354
|
part.strip.split(/[\s\"]/).reject {|x| x.blank? }.first
|
|
@@ -147,6 +359,16 @@ module Redisize
|
|
|
147
359
|
end
|
|
148
360
|
end
|
|
149
361
|
|
|
362
|
+
# +parse_instance_attrs+ recursively registers dependencies of nested serialized associations.
|
|
363
|
+
#
|
|
364
|
+
# +model_name+:: A constantizable model-name String exposing reflections.
|
|
365
|
+
# +attrs+:: A Hash of serialized attributes; Hash/Array values are treated as association data.
|
|
366
|
+
# +key+:: The dependent cache-key Array registered against nested models.
|
|
367
|
+
#
|
|
368
|
+
# Returns:: An Array of attribute names whose values are Hashes or Arrays; registration results are not returned.
|
|
369
|
+
#
|
|
370
|
+
# Redisize.parse_instance_attrs('Article', {'id' => 42}, ['json', 'Article', 'id', '42', 'short'])
|
|
371
|
+
# # => []
|
|
150
372
|
def parse_instance_attrs model_name, attrs, key
|
|
151
373
|
model = model_name.constantize
|
|
152
374
|
children =
|
|
@@ -175,8 +397,17 @@ module Redisize
|
|
|
175
397
|
end
|
|
176
398
|
end
|
|
177
399
|
|
|
178
|
-
|
|
400
|
+
# +redisize_model_metas+ invalidates previous model dependencies and registers a fetched record.
|
|
401
|
+
#
|
|
402
|
+
# +metakey+:: A model-specific meta-key Array.
|
|
403
|
+
# +model_name+:: A constantizable model-name String.
|
|
404
|
+
# +attrs+:: A Hash of serialized model attributes and nested associations.
|
|
405
|
+
# +key+:: The cached record key to register.
|
|
179
406
|
#
|
|
407
|
+
# Returns:: The final reverse-key debug logger result (for example true with a standard Logger).
|
|
408
|
+
#
|
|
409
|
+
# Redisize.redisize_model_metas(['meta', 'Article', 'id', '42'], 'Article', {'id' => 42}, ['instance', 'Article', 'id', '42'])
|
|
410
|
+
# # => true (with a standard enabled Rails logger)
|
|
180
411
|
def redisize_model_metas metakey, model_name, attrs, key
|
|
181
412
|
# binding.pry
|
|
182
413
|
drop_key(metakey)
|
|
@@ -185,9 +416,16 @@ module Redisize
|
|
|
185
416
|
assign_reverse_key(metakey, key)
|
|
186
417
|
end
|
|
187
418
|
|
|
188
|
-
# +
|
|
419
|
+
# +redisize_array_metas+ registers record and SQL dependencies for a cached result array.
|
|
420
|
+
#
|
|
421
|
+
# +key+:: A structured Array with model name at index 1, primary-key name at index 2 and SQL as its last component.
|
|
422
|
+
# +attres+:: An Array of serialized attribute Hashes keyed by the primary-key name.
|
|
189
423
|
#
|
|
190
|
-
|
|
424
|
+
# Returns:: The Array of model Classes returned by parse_sql_key, not the supplied records.
|
|
425
|
+
#
|
|
426
|
+
# Redisize.redisize_array_metas(['sql', 'Article', 'id', 'SELECT * FROM articles'], [{'id' => 42}])
|
|
427
|
+
# # => [Article]
|
|
428
|
+
def redisize_array_metas key, attres
|
|
191
429
|
model_name = key[1]
|
|
192
430
|
primary_key = key[2]
|
|
193
431
|
|
|
@@ -202,6 +440,14 @@ module Redisize
|
|
|
202
440
|
parse_sql_key(key)
|
|
203
441
|
end
|
|
204
442
|
|
|
443
|
+
# +deredisize_instance_metas+ drops a record dependency tree and deletes the record cache.
|
|
444
|
+
#
|
|
445
|
+
# +key+:: The instance cache-key Array; its prefix is replaced by "meta" for dependency lookup.
|
|
446
|
+
#
|
|
447
|
+
# Returns:: The final Rails cache delete result, normally true or false.
|
|
448
|
+
#
|
|
449
|
+
# Redisize.deredisize_instance_metas(['instance', 'Article', 'id', '42'])
|
|
450
|
+
# # => false (when the key is absent from a MemoryStore)
|
|
205
451
|
def deredisize_instance_metas key
|
|
206
452
|
metakey = rekey(key)
|
|
207
453
|
|
|
@@ -210,6 +456,14 @@ module Redisize
|
|
|
210
456
|
redisize_cache_delete(key)
|
|
211
457
|
end
|
|
212
458
|
|
|
459
|
+
# +reredisize_instance_metas+ invalidates dependents and registers a refreshed record cache.
|
|
460
|
+
#
|
|
461
|
+
# +key+:: The instance cache-key Array; its meta counterpart is derived with rekey.
|
|
462
|
+
#
|
|
463
|
+
# Returns:: The reverse-key debug logger result, backend-dependent.
|
|
464
|
+
#
|
|
465
|
+
# Redisize.reredisize_instance_metas(['instance', 'Article', 'id', '42'])
|
|
466
|
+
# # => true (with a standard enabled Rails logger)
|
|
213
467
|
def reredisize_instance_metas key
|
|
214
468
|
metakey = rekey(key)
|
|
215
469
|
# binding.pry
|
|
@@ -218,16 +472,40 @@ module Redisize
|
|
|
218
472
|
assign_reverse_key(metakey, key)
|
|
219
473
|
end
|
|
220
474
|
|
|
475
|
+
# +deredisize_model_metas+ invalidates the dependency tree for a model.
|
|
476
|
+
#
|
|
477
|
+
# +model_name+:: A model-name String, normally the polymorphic base name.
|
|
478
|
+
#
|
|
479
|
+
# Returns:: The drop_key result: a cache delete result, or nil when the meta key is absent.
|
|
480
|
+
#
|
|
481
|
+
# Redisize.deredisize_model_metas('Article') # => nil (without cached dependencies)
|
|
221
482
|
def deredisize_model_metas model_name
|
|
222
483
|
# binding.pry
|
|
223
484
|
drop_key(["meta", model_name])
|
|
224
485
|
end
|
|
225
486
|
|
|
487
|
+
# +deredisize_json_metas+ invalidates a cached JSON representation.
|
|
488
|
+
#
|
|
489
|
+
# +key+:: The complete JSON cache-key Array.
|
|
490
|
+
#
|
|
491
|
+
# Returns:: The drop_key result: a cache delete result, or nil for an absent key.
|
|
492
|
+
#
|
|
493
|
+
# Redisize.deredisize_json_metas(['json', 'Article', 'id', '42', 'short'])
|
|
494
|
+
# # => nil (when uncached)
|
|
226
495
|
def deredisize_json_metas key
|
|
227
496
|
# binding.pry
|
|
228
497
|
drop_key(key)
|
|
229
498
|
end
|
|
230
499
|
|
|
500
|
+
# +redisize_json_metas+ registers record and nested association dependencies for cached JSON.
|
|
501
|
+
#
|
|
502
|
+
# +key+:: A JSON cache-key Array containing the model name at index 1.
|
|
503
|
+
# +attrs+:: A serialized attribute Hash; primary-key lookup follows the model definition.
|
|
504
|
+
#
|
|
505
|
+
# Returns:: The reverse-key debug logger result, backend-dependent.
|
|
506
|
+
#
|
|
507
|
+
# Redisize.redisize_json_metas(['json', 'Article', 'id', '42', 'short'], {'id' => 42})
|
|
508
|
+
# # => true (with a standard enabled Rails logger)
|
|
231
509
|
def redisize_json_metas key, attrs
|
|
232
510
|
metakey = key_name_for(key[1], attrs)
|
|
233
511
|
|
|
@@ -237,7 +515,14 @@ module Redisize
|
|
|
237
515
|
end
|
|
238
516
|
end
|
|
239
517
|
|
|
240
|
-
#
|
|
518
|
+
# +redisize_json+ fetches a one-week cached representation of this record.
|
|
519
|
+
#
|
|
520
|
+
# +scheme+:: A cache-key component identifying the representation, commonly a String or Symbol.
|
|
521
|
+
# +block+:: Required on a cache miss; produces serialized data, normally an attribute Hash. Dependencies are enqueued on a miss only.
|
|
522
|
+
#
|
|
523
|
+
# Returns:: The cached value, or the block result on a miss; this method does not itself encode a JSON String.
|
|
524
|
+
#
|
|
525
|
+
# article.redisize_json('short') { {'id' => article.id} } # => {'id' => 42}
|
|
241
526
|
def redisize_json scheme, &block
|
|
242
527
|
primary_key = self.class.primary_key
|
|
243
528
|
key = ["json", self.class.polymorphic_base_name, primary_key, self[primary_key].to_s, scheme]
|
|
@@ -252,7 +537,14 @@ module Redisize
|
|
|
252
537
|
end
|
|
253
538
|
end
|
|
254
539
|
|
|
255
|
-
#
|
|
540
|
+
# +deredisize_json+ enqueues invalidation of one representation of this record.
|
|
541
|
+
#
|
|
542
|
+
# +scheme+:: The representation component used in its JSON cache key.
|
|
543
|
+
# +block+:: Optional; accepted but not used.
|
|
544
|
+
#
|
|
545
|
+
# Returns:: The queue adapter result, such as a Sidekiq job ID or an inline deletion result. The primary-key component uses its original type here.
|
|
546
|
+
#
|
|
547
|
+
# article.deredisize_json('short') # => '0123456789abcdef01234567' (example Sidekiq job ID)
|
|
256
548
|
def deredisize_json scheme, &block
|
|
257
549
|
primary_key = self.class.primary_key
|
|
258
550
|
key = ["json", self.class.polymorphic_base_name, primary_key, self[primary_key], scheme]
|
|
@@ -261,12 +553,22 @@ module Redisize
|
|
|
261
553
|
Redisize.enqueue(:deredisize_json_metas, key)
|
|
262
554
|
end
|
|
263
555
|
|
|
264
|
-
#
|
|
556
|
+
# +deredisize_model+ enqueues invalidation of all dependencies for this record model.
|
|
557
|
+
# Takes no arguments.
|
|
558
|
+
#
|
|
559
|
+
# Returns:: The queue adapter result, such as a Sidekiq job ID; inline results depend on the invoked command.
|
|
560
|
+
#
|
|
561
|
+
# article.deredisize_model # => '0123456789abcdef01234567' (example Sidekiq job ID)
|
|
265
562
|
def deredisize_model
|
|
266
563
|
Redisize.enqueue(:deredisize_model_metas, self.class.polymorphic_base_name)
|
|
267
564
|
end
|
|
268
565
|
|
|
269
|
-
#
|
|
566
|
+
# +reredisize_instance+ stores this record for 1000 years and enqueues dependency refresh.
|
|
567
|
+
# Takes no arguments.
|
|
568
|
+
#
|
|
569
|
+
# Returns:: The queue adapter result, such as a Sidekiq job ID; inline results depend on the invoked command.
|
|
570
|
+
#
|
|
571
|
+
# article.reredisize_instance # => '0123456789abcdef01234567' (example Sidekiq job ID)
|
|
270
572
|
def reredisize_instance
|
|
271
573
|
attrs = self.as_json
|
|
272
574
|
key = Redisize.key_name_for(self.class.polymorphic_base_name, attrs, "instance")
|
|
@@ -276,7 +578,12 @@ module Redisize
|
|
|
276
578
|
Redisize.enqueue(:reredisize_instance_metas, key)
|
|
277
579
|
end
|
|
278
580
|
|
|
279
|
-
#
|
|
581
|
+
# +deredisize_instance+ enqueues invalidation of this record and its dependent entries.
|
|
582
|
+
# Takes no arguments.
|
|
583
|
+
#
|
|
584
|
+
# Returns:: The queue adapter result, such as a Sidekiq job ID; inline results depend on the invoked command.
|
|
585
|
+
#
|
|
586
|
+
# article.deredisize_instance # => '0123456789abcdef01234567' (example Sidekiq job ID)
|
|
280
587
|
def deredisize_instance
|
|
281
588
|
attrs = self.as_json
|
|
282
589
|
key = Redisize.key_name_for(self.class.polymorphic_base_name, attrs, "instance")
|
|
@@ -286,6 +593,12 @@ module Redisize
|
|
|
286
593
|
end
|
|
287
594
|
|
|
288
595
|
module ClassMethods
|
|
596
|
+
# +polymorphic_base_name+ builds a namespaced cache model name using ActiveRecord polymorphic naming.
|
|
597
|
+
# Takes no arguments.
|
|
598
|
+
#
|
|
599
|
+
# Returns:: A String combining the base-class namespace with polymorphic_name when needed.
|
|
600
|
+
#
|
|
601
|
+
# Article.polymorphic_base_name # => 'Article'
|
|
289
602
|
def polymorphic_base_name
|
|
290
603
|
base_name = base_class.to_s
|
|
291
604
|
|
|
@@ -296,21 +609,101 @@ module Redisize
|
|
|
296
609
|
end
|
|
297
610
|
end
|
|
298
611
|
|
|
299
|
-
#
|
|
612
|
+
# +primary_key_with+ chooses an explicit primary key or infers it from the receiver.
|
|
613
|
+
#
|
|
614
|
+
# +key_name_in+:: An optional primary-key String or Symbol; any truthy value is returned unchanged. Defaults to nil.
|
|
615
|
+
#
|
|
616
|
+
# Returns:: The supplied key or inferred model primary key, normally a String; nil when no applicable branch finds a key.
|
|
617
|
+
#
|
|
618
|
+
# Article.primary_key_with(:slug) # => :slug
|
|
619
|
+
# Article.primary_key_with # => 'id'
|
|
620
|
+
def primary_key_with key_name_in = nil
|
|
621
|
+
key_name_in || self.respond_to?(:primary_key) && self.primary_key || case self
|
|
622
|
+
when ActiveRecord
|
|
623
|
+
self.map.first&.class&.primary_key
|
|
624
|
+
when ActiveRecord::Scoping, ActiveRecord::Reflection, ActiveRecord::Associations
|
|
625
|
+
self.values.map.first&.class&.primary_key
|
|
626
|
+
end
|
|
627
|
+
end
|
|
628
|
+
|
|
629
|
+
# +redisize_sql+ caches a block result for the model all-scope SQL.
|
|
630
|
+
#
|
|
631
|
+
# +block+:: Required on a cache miss; produces the cached value. An Array result schedules dependency discovery.
|
|
632
|
+
#
|
|
633
|
+
# Returns:: The cached value or block result, with a one-day expiry via redisize_by_key.
|
|
634
|
+
#
|
|
635
|
+
# Article.redisize_sql { Article.all.as_json } # => [{'id' => 42}] (example record set)
|
|
300
636
|
def redisize_sql &block
|
|
301
637
|
key = ["sql", self.name, self.primary_key, self.all.to_sql]
|
|
302
638
|
|
|
639
|
+
redisize_by_key(key, &block)
|
|
640
|
+
end
|
|
641
|
+
|
|
642
|
+
# +redisize_hash+ caches a block result under identifiers derived from Hash values.
|
|
643
|
+
#
|
|
644
|
+
# +key_name_in+:: An optional primary-key String/Symbol; nil asks primary_key_with to infer a key.
|
|
645
|
+
# +block+:: Required on a miss; returns the value to cache for one day.
|
|
646
|
+
#
|
|
647
|
+
# Returns:: The cached value or block result. The generated key starts with "by_key" and includes value identifiers in iteration order.
|
|
648
|
+
#
|
|
649
|
+
# records_by_id.redisize_hash(:id) { [{'id' => 42}] } # => [{'id' => 42}]
|
|
650
|
+
# # Receiver must expose Redisize::ClassMethods and cache helpers.
|
|
651
|
+
def redisize_hash key_name_in = nil, &block
|
|
652
|
+
key_name = primary_key_with(key_name_in)
|
|
653
|
+
|
|
654
|
+
key = ["by_key", *self.values.map do |x|
|
|
655
|
+
key_name && x.respond_to?(key_name) ? x.send(key_name).to_s : x.to_s
|
|
656
|
+
end ]
|
|
657
|
+
|
|
658
|
+
redisize_by_key(key, &block)
|
|
659
|
+
end
|
|
660
|
+
|
|
661
|
+
# +redisize_array+ caches a block result under identifiers derived from Array elements.
|
|
662
|
+
#
|
|
663
|
+
# +key_name_in+:: An optional primary-key String/Symbol; nil asks primary_key_with to infer a key.
|
|
664
|
+
# +block+:: Required on a miss; returns the value to cache for one day.
|
|
665
|
+
#
|
|
666
|
+
# Returns:: The cached value or block result. The generated key starts with "by_key" and includes value identifiers in iteration order.
|
|
667
|
+
#
|
|
668
|
+
# records.redisize_array(:id) { [{'id' => 42}] } # => [{'id' => 42}]
|
|
669
|
+
# # Receiver must expose Redisize::ClassMethods and cache helpers.
|
|
670
|
+
def redisize_array key_name_in = nil, &block
|
|
671
|
+
key_name = primary_key_with(key_name_in)
|
|
672
|
+
|
|
673
|
+
key = ["by_key", *self.map { |x| key_name ? x.send(key_name).to_s : x.to_s } ]
|
|
674
|
+
|
|
675
|
+
redisize_by_key(key, &block)
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
# +redisize_by_key+ fetches a one-day cached value and schedules dependencies for Array results.
|
|
679
|
+
#
|
|
680
|
+
# +key+:: Any Rails-supported cache key; Array results additionally require the structured model/SQL key expected by redisize_array_metas.
|
|
681
|
+
# +block+:: Required on a cache miss; returns the value to cache.
|
|
682
|
+
#
|
|
683
|
+
# Returns:: The cached value or block result.
|
|
684
|
+
#
|
|
685
|
+
# Article.redisize_by_key('article-count') { 3 } # => 3
|
|
686
|
+
def redisize_by_key key, &block
|
|
303
687
|
# binding.pry
|
|
304
688
|
redisize_cache_fetch(key, expires_in: 1.day) do
|
|
305
689
|
value = block.call
|
|
306
690
|
|
|
307
|
-
Redisize.enqueue(:
|
|
691
|
+
Redisize.enqueue(:redisize_array_metas, key, value) if value.is_a?(Array)
|
|
308
692
|
|
|
309
693
|
value
|
|
310
694
|
end
|
|
311
695
|
end
|
|
312
696
|
|
|
313
|
-
#
|
|
697
|
+
# +redisize_model+ fetches one model record from the cache for one week.
|
|
698
|
+
#
|
|
699
|
+
# +value+:: The lookup value used unchanged in the cache and meta keys.
|
|
700
|
+
# +options+:: A Hash; :by_key selects an alternate primary-key name (String or Symbol), otherwise the model primary_key is used.
|
|
701
|
+
# +block+:: Required on a miss; returns a record exposing as_json, or nil. Only truthy results schedule dependency registration.
|
|
702
|
+
#
|
|
703
|
+
# Returns:: The cached record, the fetched record, or nil when the block finds none.
|
|
704
|
+
#
|
|
705
|
+
# Article.redisize_model('news', by_key: :slug) { Article.find_by(slug: 'news') }
|
|
706
|
+
# # => nil (when no record exists)
|
|
314
707
|
def redisize_model value, options = {}, &block
|
|
315
708
|
primary_key = options.fetch(:by_key, self.primary_key).to_s
|
|
316
709
|
key = ["instance", name, primary_key, value]
|
data/redisize.gemspec
CHANGED
|
@@ -8,15 +8,15 @@ Gem::Specification.new do |spec|
|
|
|
8
8
|
|
|
9
9
|
spec.summary = %q{Make json record cacheable to redis}
|
|
10
10
|
spec.description = %q{Make json record cacheable to redis via various adapters like Resque, Sidekiq, etc}
|
|
11
|
-
spec.homepage = "https://
|
|
11
|
+
spec.homepage = "https://codeberg.org/majioa/redisize"
|
|
12
12
|
spec.license = "MIT"
|
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
14
14
|
|
|
15
15
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
16
16
|
|
|
17
17
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
|
-
spec.metadata["source_code_uri"] = "https://
|
|
19
|
-
spec.metadata["changelog_uri"] = "https://
|
|
18
|
+
spec.metadata["source_code_uri"] = "https://codeberg.org/majioa/redisize"
|
|
19
|
+
spec.metadata["changelog_uri"] = "https://codeberg.org/majioa/redisize/CHANGELOG.md"
|
|
20
20
|
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
|
22
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
@@ -26,5 +26,6 @@ Gem::Specification.new do |spec|
|
|
|
26
26
|
spec.bindir = "exe"
|
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
28
|
spec.require_paths = ["lib"]
|
|
29
|
+
spec.add_runtime_dependency('base64')
|
|
29
30
|
spec.add_development_dependency('pry')
|
|
30
31
|
end
|