Skip to content
Lightweight wrapper over redis sets
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin first commit Jan 14, 2017
lib fix bug with intersection Mar 25, 2019
spec updates Mar 25, 2019
.gitignore first commit Jan 14, 2017
Gemfile first commit Jan 14, 2017
README.md update readme Mar 25, 2019
Rakefile first commit Jan 14, 2017
redis-set.gemspec reorganize Mar 25, 2019

README.md

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 => 15

Using 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 unique

You can add multiple items

s.add ["one","two","three"]
s.add "four","five","six"
# set should have items "one","two","three","four","five","six" now

You 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.pop

You can pop multiple random items from the set

result = s.pop 5 # pop 5 random items from set and return them

You can remove a specific item from the set

s.remove 5  #remove the item 5 from the set if it exists

You can atomically remove multiple items from the set.

s.remove 3,4,5  #removes items 3,4, and 5 from the set if they exist

You can get the size of the set.

s.size

You can see if an item exists in the set.

s.include? "hello"

You can get all items in the set.

s.all

The set can be cleared of all items

s.clear

You 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, c

The set can also be set to expire (in seconds).

# expire in five minutes
s.expire 60*5

You 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
You can’t perform that action at this time.