Class: SidekiqSmartCache::Redis
- Inherits:
-
Object
- Object
- SidekiqSmartCache::Redis
- Defined in:
- lib/sidekiq_smart_cache/redis.rb
Instance Method Summary collapse
-
#arrayify_args(args) ⇒ Object
In support of syntax like redis.set(‘foo’, ‘bar’, nx: true, ex: 60) the new client wants that called like redis.set(‘foo’, ‘bar’, ‘NX’, ‘EX’, ‘60’).
-
#initialize(pool) ⇒ Redis
constructor
A new instance of Redis.
- #job_completion_key(key) ⇒ Object
-
#log_msg(msg) ⇒ Object
WIP all this.
- #make_blocking_call(r, name, *args) ⇒ Object
- #send_done_message(key) ⇒ Object
- #wait_for_done_message(key, timeout) ⇒ Object
Constructor Details
#initialize(pool) ⇒ Redis
Returns a new instance of Redis.
12 13 14 |
# File 'lib/sidekiq_smart_cache/redis.rb', line 12 def initialize(pool) @pool = pool end |
Instance Method Details
#arrayify_args(args) ⇒ Object
In support of syntax like redis.set(‘foo’, ‘bar’, nx: true, ex: 60) the new client wants that called like redis.set(‘foo’, ‘bar’, ‘NX’, ‘EX’, ‘60’)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sidekiq_smart_cache/redis.rb', line 41 def arrayify_args(args) out = [] args.each do |arg| if arg.is_a?(Hash) arg.each do |kw, val| # nx: true becomes 'NX' if val == true out << kw.upcase.to_s elsif val # ex: 60 becomes ['EX', '60'] out << kw.upcase.to_s out << val.to_s end end else out << arg end end out.presence end |
#job_completion_key(key) ⇒ Object
16 17 18 |
# File 'lib/sidekiq_smart_cache/redis.rb', line 16 def job_completion_key(key) "#{key}/done" end |
#log_msg(msg) ⇒ Object
WIP all this
35 36 37 |
# File 'lib/sidekiq_smart_cache/redis.rb', line 35 def log_msg(msg) # WIP all this Rails.logger.info("#{Time.now.iso8601(3)} #{Thread.current[:name]} redis #{msg}") end |
#make_blocking_call(r, name, *args) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sidekiq_smart_cache/redis.rb', line 99 def make_blocking_call(r, name, *args) # The Redis `brpop` implementation seems to allow timeout to be the last argument # or a key in a hash `timeout: 5`, so: # `r.brpop('key1', 'key2', 5)` - wait five seconds for either queue # `r.brpop('key1', 'key2', timeout: 5)` - same # `r.brpop('key1', 'key2')` - wait forever on either queue timeout = if args.last.is_a?(Hash) = args.pop [:timeout] else args.pop end # With RedisClient, the doc is a little thin, but it looks like we want to start with the timeout # then the verb, then the array of keys # and end with ... a 0? blocking_call_args = [timeout, name.to_s.upcase] + args + [0] r.blocking_call(*blocking_call_args) rescue ::RedisClient::TimeoutError nil # quietly return nil in this case end |
#send_done_message(key) ⇒ Object
20 21 22 23 |
# File 'lib/sidekiq_smart_cache/redis.rb', line 20 def (key) lpush(job_completion_key(key), 'done') expire(job_completion_key(key), 1) end |
#wait_for_done_message(key, timeout) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/sidekiq_smart_cache/redis.rb', line 25 def (key, timeout) return true if defined?(Sidekiq::Testing) && Sidekiq::Testing.inline? return unless brpop(job_completion_key(key), timeout: timeout.to_i) # log_msg("got done message for #{key}") (key) # put it back for any other readers true end |