Class: Backrub::Store::Redis
Instance Attribute Summary collapse
-
#backlog_size ⇒ Object
readonly
Returns the value of attribute backlog_size.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #backlog(channel, count) ⇒ Object
-
#initialize(config = {}, backlog_size = 100) ⇒ Redis
constructor
A new instance of Redis.
- #publish(channel, message) ⇒ Object
- #redis ⇒ Object
- #subscribe(*channels) ⇒ Object
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_size ⇒ Object (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 |
#config ⇒ Object (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 || yield channel, end end |
#publish(channel, message) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/backrub/store/redis.rb', line 25 def publish(channel, ) redis.multi do redis.publish(channel, ) redis.lpush(channel, ) redis.ltrim(channel, 0, backlog_size - 1) end end |
#redis ⇒ Object
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. do |channel, | yield channel, end end ensure local_redis.quit end |