RedisUniqueQueue
A unique FIFO queue with atomic operations built on top of Redis. Useful if you want to enqueue data without worrying about it existing multiple times in the queue.
Installation
Add this line to your application's Gemfile:
gem 'redis-unique-queue'
And then execute:
$ bundle
Or install it yourself as:
$ gem install redis-unique-queue
Getting started
You can instantiate a named queue using your default Redis configuration.
q = RedisUniqueQueue.new 'jobs'
Or you can pass in your own instance of the Redis class.
q = RedisUniqueQueue.new 'jobs', Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
A third option is to instead pass your Redis configurations.
q = RedisUniqueQueue.new 'jobs', :host => "10.0.1.1", :port => 6380, :db => 15
Using the queue
You can push data to the queue.
q.push "hello"
q.push "world"
q.push "hello" # the item 'hello' will only exist once in the queue since it is unique
You can push multiple items onto the queue.
q.push_multi [1,2,3]
q.push_multi 4,5,6
You can pop data from the queue.
result = q.pop
You can atomically pop multiple items from the queue.
result = q.pop_multi 3
You can also pop all items in the queue in one op.
result = q.pop_all
You can get the size of the queue.
q.size
You can read the first item in the queue.
q.front
You can read the last item in the queue.
q.back
You can see if an item exists in the queue.
q.include? "hello"
You can remove an arbitrary item from the queue. Note that it doesn't have to be the first item.
q.remove "world"
You can remove an arbitrary item from the queue by index.
q.remove_item_by_index 2
You can get all items in the queue.
q.all
You can also peek into arbitrary ranges in the queue.
q.peek 1 #read the item at index 1
q.peek 23 #read the item at index 23
q.peek 10, 5 #peek at five items starting at index 10
You can also peek into arbitrary ranges in the queue in reverse order.
q.peek_reverse 0 #read the last item in the queue
q.peek_reverse 7 #read the item at index 7 starting at the end of the queue
q.peek_reverse 2, 5 #peek at five items starting at index 2 in reverse order
The queue can be cleared of all items
q.clear
Optionally, the queue can also be set to expire (in seconds).
# expire in five minutes
q.expire 60*5
Contributing
- Fork it ( http://github.com//redis-unique-queue/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request