Class: RedisQueue
- Inherits:
-
Object
- Object
- RedisQueue
- Defined in:
- lib/redis_ext/redis_queue.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RedisQueue
constructor
A new instance of RedisQueue.
- #length ⇒ Object
- #pop ⇒ Object
- #pop_first(count) ⇒ Object
- #push(value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RedisQueue
Returns a new instance of RedisQueue.
7 8 9 10 |
# File 'lib/redis_ext/redis_queue.rb', line 7 def initialize( = {}) @key = [:key] @redis = ([:redis] || Redis.new()) end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
5 6 7 |
# File 'lib/redis_ext/redis_queue.rb', line 5 def key @key end |
#redis ⇒ Object
Returns the value of attribute redis.
5 6 7 |
# File 'lib/redis_ext/redis_queue.rb', line 5 def redis @redis end |
Instance Method Details
#length ⇒ Object
28 29 30 |
# File 'lib/redis_ext/redis_queue.rb', line 28 def length @redis.llen(@key) end |
#pop ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/redis_ext/redis_queue.rb', line 16 def pop begin value = JSON.parse(@redis.lpop(@key)) new_hash = {} value.each{|k, v| new_hash[k.to_sym] = v} return new_hash # Returns nil on any kind of exception rescue Exception return nil end end |
#pop_first(count) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/redis_ext/redis_queue.rb', line 32 def pop_first(count) list = [] count.times do element = self.pop break unless element list << element end list end |
#push(value) ⇒ Object
12 13 14 |
# File 'lib/redis_ext/redis_queue.rb', line 12 def push(value) @redis.rpush(@key, value.to_json) end |