sidekiq_smart_cache 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0bfe51b0884c74e8f916b2e07a756b14fa41810dc3c137225bc978ac6a41ab81
4
- data.tar.gz: e5c34d6795f47aa5b4ba17e7ca538ada975cdef8406c113a8107b205cd540955
3
+ metadata.gz: e5e2eaeccb56ff1b361713b28c59b5535ba4dcb03ce09892b78829452733e204
4
+ data.tar.gz: 7d2223177c94a71ff9d00aa8eb7a37ecb80b6c8c5777c7d5321043220563d730
5
5
  SHA512:
6
- metadata.gz: b66ae306d585ac20cf6b0e5d47bd634cb1fb36ba4e51b041c33eb3f77471190405cb4a64face8a53b578425e85f9bf85a345fd869f262ce91516941fab17e168
7
- data.tar.gz: 2fc77d0ad12e9c317dddb1d6a8358256fb86f74330ed1fde78a71e61fa9914a6dd55ff8406a57770de8b0476615ff0ac7ceba6ddf3dc0f93d999c3007d896249
6
+ metadata.gz: 5f2d87f31936b45cb6636536117d8b4625efd7f180d41b3b3fb1b8182adca8af567896809ae0991bb29babf63abd9a06f2bb439cc894b2f2c16757181ae0db7a
7
+ data.tar.gz: 399039ca0b02a6818d291f02295e0e9bf42ebe0cc33aea2fb846094287328c33b75198a15f8ca10a6abfbf49ed586341a5e34f6c4168cffcc5f0ab56c570833a
@@ -1,6 +1,6 @@
1
1
  module SidekiqSmartCache
2
2
  BLOCKING_COMMANDS = %i[ brpop ].freeze
3
- COMMANDS = BLOCKING_COMMANDS + %i[ get set lpush expire flushdb del ].freeze
3
+ COMMANDS = BLOCKING_COMMANDS + %i[get set lpush expire flushdb del ttl].freeze
4
4
 
5
5
  ERROR_TO_CATCH = if defined?(::RedisClient)
6
6
  ::RedisClient::CommandError
@@ -14,7 +14,7 @@ module SidekiqSmartCache
14
14
  end
15
15
 
16
16
  def job_completion_key(key)
17
- key + '/done'
17
+ "#{key}/done"
18
18
  end
19
19
 
20
20
  def send_done_message(key)
@@ -25,31 +25,60 @@ module SidekiqSmartCache
25
25
  def wait_for_done_message(key, timeout)
26
26
  return true if defined?(Sidekiq::Testing) && Sidekiq::Testing.inline?
27
27
 
28
- if brpop(job_completion_key(key), timeout: timeout.to_i)
29
- # log_msg("got done message for #{key}")
30
- send_done_message(key) # put it back for any other readers
31
- true
32
- end
28
+ return unless brpop(job_completion_key(key), timeout: timeout.to_i)
29
+
30
+ # log_msg("got done message for #{key}")
31
+ send_done_message(key) # put it back for any other readers
32
+ true
33
33
  end
34
34
 
35
35
  def log_msg(msg) # WIP all this
36
36
  Rails.logger.info("#{Time.now.iso8601(3)} #{Thread.current[:name]} redis #{msg}")
37
37
  end
38
38
 
39
- def method_missing(name, ...)
40
- @pool.with do |r|
41
- if COMMANDS.include? name
39
+ # In support of syntax like redis.set('foo', 'bar', nx: true, ex: 60)
40
+ # the new client wants that called like redis.set('foo', 'bar', 'NX', 'EX', '60')
41
+ def arrayify_args(args)
42
+ out = []
43
+ args.each do |arg|
44
+ if arg.is_a?(Hash)
45
+ arg.each do |kw, val|
46
+ # nx: true becomes 'NX'
47
+ if val == true
48
+ out << kw.upcase.to_s
49
+ elsif val
50
+ # ex: 60 becomes ['EX', '60']
51
+ out << kw.upcase.to_s
52
+ out << val.to_s
53
+ end
54
+ end
55
+ else
56
+ out << arg
57
+ end
58
+ end
59
+ out.presence
60
+ end
61
+
62
+ # Defining methods like `redis.set('foo', 'bar')`
63
+ COMMANDS.each do |name|
64
+ define_method(name) do |*args|
65
+ @pool.with do |r|
42
66
  retryable = true
43
67
  begin
44
68
  # log_msg("#{name} #{args}")
45
- if r.respond_to?(name)
46
- # old redis gem implements methods including `brpop` and `flusdb`
47
- r.send(name, ...)
69
+ if defined?(::Redis) && r.is_a?(::Redis)
70
+ if r.respond_to?(name)
71
+ # old redis gem implements methods including `brpop` and `flusdb`
72
+ r.send(name, *args)
73
+ else
74
+ r.call(name.to_s.upcase, *args)
75
+ end
48
76
  elsif BLOCKING_COMMANDS.include? name
49
77
  # support redis-client semantics
50
- make_blocking_call(r, name, ...)
78
+ make_blocking_call(r, name, *args)
51
79
  else
52
- r.call(name.to_s.upcase, ...)
80
+ # Translate keyword args to new style array on RedisClient (even wrapped by Sidekiq)
81
+ r.send(name, *arrayify_args(args))
53
82
  end
54
83
  # stolen from sidekiq - Thanks Mike!
55
84
  # Quietly consume this one and return nil
@@ -63,8 +92,6 @@ module SidekiqSmartCache
63
92
  end
64
93
  raise
65
94
  end
66
- else
67
- super
68
95
  end
69
96
  end
70
97
  end
@@ -18,7 +18,7 @@ class Result
18
18
 
19
19
  def self.load_from(cache_tag)
20
20
  raw = redis.get(cache_tag)
21
- new(YAML.safe_load(raw, allowed_classes)) if raw
21
+ new(YAML.safe_load(raw, permitted_classes: allowed_classes)) if raw
22
22
  end
23
23
 
24
24
  def initialize(result)
@@ -1,3 +1,3 @@
1
1
  module SidekiqSmartCache
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq_smart_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bill Kirtley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-16 00:00:00.000000000 Z
11
+ date: 2023-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  - !ruby/object:Gem::Version
78
78
  version: '0'
79
79
  requirements: []
80
- rubygems_version: 3.3.3
80
+ rubygems_version: 3.4.1
81
81
  signing_key:
82
82
  specification_version: 4
83
83
  summary: Cache objects in redis, generated with thundering herd prevention via sidekiq.