redisize 0.2.0 → 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 +14 -0
- data/lib/redisize/version.rb +1 -1
- data/lib/redisize.rb +353 -13
- data/redisize.gemspec +1 -0
- metadata +22 -3
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
|
|
@@ -21,8 +22,80 @@ module Redisize
|
|
|
21
22
|
end
|
|
22
23
|
|
|
23
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
|
|
24
34
|
attr_writer :adapter
|
|
25
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
|
+
|
|
26
99
|
ADAPTERS = {
|
|
27
100
|
sidekiq: :Sidekiq, # req: Sidekiq::LimitFetch
|
|
28
101
|
resque: :Resque,
|
|
@@ -45,7 +118,16 @@ module Redisize
|
|
|
45
118
|
Rails: 'rails'
|
|
46
119
|
}
|
|
47
120
|
|
|
48
|
-
#
|
|
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
|
|
49
131
|
def included kls
|
|
50
132
|
kls.extend(Redisize::ClassMethods)
|
|
51
133
|
|
|
@@ -56,6 +138,13 @@ module Redisize
|
|
|
56
138
|
init_support_for(kls)
|
|
57
139
|
end
|
|
58
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)
|
|
59
148
|
def init_orm_for kls
|
|
60
149
|
orm = kls.ancestors.reduce(nil) { |res, anc| res || ORMS.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
|
61
150
|
|
|
@@ -65,6 +154,13 @@ module Redisize
|
|
|
65
154
|
end
|
|
66
155
|
end
|
|
67
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
|
|
68
164
|
def init_support_for kls
|
|
69
165
|
sup = Object.constants.reduce(nil) { |res, anc| res || SUPPORTS.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
|
70
166
|
|
|
@@ -76,6 +172,13 @@ module Redisize
|
|
|
76
172
|
end
|
|
77
173
|
end
|
|
78
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)
|
|
79
182
|
def init_log_for kls
|
|
80
183
|
log = Object.constants.reduce(nil) { |res, anc| res || LOGS.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
|
81
184
|
|
|
@@ -87,6 +190,13 @@ module Redisize
|
|
|
87
190
|
end
|
|
88
191
|
end
|
|
89
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)
|
|
90
200
|
def init_cache_for kls
|
|
91
201
|
cache = Object.constants.reduce(nil) { |res, anc| res || CACHES.keys.find {|re| /#{re}/ =~ anc.to_s } }
|
|
92
202
|
|
|
@@ -99,15 +209,37 @@ module Redisize
|
|
|
99
209
|
end
|
|
100
210
|
end
|
|
101
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
|
|
102
220
|
def adapter_kind= value
|
|
103
221
|
self.adapter = acquire_adapter(ADAPTERS[value] && value || :inline)
|
|
104
222
|
end
|
|
105
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
|
|
106
231
|
def acquire_adapter kind
|
|
107
232
|
require("redisize/adapter/#{kind}")
|
|
108
233
|
Redisize::Adapter.const_get(kind.capitalize)
|
|
109
234
|
end
|
|
110
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
|
|
111
243
|
def adapter
|
|
112
244
|
@adapter ||=
|
|
113
245
|
ADAPTERS.reduce(nil) do |res, (kind, prc)|
|
|
@@ -115,14 +247,41 @@ module Redisize
|
|
|
115
247
|
end
|
|
116
248
|
end
|
|
117
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']
|
|
118
260
|
def enqueue method, *args
|
|
119
261
|
adapter.enqueue(method.to_s, *args)
|
|
120
262
|
end
|
|
121
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']
|
|
122
272
|
def filtered_for attrs, klass
|
|
123
273
|
filtered = attrs.select {|attr| klass.attribute_types.keys.include?(attr) }
|
|
124
274
|
end
|
|
125
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)
|
|
126
285
|
def assign_reverse_key key, host_key
|
|
127
286
|
# binding.pry
|
|
128
287
|
if value = redisize_cache_read(key)
|
|
@@ -134,6 +293,13 @@ module Redisize
|
|
|
134
293
|
end
|
|
135
294
|
end
|
|
136
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)
|
|
137
303
|
def drop_key key
|
|
138
304
|
# binding.pry
|
|
139
305
|
if value = redisize_cache_read(key)
|
|
@@ -148,15 +314,41 @@ module Redisize
|
|
|
148
314
|
end
|
|
149
315
|
end
|
|
150
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']
|
|
151
326
|
def rekey key, type = "meta"
|
|
152
327
|
[type.to_s] + key[1..-1]
|
|
153
328
|
end
|
|
154
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']
|
|
155
339
|
def key_name_for model_name, attrs, type = "meta"
|
|
156
340
|
primary_key = model_name.constantize.primary_key || attrs.keys.first
|
|
157
341
|
[type, model_name, primary_key, attrs[primary_key].to_s]
|
|
158
342
|
end
|
|
159
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)
|
|
160
352
|
def parse_sql_key key
|
|
161
353
|
key[-1].split(/\s(join|from)\s/i)[1..-1].map do |part|
|
|
162
354
|
part.strip.split(/[\s\"]/).reject {|x| x.blank? }.first
|
|
@@ -167,6 +359,16 @@ module Redisize
|
|
|
167
359
|
end
|
|
168
360
|
end
|
|
169
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
|
+
# # => []
|
|
170
372
|
def parse_instance_attrs model_name, attrs, key
|
|
171
373
|
model = model_name.constantize
|
|
172
374
|
children =
|
|
@@ -195,8 +397,17 @@ module Redisize
|
|
|
195
397
|
end
|
|
196
398
|
end
|
|
197
399
|
|
|
198
|
-
|
|
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.
|
|
199
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)
|
|
200
411
|
def redisize_model_metas metakey, model_name, attrs, key
|
|
201
412
|
# binding.pry
|
|
202
413
|
drop_key(metakey)
|
|
@@ -205,8 +416,15 @@ module Redisize
|
|
|
205
416
|
assign_reverse_key(metakey, key)
|
|
206
417
|
end
|
|
207
418
|
|
|
208
|
-
# +redisize_array_metas+
|
|
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.
|
|
423
|
+
#
|
|
424
|
+
# Returns:: The Array of model Classes returned by parse_sql_key, not the supplied records.
|
|
209
425
|
#
|
|
426
|
+
# Redisize.redisize_array_metas(['sql', 'Article', 'id', 'SELECT * FROM articles'], [{'id' => 42}])
|
|
427
|
+
# # => [Article]
|
|
210
428
|
def redisize_array_metas key, attres
|
|
211
429
|
model_name = key[1]
|
|
212
430
|
primary_key = key[2]
|
|
@@ -222,6 +440,14 @@ module Redisize
|
|
|
222
440
|
parse_sql_key(key)
|
|
223
441
|
end
|
|
224
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)
|
|
225
451
|
def deredisize_instance_metas key
|
|
226
452
|
metakey = rekey(key)
|
|
227
453
|
|
|
@@ -230,6 +456,14 @@ module Redisize
|
|
|
230
456
|
redisize_cache_delete(key)
|
|
231
457
|
end
|
|
232
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)
|
|
233
467
|
def reredisize_instance_metas key
|
|
234
468
|
metakey = rekey(key)
|
|
235
469
|
# binding.pry
|
|
@@ -238,16 +472,40 @@ module Redisize
|
|
|
238
472
|
assign_reverse_key(metakey, key)
|
|
239
473
|
end
|
|
240
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)
|
|
241
482
|
def deredisize_model_metas model_name
|
|
242
483
|
# binding.pry
|
|
243
484
|
drop_key(["meta", model_name])
|
|
244
485
|
end
|
|
245
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)
|
|
246
495
|
def deredisize_json_metas key
|
|
247
496
|
# binding.pry
|
|
248
497
|
drop_key(key)
|
|
249
498
|
end
|
|
250
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)
|
|
251
509
|
def redisize_json_metas key, attrs
|
|
252
510
|
metakey = key_name_for(key[1], attrs)
|
|
253
511
|
|
|
@@ -257,7 +515,14 @@ module Redisize
|
|
|
257
515
|
end
|
|
258
516
|
end
|
|
259
517
|
|
|
260
|
-
#
|
|
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}
|
|
261
526
|
def redisize_json scheme, &block
|
|
262
527
|
primary_key = self.class.primary_key
|
|
263
528
|
key = ["json", self.class.polymorphic_base_name, primary_key, self[primary_key].to_s, scheme]
|
|
@@ -272,7 +537,14 @@ module Redisize
|
|
|
272
537
|
end
|
|
273
538
|
end
|
|
274
539
|
|
|
275
|
-
#
|
|
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)
|
|
276
548
|
def deredisize_json scheme, &block
|
|
277
549
|
primary_key = self.class.primary_key
|
|
278
550
|
key = ["json", self.class.polymorphic_base_name, primary_key, self[primary_key], scheme]
|
|
@@ -281,12 +553,22 @@ module Redisize
|
|
|
281
553
|
Redisize.enqueue(:deredisize_json_metas, key)
|
|
282
554
|
end
|
|
283
555
|
|
|
284
|
-
#
|
|
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)
|
|
285
562
|
def deredisize_model
|
|
286
563
|
Redisize.enqueue(:deredisize_model_metas, self.class.polymorphic_base_name)
|
|
287
564
|
end
|
|
288
565
|
|
|
289
|
-
#
|
|
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)
|
|
290
572
|
def reredisize_instance
|
|
291
573
|
attrs = self.as_json
|
|
292
574
|
key = Redisize.key_name_for(self.class.polymorphic_base_name, attrs, "instance")
|
|
@@ -296,7 +578,12 @@ module Redisize
|
|
|
296
578
|
Redisize.enqueue(:reredisize_instance_metas, key)
|
|
297
579
|
end
|
|
298
580
|
|
|
299
|
-
#
|
|
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)
|
|
300
587
|
def deredisize_instance
|
|
301
588
|
attrs = self.as_json
|
|
302
589
|
key = Redisize.key_name_for(self.class.polymorphic_base_name, attrs, "instance")
|
|
@@ -306,6 +593,12 @@ module Redisize
|
|
|
306
593
|
end
|
|
307
594
|
|
|
308
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'
|
|
309
602
|
def polymorphic_base_name
|
|
310
603
|
base_name = base_class.to_s
|
|
311
604
|
|
|
@@ -316,6 +609,14 @@ module Redisize
|
|
|
316
609
|
end
|
|
317
610
|
end
|
|
318
611
|
|
|
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'
|
|
319
620
|
def primary_key_with key_name_in = nil
|
|
320
621
|
key_name_in || self.respond_to?(:primary_key) && self.primary_key || case self
|
|
321
622
|
when ActiveRecord
|
|
@@ -325,14 +626,28 @@ module Redisize
|
|
|
325
626
|
end
|
|
326
627
|
end
|
|
327
628
|
|
|
328
|
-
#
|
|
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)
|
|
329
636
|
def redisize_sql &block
|
|
330
637
|
key = ["sql", self.name, self.primary_key, self.all.to_sql]
|
|
331
638
|
|
|
332
639
|
redisize_by_key(key, &block)
|
|
333
640
|
end
|
|
334
641
|
|
|
335
|
-
#
|
|
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.
|
|
336
651
|
def redisize_hash key_name_in = nil, &block
|
|
337
652
|
key_name = primary_key_with(key_name_in)
|
|
338
653
|
|
|
@@ -343,7 +658,15 @@ module Redisize
|
|
|
343
658
|
redisize_by_key(key, &block)
|
|
344
659
|
end
|
|
345
660
|
|
|
346
|
-
#
|
|
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.
|
|
347
670
|
def redisize_array key_name_in = nil, &block
|
|
348
671
|
key_name = primary_key_with(key_name_in)
|
|
349
672
|
|
|
@@ -352,18 +675,35 @@ module Redisize
|
|
|
352
675
|
redisize_by_key(key, &block)
|
|
353
676
|
end
|
|
354
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
|
|
355
686
|
def redisize_by_key key, &block
|
|
356
687
|
# binding.pry
|
|
357
688
|
redisize_cache_fetch(key, expires_in: 1.day) do
|
|
358
689
|
value = block.call
|
|
359
690
|
|
|
360
|
-
Redisize.enqueue(:redisize_array_metas, key, value)
|
|
691
|
+
Redisize.enqueue(:redisize_array_metas, key, value) if value.is_a?(Array)
|
|
361
692
|
|
|
362
693
|
value
|
|
363
694
|
end
|
|
364
695
|
end
|
|
365
696
|
|
|
366
|
-
#
|
|
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)
|
|
367
707
|
def redisize_model value, options = {}, &block
|
|
368
708
|
primary_key = options.fetch(:by_key, self.primary_key).to_s
|
|
369
709
|
key = ["instance", name, primary_key, value]
|
data/redisize.gemspec
CHANGED
metadata
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: redisize
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Pavel «Malo» Skrylev
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-11-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: base64
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
13
27
|
- !ruby/object:Gem::Dependency
|
|
14
28
|
name: pry
|
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -45,7 +59,12 @@ files:
|
|
|
45
59
|
- lib/redisize/adapter/inline.rb
|
|
46
60
|
- lib/redisize/adapter/resque.rb
|
|
47
61
|
- lib/redisize/adapter/sidekiq.rb
|
|
62
|
+
- lib/redisize/adapter/sidekiq/codec.rb
|
|
48
63
|
- lib/redisize/cache/rails.rb
|
|
64
|
+
- lib/redisize/coder.rb
|
|
65
|
+
- lib/redisize/coder/base.rb
|
|
66
|
+
- lib/redisize/coder/json.rb
|
|
67
|
+
- lib/redisize/coder/message_pack.rb
|
|
49
68
|
- lib/redisize/log/rails.rb
|
|
50
69
|
- lib/redisize/orm/active_record.rb
|
|
51
70
|
- lib/redisize/support/enumerable.rb
|
|
@@ -74,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
74
93
|
- !ruby/object:Gem::Version
|
|
75
94
|
version: '0'
|
|
76
95
|
requirements: []
|
|
77
|
-
rubygems_version: 3.
|
|
96
|
+
rubygems_version: 3.5.22
|
|
78
97
|
signing_key:
|
|
79
98
|
specification_version: 4
|
|
80
99
|
summary: Make json record cacheable to redis
|