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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 286acdcb49ceaf7f9bddf229a07fa9495e5462014affec0640258f4f7c7f343a
|
|
4
|
+
data.tar.gz: c15fe9a2a1e13f6fb3f94101a23b2dc3bf5b3fcb6e9bec94185ddc4df3a27d58
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 87c3a5dbc7bdc7abcad4ddf4d39d6fda228bf87ae33b31fc468406938890982c67696415027eead15dfb48bf28b35ec1ae7fe91bfa86259d5adab64f9f237ee7
|
|
7
|
+
data.tar.gz: cce2661cf02c9e6b974fde5e8d877edbde8cae08a4a5649e193fb94e302d3b71cc98cb2ce98d4c5b3d88858e459922ace3555940ad4947be96579a9b8665cf25
|
data/README.md
CHANGED
|
@@ -28,6 +28,93 @@ Redisize.adapter_kind = :inline
|
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
Other values are ```:resque```, and ```:sidekiq```.
|
|
31
|
+
|
|
32
|
+
### Data coders and Sidekiq job encoding
|
|
33
|
+
|
|
34
|
+
`Redisize.json_coder` configures an adapter-independent data coder. Its default
|
|
35
|
+
is `:auto`: if `require 'msgpack'` succeeds, Sidekiq jobs are encoded with
|
|
36
|
+
MessagePack directly in Redis; otherwise the existing JSON format is used. With
|
|
37
|
+
Bundler, add `gem 'msgpack'` to your application's Gemfile to enable MessagePack.
|
|
38
|
+
The `msgpack` gem is optional and is not installed by redisize itself.
|
|
39
|
+
|
|
40
|
+
Configure it in a Rails initializer, for example
|
|
41
|
+
`config/initializers/redisize.rb`:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
Redisize.json_coder = :auto
|
|
45
|
+
# Or require MessagePack; raises LoadError if msgpack is unavailable:
|
|
46
|
+
Redisize.json_coder = :message_pack
|
|
47
|
+
|
|
48
|
+
# Select and load the adapter before a worker or Web process reads jobs:
|
|
49
|
+
Redisize.adapter_kind = :sidekiq
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
These are the only supported values; other values raise `ArgumentError`.
|
|
53
|
+
Forced MessagePack mode checks availability when the setting is assigned.
|
|
54
|
+
|
|
55
|
+
Redisize exposes `coder_available?(name)` as a public predicate. Availability
|
|
56
|
+
is checked and cached independently for `:json` and `:message_pack`; unsupported
|
|
57
|
+
names return false. This does not add `:json` as a configuration mode.
|
|
58
|
+
|
|
59
|
+
```ruby
|
|
60
|
+
Redisize.coder_available?(:message_pack)
|
|
61
|
+
Redisize.has_message_pack?
|
|
62
|
+
Redisize.require_coder!(:message_pack)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The generic JSON and MessagePack implementations live in `lib/redisize/coder/`.
|
|
66
|
+
They can encode/decode prepared Ruby data without loading Sidekiq. The common
|
|
67
|
+
MessagePack coder also reads the earlier Base64 argument envelope; the worker
|
|
68
|
+
only delegates to it. Configuration, availability and optional-library loading
|
|
69
|
+
do not belong to an adapter. Every generic format reader reports malformed
|
|
70
|
+
input as `Redisize::Coder::DecodeError`; the Sidekiq bridge translates that into
|
|
71
|
+
the error type expected by Sidekiq.
|
|
72
|
+
|
|
73
|
+
The complete `Redisize::Adapter::Sidekiq` job is stored with a binary version
|
|
74
|
+
header followed by MessagePack. New jobs have neither a Base64 layer nor an
|
|
75
|
+
outer JSON envelope. Other Sidekiq workers retain their existing JSON format.
|
|
76
|
+
Sidekiq's argument validation still applies; prepare model data with jsonize or
|
|
77
|
+
as_json before enqueueing.
|
|
78
|
+
|
|
79
|
+
Updated workers read both native MessagePack jobs and existing JSON jobs,
|
|
80
|
+
regardless of their current `json_coder` setting. The intermediate Base64
|
|
81
|
+
argument format is also readable for compatibility. Reading either MessagePack
|
|
82
|
+
format requires `msgpack` on the worker. Unsupported versions or malformed native
|
|
83
|
+
records raise `JSON::ParserError`, matching Sidekiq's invalid-job handling.
|
|
84
|
+
|
|
85
|
+
The transport belongs to the Sidekiq adapter and installs when that adapter is
|
|
86
|
+
loaded. Merely loading redisize defines the common coder interface but does not
|
|
87
|
+
load Sidekiq or install its transport. Rails applications can select and
|
|
88
|
+
configure the adapter with the initializer above.
|
|
89
|
+
Standalone worker and Web processes must load Sidekiq and redisize, then load
|
|
90
|
+
the adapter before reading jobs, for example with
|
|
91
|
+
`Redisize.adapter_kind = :sidekiq`. Client processes also load it when they first
|
|
92
|
+
enqueue through this adapter. The implementation lives in
|
|
93
|
+
`lib/redisize/adapter/sidekiq/codec.rb`; it contains only the transport header,
|
|
94
|
+
job validation, Redis member handling, search and Web integration. The abstract
|
|
95
|
+
Redisize layer has no Sidekiq integration hooks. jsonize itself is unchanged.
|
|
96
|
+
`PREFIX` and `HEADER` identify this adapter's versioned job envelope, whose body
|
|
97
|
+
is validated as a Sidekiq job; they are not standard MessagePack bytes.
|
|
98
|
+
`WEB_TOKEN` belongs to Sidekiq Web's delete form. These markers stay in the
|
|
99
|
+
adapter; the generic MessagePack coder produces ordinary unframed MessagePack.
|
|
100
|
+
Deploy updated workers and Web processes with `msgpack` before enabling native
|
|
101
|
+
encoding on producers. Old Sidekiq processes cannot read native records.
|
|
102
|
+
|
|
103
|
+
Queue inspection, scheduled jobs, retries, dead jobs and rescheduling use the
|
|
104
|
+
same transport. Sorted-set `scan` searches a JSON view of decoded records on the
|
|
105
|
+
client, so JSON-fragment searches remain usable but transfer more data than a
|
|
106
|
+
server-side Redis MATCH. Rescheduling preserves the original stored bytes,
|
|
107
|
+
including for old JSON jobs. Sidekiq Web responses, heartbeat/work-state data and
|
|
108
|
+
logs remain JSON; nested native payloads are rendered as readable JSON strings.
|
|
109
|
+
The Web queue-delete form uses a reversible Base64 token in its hidden HTML
|
|
110
|
+
field, so the exact binary member can be deleted. This is an HTTP presentation
|
|
111
|
+
detail; queued records themselves are not wrapped in Base64 or JSON.
|
|
112
|
+
|
|
113
|
+
The integration uses Sidekiq singleton/API extensions, not a global JSON monkey
|
|
114
|
+
patch. It is verified with Sidekiq 7.1.6; other versions and Pro/Enterprise
|
|
115
|
+
extensions require separate validation. It does not change jsonize's output,
|
|
116
|
+
Rails cache serialization, or the application's HTTP API.
|
|
117
|
+
|
|
31
118
|
And then to use the gem just define in the target object:
|
|
32
119
|
|
|
33
120
|
```ruby
|
|
@@ -2,6 +2,14 @@ module Redisize
|
|
|
2
2
|
module Adapter
|
|
3
3
|
class Inline
|
|
4
4
|
class << self
|
|
5
|
+
# +enqueue+ executes a Redisize command immediately in the calling process.
|
|
6
|
+
#
|
|
7
|
+
# +method+:: A String or Symbol naming a Redisize singleton method.
|
|
8
|
+
# +args+:: Zero or more arguments forwarded unchanged to the command.
|
|
9
|
+
#
|
|
10
|
+
# Returns:: The command result; command exceptions propagate synchronously.
|
|
11
|
+
#
|
|
12
|
+
# Redisize::Adapter::Inline.enqueue(:rekey, ['json', 'Article']) # => ['meta', 'Article']
|
|
5
13
|
def enqueue method, *args
|
|
6
14
|
Redisize.send(method, *args)
|
|
7
15
|
end
|
|
@@ -4,14 +4,38 @@ module Redisize
|
|
|
4
4
|
@queue = :caching
|
|
5
5
|
|
|
6
6
|
class << self
|
|
7
|
+
# +enqueue+ submits this adapter worker to the Resque caching queue.
|
|
8
|
+
#
|
|
9
|
+
# +args+:: The command name (String/Symbol) followed by command arguments; values must be supported by Resque serialization.
|
|
10
|
+
#
|
|
11
|
+
# Returns:: The underlying Resque enqueue result, normally true when queued or nil when rejected by a hook.
|
|
12
|
+
#
|
|
13
|
+
# Redisize::Adapter::Resque.enqueue('deredisize_model_metas', 'Article')
|
|
14
|
+
# # => true (when accepted by Resque)
|
|
7
15
|
def enqueue *args
|
|
8
16
|
::Resque.enqueue(self, *args)
|
|
9
17
|
end
|
|
10
18
|
|
|
19
|
+
# +lock_workers+ reports the queue used by this worker.
|
|
20
|
+
#
|
|
21
|
+
# +_method+:: Any command name; ignored.
|
|
22
|
+
# +_args+:: Zero or more arguments; ignored.
|
|
23
|
+
#
|
|
24
|
+
# Returns:: The Symbol :caching. This method does not itself acquire a lock.
|
|
25
|
+
#
|
|
26
|
+
# Redisize::Adapter::Resque.lock_workers('refresh', 42) # => :caching
|
|
11
27
|
def lock_workers _method, *_args
|
|
12
28
|
@queue
|
|
13
29
|
end
|
|
14
30
|
|
|
31
|
+
# +perform+ executes a command restored by Resque.
|
|
32
|
+
#
|
|
33
|
+
# +method+:: A String or Symbol naming a Redisize singleton method.
|
|
34
|
+
# +args+:: Zero or more command arguments, forwarded unchanged.
|
|
35
|
+
#
|
|
36
|
+
# Returns:: The Redisize command result; errors propagate to Resque.
|
|
37
|
+
#
|
|
38
|
+
# Redisize::Adapter::Resque.perform('rekey', ['json', 'Article']) # => ['meta', 'Article']
|
|
15
39
|
def perform method, *args
|
|
16
40
|
Redisize.send(method, *args)
|
|
17
41
|
end
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'base64'
|
|
3
|
+
|
|
4
|
+
# Connect the generic Redisize coders to Sidekiq's storage and public APIs.
|
|
5
|
+
class Redisize::Adapter::Sidekiq
|
|
6
|
+
# A transport for complete Redisize jobs, not a replacement for JSON's API.
|
|
7
|
+
module Codec
|
|
8
|
+
# Adapter-owned job framing; neither marker is part of standard MessagePack.
|
|
9
|
+
PREFIX = "\x00redisize:msgpack:".b.freeze
|
|
10
|
+
HEADER = "\x00redisize:msgpack:1\x00".b.freeze
|
|
11
|
+
WORKER = 'Redisize::Adapter::Sidekiq'.freeze
|
|
12
|
+
# Text framing used only by Sidekiq Web's queue-delete form.
|
|
13
|
+
WEB_TOKEN = 'redisize:msgpack-form:1:'.freeze
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
# +install!+ attaches this adapter's storage, search and Web integration.
|
|
17
|
+
# Takes no arguments and can be called repeatedly.
|
|
18
|
+
#
|
|
19
|
+
# Returns:: Sidekiq::WebAction when its hook is first installed, otherwise
|
|
20
|
+
# +nil+. Requiring this file already performs installation.
|
|
21
|
+
#
|
|
22
|
+
# Redisize::Adapter::Sidekiq::Codec.install! # => nil, already installed
|
|
23
|
+
def install!
|
|
24
|
+
require 'sidekiq/api'
|
|
25
|
+
require 'sidekiq/web/action'
|
|
26
|
+
|
|
27
|
+
::Sidekiq.singleton_class.prepend(Transport) unless ::Sidekiq.singleton_class.ancestors.include?(Transport)
|
|
28
|
+
::Sidekiq::SortedSet.prepend(Search) unless ::Sidekiq::SortedSet.ancestors.include?(Search)
|
|
29
|
+
::Sidekiq::SortedEntry.prepend(Reschedule) unless ::Sidekiq::SortedEntry.ancestors.include?(Reschedule)
|
|
30
|
+
::Sidekiq::WebAction.prepend(WebJSON) unless ::Sidekiq::WebAction.ancestors.include?(WebJSON)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# +job?+ recognizes a complete job owned by this adapter.
|
|
34
|
+
#
|
|
35
|
+
# +value+:: Any Ruby object. A matching Hash must have +class+ equal to
|
|
36
|
+
# +WORKER+ and +args+ containing an Array.
|
|
37
|
+
#
|
|
38
|
+
# Returns:: +true+ for a matching Hash, otherwise +false+.
|
|
39
|
+
#
|
|
40
|
+
# codec = Redisize::Adapter::Sidekiq::Codec
|
|
41
|
+
# codec.job?('class' => codec::WORKER, 'args' => []) # => true
|
|
42
|
+
def job? value
|
|
43
|
+
value.is_a?(Hash) && value['class'] == WORKER && value['args'].is_a?(Array)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# +encoded?+ recognizes a native record prefix without validating its body.
|
|
47
|
+
#
|
|
48
|
+
# +value+:: Any Ruby object; only Strings beginning with +PREFIX+ match.
|
|
49
|
+
#
|
|
50
|
+
# Returns:: A Boolean. +true+ does not guarantee a supported version or body.
|
|
51
|
+
#
|
|
52
|
+
# codec = Redisize::Adapter::Sidekiq::Codec
|
|
53
|
+
# codec.encoded?(codec::HEADER) # => true
|
|
54
|
+
# codec.encoded?('{}') # => false
|
|
55
|
+
def encoded? value
|
|
56
|
+
value.is_a?(String) && value.b.start_with?(PREFIX)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# +unpack+ validates this adapter's header and restores its job record.
|
|
60
|
+
#
|
|
61
|
+
# +value+:: A binary String containing +HEADER+ followed by one MessagePack
|
|
62
|
+
# Hash accepted by +job?+.
|
|
63
|
+
#
|
|
64
|
+
# Returns:: The decoded job Hash. Unsupported versions and malformed jobs
|
|
65
|
+
# raise JSON::ParserError; an unavailable msgpack raises LoadError.
|
|
66
|
+
#
|
|
67
|
+
# codec = Redisize::Adapter::Sidekiq::Codec
|
|
68
|
+
# job = { 'class' => codec::WORKER, 'args' => [] }
|
|
69
|
+
# wire = codec::HEADER + Redisize::Coder::MessagePack.dump(job)
|
|
70
|
+
# codec.unpack(wire) # => {"class"=>"Redisize::Adapter::Sidekiq", "args"=>[]}
|
|
71
|
+
def unpack value
|
|
72
|
+
unless value.b.start_with?(HEADER)
|
|
73
|
+
raise ::JSON::ParserError, 'Unsupported Redisize MessagePack transport version'
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
begin
|
|
77
|
+
job = ::Redisize::Coder::MessagePack.load(value.byteslice(HEADER.bytesize..-1))
|
|
78
|
+
rescue ::Redisize::Coder::DecodeError => error
|
|
79
|
+
raise ::JSON::ParserError, "Invalid Redisize MessagePack job: #{error.message}"
|
|
80
|
+
end
|
|
81
|
+
raise ::JSON::ParserError, 'Invalid Redisize MessagePack job structure' unless job?(job)
|
|
82
|
+
|
|
83
|
+
job
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# +json_safe+ prepares Sidekiq status/log data for JSON presentation.
|
|
87
|
+
#
|
|
88
|
+
# +value+:: A Hash, Array or scalar, possibly containing native job Strings.
|
|
89
|
+
#
|
|
90
|
+
# Returns:: Ruby data with native job Strings replaced by readable JSON
|
|
91
|
+
# Strings. Invalid native records become hexadecimal diagnostic
|
|
92
|
+
# Strings; ordinary scalars are unchanged. Hashes/Arrays are copied.
|
|
93
|
+
#
|
|
94
|
+
# codec = Redisize::Adapter::Sidekiq::Codec
|
|
95
|
+
# codec.json_safe(['ready', 42]) # => ['ready', 42]
|
|
96
|
+
def json_safe value
|
|
97
|
+
case value
|
|
98
|
+
when String
|
|
99
|
+
return value unless encoded?(value)
|
|
100
|
+
|
|
101
|
+
begin
|
|
102
|
+
::Redisize::Coder::Json.dump(unpack(value))
|
|
103
|
+
rescue ::JSON::ParserError
|
|
104
|
+
"Invalid Redisize MessagePack (hex): #{value.unpack('H*').first}"
|
|
105
|
+
end
|
|
106
|
+
when Hash
|
|
107
|
+
value.each_with_object({}) { |(key, item), result| result[key] = json_safe(item) }
|
|
108
|
+
when Array
|
|
109
|
+
value.map { |item| json_safe(item) }
|
|
110
|
+
else
|
|
111
|
+
value
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Connect the generic coders to Sidekiq's Redis serialization entry points.
|
|
117
|
+
module Transport
|
|
118
|
+
# +dump_json+ connects Sidekiq's writer to the selected data coder.
|
|
119
|
+
#
|
|
120
|
+
# +value+:: A complete Redisize job Hash, another job Hash, or JSON-compatible
|
|
121
|
+
# status/log data. Only Redisize jobs use the selected binary coder.
|
|
122
|
+
#
|
|
123
|
+
# Returns:: A binary String with +Codec::HEADER+ for native jobs, or a JSON
|
|
124
|
+
# String for JSON jobs and other callers.
|
|
125
|
+
#
|
|
126
|
+
# Sidekiq.dump_json('count' => 1) # => '{"count":1}'
|
|
127
|
+
def dump_json value
|
|
128
|
+
if Codec.job?(value)
|
|
129
|
+
coder = ::Redisize::Coder.resolve(Redisize.json_coder)
|
|
130
|
+
bytes = coder.dump(value)
|
|
131
|
+
coder == ::Redisize::Coder::MessagePack ? Codec::HEADER + bytes : bytes
|
|
132
|
+
elsif value.is_a?(Hash) && value.key?('class') && value['args'].is_a?(Array)
|
|
133
|
+
# Never reinterpret application arguments of a JSON job as a
|
|
134
|
+
# work-state/log copy of a native payload.
|
|
135
|
+
super(value)
|
|
136
|
+
else
|
|
137
|
+
super(Codec.json_safe(value))
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# +load_json+ reads either format through the common data coders.
|
|
142
|
+
#
|
|
143
|
+
# +value+:: A String containing JSON, or this adapter's native job record.
|
|
144
|
+
#
|
|
145
|
+
# Returns:: Decoded Ruby data, including a Hash for native jobs. Malformed
|
|
146
|
+
# input raises JSON::ParserError as expected by Sidekiq.
|
|
147
|
+
#
|
|
148
|
+
# Sidekiq.load_json('{"count":1}') # => {"count" => 1}
|
|
149
|
+
def load_json value
|
|
150
|
+
return Codec.unpack(value) if Codec.encoded?(value)
|
|
151
|
+
|
|
152
|
+
::Redisize::Coder::Json.load(value)
|
|
153
|
+
rescue ::Redisize::Coder::DecodeError => error
|
|
154
|
+
raise ::JSON::ParserError, error.message
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Preserve sorted-set searches across JSON and native Redis members.
|
|
159
|
+
module Search
|
|
160
|
+
# +scan+ searches JSON records and JSON views of native job records.
|
|
161
|
+
#
|
|
162
|
+
# +match+:: A String containing a text fragment or Redis glob pattern;
|
|
163
|
+
# fragments without +*+ are wrapped in wildcards.
|
|
164
|
+
# +count+:: A positive Integer scan hint; defaults to +100+, not a result limit.
|
|
165
|
+
# Block:: Optional; receives a Sidekiq::SortedEntry for each matching job.
|
|
166
|
+
#
|
|
167
|
+
# Returns:: An Enumerator without a block, or +nil+ after yielding matches.
|
|
168
|
+
# Native matches are filtered on the client, not by Redis MATCH.
|
|
169
|
+
#
|
|
170
|
+
# set = Sidekiq::ScheduledSet.new
|
|
171
|
+
# set.scan('RefreshWorker', 50) # => #<Enumerator: ...>
|
|
172
|
+
def scan match, count = 100
|
|
173
|
+
return to_enum(:scan, match, count) unless block_given?
|
|
174
|
+
|
|
175
|
+
pattern = (match.include?('*') ? match : "*#{match}*").b
|
|
176
|
+
# Redis treats ! as a literal in bracket classes; fnmatch also
|
|
177
|
+
# accepts it as negation. Escape it for the client-side match.
|
|
178
|
+
native_pattern = pattern.gsub('[!') { '[\!' }
|
|
179
|
+
::Sidekiq.redis do |connection|
|
|
180
|
+
# Keep Redis's exact MATCH behavior for ordinary JSON records.
|
|
181
|
+
connection.zscan(name, match: pattern, count: count) do |entry, score|
|
|
182
|
+
yield ::Sidekiq::SortedEntry.new(self, score, entry) unless Codec.encoded?(entry)
|
|
183
|
+
end
|
|
184
|
+
connection.zscan(name, match: Codec::PREFIX + '*', count: count) do |entry, score|
|
|
185
|
+
text = Codec.json_safe(entry)
|
|
186
|
+
if File.fnmatch?(native_pattern, text.b, File::FNM_DOTMATCH)
|
|
187
|
+
yield ::Sidekiq::SortedEntry.new(self, score, entry)
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Preserve member identity when only a scheduled timestamp changes.
|
|
195
|
+
module Reschedule
|
|
196
|
+
# +reschedule+ changes a job's score without re-encoding its Redis member.
|
|
197
|
+
#
|
|
198
|
+
# +at+:: A Time or Numeric Unix timestamp in seconds.
|
|
199
|
+
#
|
|
200
|
+
# Returns:: The new score returned by Redis: a Float with RESP3, or a numeric
|
|
201
|
+
# String with RESP2. Existing payload bytes are kept unchanged.
|
|
202
|
+
#
|
|
203
|
+
# entry.reschedule(Time.at(2_000_000_000)) # => 2000000000.0 with RESP3
|
|
204
|
+
def reschedule at
|
|
205
|
+
::Sidekiq.redis do |connection|
|
|
206
|
+
member = value.is_a?(String) ? value : ::Sidekiq.dump_json(item)
|
|
207
|
+
connection.zincrby(parent.name, at.to_f - score, member)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Keep Sidekiq's HTTP responses and HTML form values text-safe.
|
|
213
|
+
module WebJSON
|
|
214
|
+
# +h+ makes native records safe for Sidekiq Web's hidden delete-form field.
|
|
215
|
+
#
|
|
216
|
+
# +text+:: An ordinary String to HTML-escape, or a native job String to wrap
|
|
217
|
+
# in the reversible +Codec::WEB_TOKEN+ text format.
|
|
218
|
+
#
|
|
219
|
+
# Returns:: An HTML-escaped String; native record bytes are not put in HTML.
|
|
220
|
+
# Ordinary text is delegated to Sidekiq's original helper.
|
|
221
|
+
#
|
|
222
|
+
# h('<tag>') # => '<tag>'
|
|
223
|
+
def h text
|
|
224
|
+
if Codec.encoded?(text)
|
|
225
|
+
super(Codec::WEB_TOKEN + Base64.strict_encode64(text))
|
|
226
|
+
else
|
|
227
|
+
super
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# +params+ restores native member bytes submitted by the delete form.
|
|
232
|
+
# Takes no arguments; it reads the current Sidekiq Web request.
|
|
233
|
+
#
|
|
234
|
+
# Returns:: The parameter Hash, with a recognized +key_val+ token decoded
|
|
235
|
+
# to its binary String. A malformed token halts the request with 400.
|
|
236
|
+
#
|
|
237
|
+
# For a request with +?page=2+ and no route parameters:
|
|
238
|
+
#
|
|
239
|
+
# params # => {"page" => "2"}
|
|
240
|
+
def params
|
|
241
|
+
values = super
|
|
242
|
+
token = values['key_val']
|
|
243
|
+
if token.is_a?(String) && token.start_with?(Codec::WEB_TOKEN)
|
|
244
|
+
begin
|
|
245
|
+
raw = Base64.strict_decode64(token.byteslice(Codec::WEB_TOKEN.bytesize..-1))
|
|
246
|
+
rescue ArgumentError
|
|
247
|
+
halt(400)
|
|
248
|
+
end
|
|
249
|
+
halt(400) unless Codec.encoded?(raw)
|
|
250
|
+
values['key_val'] = raw
|
|
251
|
+
end
|
|
252
|
+
values
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# +json+ renders an HTTP JSON response, not a binary Redis job record.
|
|
256
|
+
#
|
|
257
|
+
# +payload+:: JSON-compatible Ruby data, possibly containing native job Strings
|
|
258
|
+
# that +Codec.json_safe+ converts for display.
|
|
259
|
+
#
|
|
260
|
+
# Returns:: A Rack response Array: status +200+, JSON/cache-control headers,
|
|
261
|
+
# and an Array containing the JSON response String.
|
|
262
|
+
#
|
|
263
|
+
# status, headers, body = json('count' => 1)
|
|
264
|
+
# status # => 200
|
|
265
|
+
# body # => ['{"count":1}']
|
|
266
|
+
def json payload
|
|
267
|
+
[200, {::Rack::CONTENT_TYPE => 'application/json', ::Rack::CACHE_CONTROL => 'private, no-store'},
|
|
268
|
+
[::Redisize::Coder::Json.dump(Codec.json_safe(payload))]]
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
Redisize::Adapter::Sidekiq::Codec.install!
|
|
@@ -6,11 +6,29 @@ module Redisize
|
|
|
6
6
|
sidekiq_options limits: { caching: 1 }
|
|
7
7
|
sidekiq_options process_limits: { caching: 1 }
|
|
8
8
|
|
|
9
|
+
# +perform+ executes a Redisize command restored by Sidekiq.
|
|
10
|
+
#
|
|
11
|
+
# +method+:: A command-name String or Symbol, or the earlier MessagePack encoded-argument marker.
|
|
12
|
+
# +args+:: Zero or more command arguments restored by the backend.
|
|
13
|
+
#
|
|
14
|
+
# Returns:: The command result; exceptions propagate to Sidekiq for its retry handling.
|
|
15
|
+
#
|
|
16
|
+
# Redisize::Adapter::Sidekiq.new.perform('rekey', ['json', 'Article'])
|
|
17
|
+
# # => ['meta', 'Article']
|
|
9
18
|
def perform method, *args
|
|
19
|
+
method, args = Coder::MessagePack.message_pack_arguments(method, args)
|
|
10
20
|
Redisize.send(method, *args)
|
|
11
21
|
end
|
|
12
22
|
|
|
13
23
|
class << self
|
|
24
|
+
# +enqueue+ submits a command to the Sidekiq caching queue.
|
|
25
|
+
#
|
|
26
|
+
# +args+:: A command-name String followed by its serializable arguments; forwarded to perform_async.
|
|
27
|
+
#
|
|
28
|
+
# Returns:: A Sidekiq job-ID String, or nil when client middleware cancels submission.
|
|
29
|
+
#
|
|
30
|
+
# Redisize::Adapter::Sidekiq.enqueue('deredisize_model_metas', 'Article')
|
|
31
|
+
# # => '0123456789abcdef01234567' (example job ID)
|
|
14
32
|
def enqueue *args
|
|
15
33
|
self.perform_async(*args)
|
|
16
34
|
end
|
|
@@ -18,3 +36,5 @@ module Redisize
|
|
|
18
36
|
end
|
|
19
37
|
end
|
|
20
38
|
end
|
|
39
|
+
|
|
40
|
+
require 'redisize/adapter/sidekiq/codec'
|
data/lib/redisize/cache/rails.rb
CHANGED
|
@@ -1,18 +1,50 @@
|
|
|
1
1
|
module Redisize
|
|
2
2
|
module Cache
|
|
3
3
|
module Rails
|
|
4
|
+
# +redisize_cache_write+ delegates a cache write to Rails.cache.
|
|
5
|
+
#
|
|
6
|
+
# +key+:: Any key supported by the configured Rails cache store, including String or Array.
|
|
7
|
+
# +object+:: The Ruby value to cache; serialization is performed by the Rails store.
|
|
8
|
+
# +options+:: A Hash of Rails write options; defaults to {}, for example expires_in: 60.
|
|
9
|
+
#
|
|
10
|
+
# Returns:: The configured store write result, normally true on success; store errors propagate.
|
|
11
|
+
#
|
|
12
|
+
# Redisize.redisize_cache_write('answer', 42, expires_in: 60) # => true (MemoryStore)
|
|
4
13
|
def redisize_cache_write key, object, options = {}
|
|
5
14
|
::Rails.cache.write(key, object, options)
|
|
6
15
|
end
|
|
7
16
|
|
|
17
|
+
# +redisize_cache_read+ reads an entry from Rails.cache.
|
|
18
|
+
#
|
|
19
|
+
# +key+:: Any key supported by the Rails store, for example a String or Array.
|
|
20
|
+
#
|
|
21
|
+
# Returns:: The cached Ruby value, or nil when missing or expired.
|
|
22
|
+
#
|
|
23
|
+
# Redisize.redisize_cache_read('missing-key') # => nil (uncached key)
|
|
8
24
|
def redisize_cache_read key
|
|
9
25
|
::Rails.cache.read(key)
|
|
10
26
|
end
|
|
11
27
|
|
|
28
|
+
# +redisize_cache_fetch+ delegates cache lookup and miss generation to Rails.cache.
|
|
29
|
+
#
|
|
30
|
+
# +key+:: A Rails-supported cache key.
|
|
31
|
+
# +options+:: A Hash of Rails fetch options; defaults to {}, for example expires_in: 60 or force: true.
|
|
32
|
+
# +block+:: Optional unless a value must be generated; Rails invokes it on a miss with the normalized key and, in supported Rails versions, write options.
|
|
33
|
+
#
|
|
34
|
+
# Returns:: The cached value or block result; without a block, behaves according to the configured Rails fetch implementation.
|
|
35
|
+
#
|
|
36
|
+
# Redisize.redisize_cache_fetch('answer', expires_in: 60) { 42 } # => 42
|
|
12
37
|
def redisize_cache_fetch key, options = {}, &block
|
|
13
38
|
::Rails.cache.fetch(key, options, &block)
|
|
14
39
|
end
|
|
15
40
|
|
|
41
|
+
# +redisize_cache_delete+ delegates cache deletion to Rails.cache.
|
|
42
|
+
#
|
|
43
|
+
# +key+:: Any key supported by the Rails store, for example a String or Array.
|
|
44
|
+
#
|
|
45
|
+
# Returns:: The store deletion result, usually true for a deleted entry or false/nil when absent.
|
|
46
|
+
#
|
|
47
|
+
# Redisize.redisize_cache_delete('missing-key') # => false (MemoryStore)
|
|
16
48
|
def redisize_cache_delete key
|
|
17
49
|
::Rails.cache.delete(key)
|
|
18
50
|
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'redisize/coder'
|
|
2
|
+
|
|
3
|
+
module Redisize
|
|
4
|
+
module Coder
|
|
5
|
+
# Shared optional-library loading for adapter-independent coders.
|
|
6
|
+
class Base
|
|
7
|
+
class << self
|
|
8
|
+
# +available?+ checks and caches this implementation's library availability.
|
|
9
|
+
# Takes no arguments; call it on a concrete coder subclass.
|
|
10
|
+
#
|
|
11
|
+
# Returns:: +true+ if the library loads, or +false+ if it is missing.
|
|
12
|
+
# Errors inside an installed library are propagated.
|
|
13
|
+
#
|
|
14
|
+
# Redisize::Coder::Json.available? # => true
|
|
15
|
+
def available?
|
|
16
|
+
return @available if instance_variable_defined?(:@available)
|
|
17
|
+
|
|
18
|
+
begin
|
|
19
|
+
require self::LIBRARY
|
|
20
|
+
rescue LoadError => error
|
|
21
|
+
raise unless error.path == self::LIBRARY
|
|
22
|
+
|
|
23
|
+
return @available = false
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
@available = true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# +require!+ loads this implementation's library or raises LoadError.
|
|
30
|
+
# Takes no arguments; call it on a concrete coder subclass.
|
|
31
|
+
#
|
|
32
|
+
# Returns:: The receiver coder Class, suitable for calling +dump+ or +load+.
|
|
33
|
+
#
|
|
34
|
+
# Redisize::Coder::Json.require! # => Redisize::Coder::Json
|
|
35
|
+
def require!
|
|
36
|
+
return self if @available
|
|
37
|
+
|
|
38
|
+
begin
|
|
39
|
+
require self::LIBRARY
|
|
40
|
+
rescue LoadError => error
|
|
41
|
+
raise unless error.path == self::LIBRARY
|
|
42
|
+
|
|
43
|
+
raise LoadError, "#{name} requires the #{self::LIBRARY} gem"
|
|
44
|
+
end
|
|
45
|
+
@available = true
|
|
46
|
+
self
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# +reset_availability!+ clears this subclass's cached availability flag.
|
|
50
|
+
# Takes no arguments. The next +available?+ call checks the library again.
|
|
51
|
+
#
|
|
52
|
+
# Returns:: The old cached Boolean, or +nil+ if no flag was cached.
|
|
53
|
+
#
|
|
54
|
+
# Redisize::Coder::Json.available? # => true
|
|
55
|
+
# Redisize::Coder::Json.reset_availability! # => true
|
|
56
|
+
# Redisize::Coder::Json.reset_availability! # => nil
|
|
57
|
+
def reset_availability!
|
|
58
|
+
remove_instance_variable(:@available) if instance_variable_defined?(:@available)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require 'redisize/coder'
|
|
2
|
+
|
|
3
|
+
module Redisize
|
|
4
|
+
module Coder
|
|
5
|
+
# Encode and decode JSON without installing adapter-specific hooks.
|
|
6
|
+
class Json < Base
|
|
7
|
+
LIBRARY = 'json'.freeze
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
# +dump+ serializes prepared Ruby data using JSON.
|
|
11
|
+
#
|
|
12
|
+
# +value+:: JSON-compatible data: Hash, Array, String, Integer,
|
|
13
|
+
# finite Float, +true+, +false+, or +nil+.
|
|
14
|
+
#
|
|
15
|
+
# Returns:: A JSON String containing the supplied data.
|
|
16
|
+
#
|
|
17
|
+
# Redisize::Coder::Json.dump('id' => 42) # => '{"id":42}'
|
|
18
|
+
def dump value
|
|
19
|
+
require!
|
|
20
|
+
::JSON.generate(value)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# +load+ restores Ruby data from JSON text.
|
|
24
|
+
#
|
|
25
|
+
# +value+:: A String containing a JSON document, including a scalar document.
|
|
26
|
+
#
|
|
27
|
+
# Returns:: The decoded Hash, Array, String, number, Boolean, or +nil+.
|
|
28
|
+
# Malformed JSON raises Redisize::Coder::DecodeError.
|
|
29
|
+
#
|
|
30
|
+
# Redisize::Coder::Json.load('{"id":42}') # => {"id" => 42}
|
|
31
|
+
def load value
|
|
32
|
+
require!
|
|
33
|
+
begin
|
|
34
|
+
::JSON.parse(value)
|
|
35
|
+
rescue ::JSON::ParserError => error
|
|
36
|
+
raise DecodeError, error.message
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|