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
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
require 'base64'
|
|
2
|
+
require 'redisize/coder'
|
|
3
|
+
|
|
4
|
+
module Redisize
|
|
5
|
+
module Coder
|
|
6
|
+
# Encode binary data and read Redisize's earlier argument envelope.
|
|
7
|
+
class MessagePack < Base
|
|
8
|
+
LIBRARY = 'msgpack'.freeze
|
|
9
|
+
ARGUMENT_FORMAT = '__redisize_message_pack_v1__'.freeze
|
|
10
|
+
ARGUMENT_PREFIX = '__redisize_message_pack_'.freeze
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# +dump+ serializes data as standard MessagePack without a transport header.
|
|
14
|
+
#
|
|
15
|
+
# +value+:: MessagePack-compatible data: Hash, Array, String, Integer,
|
|
16
|
+
# Float, +true+, +false+, or +nil+.
|
|
17
|
+
#
|
|
18
|
+
# Returns:: A binary String. Requires the optional msgpack library.
|
|
19
|
+
#
|
|
20
|
+
# bytes = Redisize::Coder::MessagePack.dump('id' => 42)
|
|
21
|
+
# bytes.bytes # => [129, 162, 105, 100, 42]
|
|
22
|
+
def dump value
|
|
23
|
+
require!
|
|
24
|
+
::MessagePack.pack(value)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# +load+ restores data from standard MessagePack bytes.
|
|
28
|
+
#
|
|
29
|
+
# +value+:: A binary String containing one complete MessagePack value.
|
|
30
|
+
#
|
|
31
|
+
# Returns:: The decoded Ruby value. Malformed or incomplete input raises
|
|
32
|
+
# Redisize::Coder::DecodeError; a missing library raises LoadError.
|
|
33
|
+
#
|
|
34
|
+
# coder = Redisize::Coder::MessagePack
|
|
35
|
+
# coder.load(coder.dump('id' => 42)) # => {"id" => 42}
|
|
36
|
+
def load value
|
|
37
|
+
require!
|
|
38
|
+
begin
|
|
39
|
+
::MessagePack.unpack(value)
|
|
40
|
+
rescue ::MessagePack::UnpackError, EOFError => error
|
|
41
|
+
raise DecodeError, error.message
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# +message_pack_arguments+ reads the earlier Base64 command envelope.
|
|
46
|
+
#
|
|
47
|
+
# +method+:: A command-name String, or +ARGUMENT_FORMAT+ for an encoded
|
|
48
|
+
# command. Unsupported envelope versions raise ArgumentError.
|
|
49
|
+
# +args+:: An Array of ordinary arguments, or a one-element Array containing
|
|
50
|
+
# a Base64 String when +method+ is +ARGUMENT_FORMAT+.
|
|
51
|
+
#
|
|
52
|
+
# Returns:: A two-element Array: the command name and its argument Array.
|
|
53
|
+
# Plain arguments are passed through; malformed envelopes raise.
|
|
54
|
+
#
|
|
55
|
+
# coder = Redisize::Coder::MessagePack
|
|
56
|
+
# coder.message_pack_arguments('refresh', [42]) # => ['refresh', [42]]
|
|
57
|
+
# payload = Base64.strict_encode64(coder.dump(['refresh', 42]))
|
|
58
|
+
# coder.message_pack_arguments(coder::ARGUMENT_FORMAT, [payload])
|
|
59
|
+
# # => ['refresh', [42]]
|
|
60
|
+
def message_pack_arguments method, args
|
|
61
|
+
if method == ARGUMENT_FORMAT
|
|
62
|
+
unless args.size == 1 && args.first.is_a?(String)
|
|
63
|
+
raise ArgumentError, 'Invalid Redisize MessagePack envelope'
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
decoded = load(Base64.strict_decode64(args.first))
|
|
67
|
+
unless decoded.is_a?(Array) && decoded.first.is_a?(String)
|
|
68
|
+
raise ArgumentError, 'Invalid Redisize MessagePack arguments'
|
|
69
|
+
end
|
|
70
|
+
[decoded.shift, decoded]
|
|
71
|
+
elsif method.is_a?(String) && method.start_with?(ARGUMENT_PREFIX)
|
|
72
|
+
raise ArgumentError, "Unsupported Redisize argument format: #{method}"
|
|
73
|
+
else
|
|
74
|
+
[method, args]
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module Redisize
|
|
2
|
+
# Adapter-independent implementations of the supported data formats.
|
|
3
|
+
module Coder
|
|
4
|
+
# A decoded byte sequence is not valid for the requested format.
|
|
5
|
+
class DecodeError < StandardError; end
|
|
6
|
+
|
|
7
|
+
autoload :Base, File.join(__dir__, 'coder/base')
|
|
8
|
+
autoload :Json, File.join(__dir__, 'coder/json')
|
|
9
|
+
autoload :MessagePack, File.join(__dir__, 'coder/message_pack')
|
|
10
|
+
|
|
11
|
+
CODERS = { json: :Json, message_pack: :MessagePack }.freeze
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# +find+ looks up an implementation without requiring its external library.
|
|
15
|
+
#
|
|
16
|
+
# +name+:: A Symbol: +:json+ or +:message_pack+. Other names are unknown.
|
|
17
|
+
#
|
|
18
|
+
# Returns:: A coder Class for a known name, otherwise +nil+.
|
|
19
|
+
#
|
|
20
|
+
# Redisize::Coder.find(:json) # => Redisize::Coder::Json
|
|
21
|
+
# Redisize::Coder.find(:unknown) # => nil
|
|
22
|
+
def find name
|
|
23
|
+
constant = CODERS[name]
|
|
24
|
+
const_get(constant, false) if constant
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# +fetch+ looks up an implementation and rejects unknown names.
|
|
28
|
+
#
|
|
29
|
+
# +name+:: A Symbol: +:json+ or +:message_pack+.
|
|
30
|
+
#
|
|
31
|
+
# Returns:: The coder Class. Unknown names raise ArgumentError.
|
|
32
|
+
#
|
|
33
|
+
# Redisize::Coder.fetch(:json) # => Redisize::Coder::Json
|
|
34
|
+
def fetch name
|
|
35
|
+
find(name) || raise(ArgumentError, "Unsupported coder: #{name}")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# +resolve+ selects a coder independently of any queue backend.
|
|
39
|
+
#
|
|
40
|
+
# +name+:: +:json+ or +:message_pack+ selects that implementation;
|
|
41
|
+
# +:auto+ prefers available MessagePack and falls back to JSON.
|
|
42
|
+
#
|
|
43
|
+
# Returns:: A coder Class. Explicit names do not load its library yet;
|
|
44
|
+
# unknown names raise ArgumentError.
|
|
45
|
+
#
|
|
46
|
+
# Redisize::Coder.resolve(:json) # => Redisize::Coder::Json
|
|
47
|
+
def resolve name
|
|
48
|
+
return fetch(name) unless name == :auto
|
|
49
|
+
|
|
50
|
+
MessagePack.available? ? MessagePack : Json
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/redisize/log/rails.rb
CHANGED
|
@@ -1,14 +1,35 @@
|
|
|
1
1
|
module Redisize
|
|
2
2
|
module Log
|
|
3
3
|
module Rails
|
|
4
|
+
# +redisize_log_info+ delegates a info-severity message to Rails.logger.
|
|
5
|
+
#
|
|
6
|
+
# +args+:: Positional arguments accepted by the configured logger; normally one String or other message object. No block is forwarded.
|
|
7
|
+
#
|
|
8
|
+
# Returns:: The configured logger result; a standard Ruby Logger returns true, including when the severity is filtered.
|
|
9
|
+
#
|
|
10
|
+
# Redisize.redisize_log_info('Cache entry updated') # => true (standard Logger)
|
|
4
11
|
def redisize_log_info *args
|
|
5
12
|
::Rails.logger.info(*args)
|
|
6
13
|
end
|
|
7
14
|
|
|
15
|
+
# +redisize_log_debug+ delegates a debug-severity message to Rails.logger.
|
|
16
|
+
#
|
|
17
|
+
# +args+:: Positional arguments accepted by the configured logger; normally one String or other message object. No block is forwarded.
|
|
18
|
+
#
|
|
19
|
+
# Returns:: The configured logger result; a standard Ruby Logger returns true, including when the severity is filtered.
|
|
20
|
+
#
|
|
21
|
+
# Redisize.redisize_log_debug('Cache entry updated') # => true (standard Logger)
|
|
8
22
|
def redisize_log_debug *args
|
|
9
23
|
::Rails.logger.debug(*args)
|
|
10
24
|
end
|
|
11
25
|
|
|
26
|
+
# +redisize_log_error+ delegates a error-severity message to Rails.logger.
|
|
27
|
+
#
|
|
28
|
+
# +args+:: Positional arguments accepted by the configured logger; normally one String or other message object. No block is forwarded.
|
|
29
|
+
#
|
|
30
|
+
# Returns:: The configured logger result; a standard Ruby Logger returns true, including when the severity is filtered.
|
|
31
|
+
#
|
|
32
|
+
# Redisize.redisize_log_error('Cache entry updated') # => true (standard Logger)
|
|
12
33
|
def redisize_log_error *args
|
|
13
34
|
::Rails.logger.error(*args)
|
|
14
35
|
end
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
module Redisize
|
|
2
2
|
module ORM
|
|
3
3
|
module ActiveRecord
|
|
4
|
+
# +included+ wraps persistence methods to maintain Redisize dependencies.
|
|
5
|
+
#
|
|
6
|
+
# +kls+:: An ActiveRecord model Class exposing create_or_update, destroy, update_columns and touch.
|
|
7
|
+
#
|
|
8
|
+
# Returns:: The Symbol :destroy from the final method definition inside class_eval; normal Class#include returns its receiver. Intended to run once per class.
|
|
9
|
+
#
|
|
10
|
+
# class Article < ActiveRecord::Base
|
|
11
|
+
# include Redisize
|
|
12
|
+
# end
|
|
13
|
+
# Article.instance_methods.include?(:__redisize_destroy) # => true
|
|
4
14
|
def self.included kls
|
|
5
15
|
kls.class_eval do
|
|
6
16
|
alias_method :__redisize_create_or_update, :create_or_update
|
|
@@ -8,6 +18,15 @@ module Redisize
|
|
|
8
18
|
alias_method :__redisize_update_columns, :update_columns
|
|
9
19
|
alias_method :__redisize_touch, :touch
|
|
10
20
|
|
|
21
|
+
# +create_or_update+ calls the original persistence method and schedules cache maintenance.
|
|
22
|
+
#
|
|
23
|
+
# +args+:: Keyword options supported by the installed ActiveRecord create_or_update, such as validate: false.
|
|
24
|
+
# +block+:: An optional persistence block forwarded to the original method.
|
|
25
|
+
#
|
|
26
|
+
# Returns:: The original persistence result, normally true/false. New records invalidate model dependencies; existing records refresh instance cache. Maintenance is not conditional on success.
|
|
27
|
+
#
|
|
28
|
+
# article.save # => true (when validation and persistence succeed)
|
|
29
|
+
# # ActiveRecord invokes create_or_update internally.
|
|
11
30
|
def create_or_update **args, &block
|
|
12
31
|
new_record = new_record?
|
|
13
32
|
state = __redisize_create_or_update(**args, &block)
|
|
@@ -17,6 +36,13 @@ module Redisize
|
|
|
17
36
|
state
|
|
18
37
|
end
|
|
19
38
|
|
|
39
|
+
# +update_columns+ delegates a direct column update and refreshes instance cache.
|
|
40
|
+
#
|
|
41
|
+
# +args+:: Column-name keyword pairs accepted by the aliased ActiveRecord method, for example title: "News".
|
|
42
|
+
#
|
|
43
|
+
# Returns:: The original update result, normally true/false. ActiveRecord exceptions propagate; cache refresh follows a returned result.
|
|
44
|
+
#
|
|
45
|
+
# article.update_columns(title: 'News') # => true (when the row is updated)
|
|
20
46
|
def update_columns **args
|
|
21
47
|
state = __redisize_update_columns(**args)
|
|
22
48
|
|
|
@@ -25,6 +51,13 @@ module Redisize
|
|
|
25
51
|
state
|
|
26
52
|
end
|
|
27
53
|
|
|
54
|
+
# +touch+ delegates a timestamp touch and refreshes instance cache.
|
|
55
|
+
#
|
|
56
|
+
# +args+:: Keyword options accepted by the installed ActiveRecord touch method, for example time: Time.current. This wrapper accepts no positional attribute names.
|
|
57
|
+
#
|
|
58
|
+
# Returns:: The original touch result, normally true/false. Errors from ActiveRecord propagate.
|
|
59
|
+
#
|
|
60
|
+
# article.touch(time: Time.current) # => true (when the row is touched)
|
|
28
61
|
def touch **args
|
|
29
62
|
state = __redisize_touch(**args)
|
|
30
63
|
|
|
@@ -33,6 +66,12 @@ module Redisize
|
|
|
33
66
|
state
|
|
34
67
|
end
|
|
35
68
|
|
|
69
|
+
# +destroy+ delegates destruction and enqueues record and model invalidation.
|
|
70
|
+
# Takes no arguments.
|
|
71
|
+
#
|
|
72
|
+
# Returns:: The original destroy result: usually the frozen record on success or false when a callback aborts. Invalidation follows any returned result.
|
|
73
|
+
#
|
|
74
|
+
# article.destroy # => article (destroyed and frozen on success)
|
|
36
75
|
def destroy
|
|
37
76
|
state = __redisize_destroy
|
|
38
77
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module Redisize
|
|
2
|
+
module Support
|
|
3
|
+
module Enumerable
|
|
4
|
+
# +included+ defines the collection redisize helper on the including module or class.
|
|
5
|
+
#
|
|
6
|
+
# +kls+:: A Module or Class, normally Enumerable; the helper is defined with class_eval.
|
|
7
|
+
#
|
|
8
|
+
# Returns:: The Symbol :redisize from the method definition; ordinary include returns the including receiver.
|
|
9
|
+
#
|
|
10
|
+
# Enumerable.include(Redisize::Support::Enumerable) # => Enumerable
|
|
11
|
+
def self.included kls
|
|
12
|
+
kls.class_eval do
|
|
13
|
+
# +redisize+ caches collection values after applying each element jsonize representation.
|
|
14
|
+
# Takes no arguments.
|
|
15
|
+
#
|
|
16
|
+
# Returns:: The cached block result: an Array produced by map for Array and Hash receivers. Hash iteration supplies key/value pairs. The fallback branch references x and raises NameError on ordinary unsupported receivers.
|
|
17
|
+
#
|
|
18
|
+
# # With Rails cache, Redisize::ClassMethods and collection integration installed:
|
|
19
|
+
# [].redisize # => []
|
|
20
|
+
def redisize
|
|
21
|
+
case self
|
|
22
|
+
when Hash
|
|
23
|
+
redisize_hash { map { |x| x.jsonize } }
|
|
24
|
+
when Array
|
|
25
|
+
redisize_array { map { |x| x.jsonize } }
|
|
26
|
+
else
|
|
27
|
+
x.jsonize
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
data/lib/redisize/version.rb
CHANGED