Module: Redis::Sets
- Defined in:
- lib/redis/sets.rb
Instance Method Summary collapse
- #redis_SADD(key, member) ⇒ Object
- #redis_SCARD(key) ⇒ Object
- #redis_SDIFF(*keys) ⇒ Object
- #redis_SDIFFSTORE(destination, *keys) ⇒ Object
- #redis_SINTER(*keys) ⇒ Object
- #redis_SINTERSTORE(destination, *keys) ⇒ Object
- #redis_SISMEMBER(key, member) ⇒ Object
- #redis_SMEMBERS(key) ⇒ Object
-
#redis_SMOVE(source, destination, member) ⇒ Object
Type checks are only to make tests pass.
- #redis_SPOP(key) ⇒ Object
- #redis_SRANDMEMBER(key) ⇒ Object
- #redis_SREM(key, member) ⇒ Object
- #redis_SUNION(*keys) ⇒ Object
- #redis_SUNIONSTORE(destination, *keys) ⇒ Object
Instance Method Details
#redis_SADD(key, member) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/redis/sets.rb', line 6 def redis_SADD key, member record = (@database[key] ||= Set.new) redis_t Set, record return false if record.include? member record.add member true end |
#redis_SCARD(key) ⇒ Object
35 36 37 |
# File 'lib/redis/sets.rb', line 35 def redis_SCARD key (@database[key] || []).size end |
#redis_SDIFF(*keys) ⇒ Object
79 80 81 |
# File 'lib/redis/sets.rb', line 79 def redis_SDIFF *keys keys.reduce(nil) { |memo, key| memo ? memo - (@database[key]||[]) : (@database[key]||Set.new) } end |
#redis_SDIFFSTORE(destination, *keys) ⇒ Object
83 84 85 |
# File 'lib/redis/sets.rb', line 83 def redis_SDIFFSTORE destination, *keys (@database[destination] = redis_SDIFF *keys).size end |
#redis_SINTER(*keys) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/redis/sets.rb', line 47 def redis_SINTER *keys keys.each { |key| redis_t NilClass, Array, Set, @database[key] } keys.reduce(nil) do |memo, key| memo ? memo & (@database[key]||[]) : (@database[key]||Set.new) end end |
#redis_SINTERSTORE(destination, *keys) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/redis/sets.rb', line 54 def redis_SINTERSTORE destination, *keys record = redis_SINTER *keys if record.empty? @database.delete destination else @database[destination] = record end record.size end |
#redis_SISMEMBER(key, member) ⇒ Object
39 40 41 |
# File 'lib/redis/sets.rb', line 39 def redis_SISMEMBER key, member (@database[key] || []).include? member end |
#redis_SMEMBERS(key) ⇒ Object
43 44 45 |
# File 'lib/redis/sets.rb', line 43 def redis_SMEMBERS key (@database[key] || []).to_a end |
#redis_SMOVE(source, destination, member) ⇒ Object
Type checks are only to make tests pass
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/redis/sets.rb', line 23 def redis_SMOVE source, destination, member source_record = @database[source] || Set.new dest_record = @database[destination] redis_t Set, source_record redis_t NilClass, Set, dest_record return false unless source_record.include? member (@database[destination] ||= Set.new).add member source_record.delete member @database.delete source if source_record.empty? return true end |
#redis_SPOP(key) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/redis/sets.rb', line 87 def redis_SPOP key set = @database[key] return nil unless set rec = rand set.size result = set.to_a[rec] set.delete result @database.delete key if set.empty? result end |
#redis_SRANDMEMBER(key) ⇒ Object
97 98 99 100 101 |
# File 'lib/redis/sets.rb', line 97 def redis_SRANDMEMBER key set = (@database[key] || []) return nil if set.empty? set.to_a[rand set.size] end |
#redis_SREM(key, member) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/redis/sets.rb', line 14 def redis_SREM key, member record = @database[key] || [] return false unless record.include? member record.delete member @database.delete key if record.empty? return true end |
#redis_SUNION(*keys) ⇒ Object
64 65 66 67 |
# File 'lib/redis/sets.rb', line 64 def redis_SUNION *keys keys.each { |key| redis_t NilClass, Array, Set, @database[key] } keys.reduce(Set.new) { |memo, key| memo | (@database[key]||[]) } end |
#redis_SUNIONSTORE(destination, *keys) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/redis/sets.rb', line 69 def redis_SUNIONSTORE destination, *keys record = redis_SUNION *keys if record.empty? @database.delete destination else @database[destination] = record end record.size end |