Class: Backrub::Store::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/backrub/store/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, backlog_size = 100) ⇒ Redis

Returns a new instance of Redis.



8
9
10
11
# File 'lib/backrub/store/redis.rb', line 8

def initialize(config={}, backlog_size=100)
  @config = config
  @backlog_size = backlog_size
end

Instance Attribute Details

#backlog_sizeObject (readonly)

Returns the value of attribute backlog_size.



6
7
8
# File 'lib/backrub/store/redis.rb', line 6

def backlog_size
  @backlog_size
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/backrub/store/redis.rb', line 6

def config
  @config
end

Instance Method Details

#backlog(channel, count) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/backrub/store/redis.rb', line 17

def backlog(channel, count)
  backlog = redis.lrange(channel, 0, count - 1)

  backlog.reverse_each do |message|
    yield channel, message
  end
end

#publish(channel, message) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/backrub/store/redis.rb', line 25

def publish(channel, message)
  redis.multi do
    redis.publish(channel, message)
    redis.lpush(channel, message)
    redis.ltrim(channel, 0, backlog_size - 1)
  end
end

#redisObject



13
14
15
# File 'lib/backrub/store/redis.rb', line 13

def redis
  @redis ||= ::Redis.new(config)
end

#subscribe(*channels) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/backrub/store/redis.rb', line 33

def subscribe(*channels)
  # Open a new connection because the connection blocks, causing other threads to be unable to use it
  local_redis = ::Redis.new(config)
  local_redis.subscribe(*channels) do |on|
    on.message do |channel, message|
      yield channel, message
    end
  end
ensure
  local_redis.quit
end