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(length) ⇒ Object
- #push(value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RedisQueue
Returns a new instance of RedisQueue.
8 9 10 11 |
# File 'lib/redis_ext/redis_queue.rb', line 8 def initialize( = {}) @key = [:key] @redis = ([:redis] || Redis.new()) end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/redis_ext/redis_queue.rb', line 6 def key @key end |
#redis ⇒ Object
Returns the value of attribute redis.
6 7 8 |
# File 'lib/redis_ext/redis_queue.rb', line 6 def redis @redis end |
Instance Method Details
#length ⇒ Object
29 30 31 |
# File 'lib/redis_ext/redis_queue.rb', line 29 def length @redis.llen(@key) end |
#pop ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/redis_ext/redis_queue.rb', line 17 def pop begin value = JSON.parse(@redis.lpop(@key)) new_hash = {} value.each{|k, v| new_hash[k.to_sym] = v} return new_hash rescue Exception => ex puts ex return nil end end |
#pop_first(length) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/redis_ext/redis_queue.rb', line 33 def pop_first(length) list = [] length.times do element = self.pop break unless element list << element end list end |
#push(value) ⇒ Object
13 14 15 |
# File 'lib/redis_ext/redis_queue.rb', line 13 def push(value) @redis.rpush(@key, value.to_json) end |