Class: Turnpike::UniqueQueue

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

Constant Summary collapse

ZPOP =
<<-EOF
  local res = redis.call('zrange', KEYS[1], 0, KEYS[2] - 1)
  redis.pcall('zrem', KEYS[1], unpack(res))
  return res
EOF
ZPOP_SHA =
Digest::SHA1.hexdigest(ZPOP)

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#bpop, #clear, #empty?, #initialize

Constructor Details

This class inherits a constructor from Turnpike::Base

Instance Method Details

#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.



19
20
21
22
# File 'lib/turnpike/unique_queue.rb', line 19

def pop(n = 1)
  items = zpop(n).map { |i| unpack(i) }
  n == 1 ? items.first : items
end

#push(*items, score: Time.now.to_f) ⇒ Object Also known as: <<

Push items to the end of the queue.

items - A splat Array of items.

Returns nothing.



29
30
31
# File 'lib/turnpike/unique_queue.rb', line 29

def push(*items, score: Time.now.to_f)
  redis.zadd(name, items.reduce([]) { |ary, i| ary.push(score, pack(i)) })
end

#sizeObject

Returns the Integer size of the queue.



36
37
38
# File 'lib/turnpike/unique_queue.rb', line 36

def size
  redis.zcard(name)
end

#unshift(*items) ⇒ Object

Push items to the front of the queue.

items - A splat Array of items.

Returns nothing.



45
46
47
48
# File 'lib/turnpike/unique_queue.rb', line 45

def unshift(*items)
  _, score = redis.zrange(name, 0, 0, with_scores: true).pop
  score ? push(*items, score: score - 1) : push(*items)
end