Redis::Set
A unique set of unordered items. Lightweight wrapper over redis sets with some additional enumeration and atomic operations.
Installation
Add this line to your application's Gemfile:
gem 'redis-set'And then execute:
$ bundle
Or install it yourself as:
$ gem install redis-set
Getting started
s = RedisSet.new 'completed_customer_ids'Or you can pass in your own instance of the Redis class.
s = RedisSet.new 'completed_customer_ids', Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)A third option is to instead pass your Redis configurations.
s = RedisSet.new 'completed_customer_ids', :host => "10.0.1.1", :port => 6380, :db => 15Using the set
You can add data to the set using either the add or push methods.
s.add "hello"
s.add "world"
s.add "hello" # the item 'hello' will only exist once in the set since it is uniqueYou can add multiple items
s.add ["one","two","three"]
s.add "four","five","six"
# set should have items "one","two","three","four","five","six" nowYou can insert a new item into the set and get the resultant size of the set atomically
new_count = s.add_with_count "awesome"You can pop a random item from the set
result = s.popYou can pop multiple random items from the set
result = s.pop 5 # pop 5 random items from set and return themYou can remove a specific item from the set
s.remove 5 #remove the item 5 from the set if it existsYou can atomically remove multiple items from the set.
s.remove 3,4,5 #removes items 3,4, and 5 from the set if they existYou can get the size of the set.
s.sizeYou can see if an item exists in the set.
s.include? "hello"You can get all items in the set.
s.allThe set can be cleared of all items
s.clearYou can get the intersection between the set and another set
a = RedisSet.new 'a'
a.push 'a', 'b', 'c', 'd'
b = RedisSet.new 'b'
b.push 'c', 'd', 'e', 'f'
c = RedisSet.new 'c'
c.push 'c', 'd', 'f'
# should return ['c', 'd']
a.intersection b, cThe set can also be set to expire (in seconds).
# expire in five minutes
s.expire 60*5You can enumerate the set in batches.
s.enumerator(100).each{ |i| puts i } #enumerate through the set in batches of 100 items per redis op