Class: Restis::Client
- Inherits:
-
Object
- Object
- Restis::Client
- Defined in:
- lib/restis/client.rb
Instance Attribute Summary collapse
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
- #publish(channel, message) ⇒ Object
- #subscribe(channel, key, &block) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
5 6 7 8 |
# File 'lib/restis/client.rb', line 5 def initialize(args = {}) @args = args @redis = Redis.new(@args) end |
Instance Attribute Details
#redis ⇒ Object
Returns the value of attribute redis.
4 5 6 |
# File 'lib/restis/client.rb', line 4 def redis @redis end |
Instance Method Details
#publish(channel, message) ⇒ Object
10 11 12 13 |
# File 'lib/restis/client.rb', line 10 def publish(channel, ) @redis.rpush("#{channel}:backlog", ) @redis.publish(channel, ) end |
#subscribe(channel, key, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/restis/client.rb', line 15 def subscribe(channel, key, &block) loop do last_read_msg = @redis.get(key) queue_size = @redis.llen("#{channel}:backlog") = @redis.lrange("#{channel}:backlog", last_read_msg, queue_size) break if .empty? .each do |msg| block.call(@redis, channel, msg) @redis.incr(key) end @redis.set(key, queue_size) end redis = Redis.new(:timeout => 0) redis.subscribe(channel) do |on| on. do |channel, msg| block.call(redis, channel, msg) Redis.new(@args).incr(key) end end end |