Class: Restis::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/restis/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#redisObject

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, message)
	@redis.rpush("#{channel}:backlog", message)
	@redis.publish(channel, message)
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")
		messages = @redis.lrange("#{channel}:backlog", last_read_msg, queue_size)
		break if messages.empty?
		messages.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.message do |channel, msg|
			block.call(redis, channel, msg)
			Redis.new(@args).incr(key)
		end
	end
end