Class: Coordinator::RedisQueue
- Inherits:
-
Object
- Object
- Coordinator::RedisQueue
- Defined in:
- lib/coordinator/redis_queue.rb
Direct Known Subclasses
Instance Method Summary collapse
- #capacity ⇒ Object
- #capacity=(capacity) ⇒ Object
- #full? ⇒ Boolean
-
#initialize(name) ⇒ RedisQueue
constructor
A new instance of RedisQueue.
- #items ⇒ Object
- #left_push(item) ⇒ Object
- #length ⇒ Object
- #peek ⇒ Object
- #pop ⇒ Object
- #push(item) ⇒ Object
- #remove(item) ⇒ Object
Constructor Details
#initialize(name) ⇒ RedisQueue
Returns a new instance of RedisQueue.
5 6 7 8 9 10 |
# File 'lib/coordinator/redis_queue.rb', line 5 def initialize(name) @queue_name = "#{name}-queue" @capacity_name = "#{name}-capacity" raise Coordinator::Error, "'Redis.current' not set" unless Redis.current @redis = Redis.current end |
Instance Method Details
#capacity ⇒ Object
42 43 44 45 |
# File 'lib/coordinator/redis_queue.rb', line 42 def capacity data = @redis.get(@capacity_name) deserialize(data) end |
#capacity=(capacity) ⇒ Object
47 48 49 |
# File 'lib/coordinator/redis_queue.rb', line 47 def capacity=(capacity) @redis.set(@capacity_name, capacity) end |
#full? ⇒ Boolean
55 56 57 |
# File 'lib/coordinator/redis_queue.rb', line 55 def full? capacity && capacity <= length end |
#items ⇒ Object
51 52 53 |
# File 'lib/coordinator/redis_queue.rb', line 51 def items serialized_items.map { |i| deserialize(i) } end |
#left_push(item) ⇒ Object
18 19 20 21 |
# File 'lib/coordinator/redis_queue.rb', line 18 def left_push(item) data = serialize(item) @redis.lpush(@queue_name, data) unless serialized_items.include?(data) end |
#length ⇒ Object
38 39 40 |
# File 'lib/coordinator/redis_queue.rb', line 38 def length @redis.llen(@queue_name) end |
#peek ⇒ Object
33 34 35 36 |
# File 'lib/coordinator/redis_queue.rb', line 33 def peek data = @redis.lrange(@queue_name, 0, 0).first deserialize(data) end |
#pop ⇒ Object
23 24 25 26 |
# File 'lib/coordinator/redis_queue.rb', line 23 def pop data = @redis.lpop(@queue_name) deserialize(data) end |
#push(item) ⇒ Object
12 13 14 15 16 |
# File 'lib/coordinator/redis_queue.rb', line 12 def push(item) return false if full? data = serialize(item) @redis.rpush(@queue_name, data) unless serialized_items.include?(data) end |
#remove(item) ⇒ Object
28 29 30 31 |
# File 'lib/coordinator/redis_queue.rb', line 28 def remove(item) data = serialize(item) @redis.lrem(@queue_name, 1, data) == 1 end |