class Backrub::Store::Redis

Attributes

backlog_size[R]
config[R]

Public Class Methods

new(config={}, backlog_size=100) click to toggle source
# File lib/backrub/store/redis.rb, line 8
def initialize(config={}, backlog_size=100)
  @config = config
  @backlog_size = backlog_size
end

Public Instance Methods

backlog(channel, count) { |channel, message| ... } click to toggle source
# 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) click to toggle source
# 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
redis() click to toggle source
# File lib/backrub/store/redis.rb, line 13
def redis
  @redis ||= ::Redis.new(config)
end
subscribe(*channels) { |channel, message| ... } click to toggle source
# 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