Redis::Unique::Queue
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 = Redis::Unique::Queue.new 'jobs'
Or you can pass in your own instance of the Redis class.
q = Redis::Unique::Queue.new 'jobs', Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
A third option is to instead pass your Redis configurations.
q = Redis::Unique::Queue.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 pop data from the queue.
result = q.pop
You can get the size of the queue.
q.size
You can read the first item in the queue.
q.front
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
The queue can be cleared of all items
q.clear
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