Class: RedSetUnion

Inherits:
Object
  • Object
show all
Defined in:
lib/redness/red_set_union.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*keys) ⇒ RedSetUnion

Returns a new instance of RedSetUnion.



4
5
6
7
8
9
# File 'lib/redness/red_set_union.rb', line 4

def initialize(*keys)
  self.keys = keys
  self.redis = Red.new

  @temporary_key = "tmp:red_set_union"
end

Instance Attribute Details

#keysObject

Returns the value of attribute keys.



2
3
4
# File 'lib/redness/red_set_union.rb', line 2

def keys
  @keys
end

#redisObject

Returns the value of attribute redis.



2
3
4
# File 'lib/redness/red_set_union.rb', line 2

def redis
  @redis
end

Instance Method Details

#get(options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redness/red_set_union.rb', line 11

def get(options = {})
  lower_bound = options[:lower] || 0
  upper_bound = options[:upper] || -1

  redis.execute_with_uncertainty([]) do
    results = redis.multi_with_caution do
      redis.zunionstore(@temporary_key, keys)
      redis.zrevrange(@temporary_key, lower_bound, upper_bound)
      redis.del(@temporary_key)
    end

    if results.present?
      results[1].flatten.compact.uniq.map(&:to_i)
    else
      []
    end
  end
end