Class: SidekiqSmartCache::Redis
- Inherits:
-
Object
- Object
- SidekiqSmartCache::Redis
show all
- Defined in:
- lib/sidekiq_smart_cache/redis.rb
Instance Method Summary
collapse
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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/sidekiq_smart_cache/redis.rb', line 39
def method_missing(name, ...)
@pool.with do |r|
if COMMANDS.include? name
retryable = true
begin
if r.respond_to?(name)
r.send(name, ...)
elsif BLOCKING_COMMANDS.include? name
make_blocking_call(r, name, ...)
else
r.call(name.to_s.upcase, ...)
end
rescue ERROR_TO_CATCH => ex
if retryable && ex.message =~ /READONLY/
r.disconnect!
retryable = false
retry
end
raise
end
else
super
end
end
end
|
Instance Method Details
#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
35
36
37
|
# File 'lib/sidekiq_smart_cache/redis.rb', line 35
def log_msg(msg) Rails.logger.info("#{Time.now.iso8601(3)} #{Thread.current[:name]} redis #{msg}")
end
|
#make_blocking_call(r, name, *args) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/sidekiq_smart_cache/redis.rb', line 72
def make_blocking_call(r, name, *args)
timeout = if args.last.is_a?(Hash)
options = args.pop
options[:timeout]
else
args.pop
end
blocking_call_args = [timeout, name.to_s.upcase] + args + [0]
r.blocking_call(*blocking_call_args)
rescue ::RedisClient::TimeoutError
nil end
|
#send_done_message(key) ⇒ Object
20
21
22
23
|
# File 'lib/sidekiq_smart_cache/redis.rb', line 20
def send_done_message(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 wait_for_done_message(key, timeout)
return true if defined?(Sidekiq::Testing) && Sidekiq::Testing.inline?
if brpop(job_completion_key(key), timeout: timeout.to_i)
send_done_message(key) true
end
end
|