Class: Turnpike::Queue

Inherits:
Base
  • Object
show all
Defined in:
lib/turnpike/queue.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#clear, #empty?, #initialize

Constructor Details

This class inherits a constructor from Turnpike::Base

Instance Method Details

#bpop(n = 1) ⇒ Object

 Pop one or more items from the queue and block if the queue is empty.

n - Integer number of items to pop.

Returns an item or an Array of items.



24
25
26
27
# File 'lib/turnpike/queue.rb', line 24

def bpop(n = 1)
  items = n.times.map { unpack(redis.blpop(name).last) }
  n == 1 ? items.first : items
end

#pop(n = 1) ⇒ Object

 Pop one or more items from the queue.

n - Integer number of items to pop.

Returns an item, an Array of items, or nil if the queue is empty.



10
11
12
13
14
15
16
17
# File 'lib/turnpike/queue.rb', line 10

def pop(n = 1)
  items = n.times.reduce([]) { |ary, _|
    break ary unless item = redis.lpop(name)
    ary << unpack(item)
  }

  n == 1 ? items.first : items
end

#push(*items) ⇒ Object Also known as: <<

Push items to the end of the queue.

items - A splat Array of items.

Returns nothing.



34
35
36
# File 'lib/turnpike/queue.rb', line 34

def push(*items)
  redis.rpush(name, items.map { |i| pack(i) })
end

#sizeObject

Returns the Integer size of the queue.



41
42
43
# File 'lib/turnpike/queue.rb', line 41

def size
  redis.llen(name)
end

#unshift(*items) ⇒ Object

Push items to the front of the queue.

items - A splat Array of items.

Returns nothing.



50
51
52
# File 'lib/turnpike/queue.rb', line 50

def unshift(*items)
  redis.lpush(name, items.map { |i| pack(i) })
end