Class: RedSet

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

Direct Known Subclasses

TimedRedSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ RedSet

Returns a new instance of RedSet.



91
92
93
# File 'lib/redness/red_set.rb', line 91

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

.add(key, member, options = {}) ⇒ Object



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

def self.add(key, member, options = {})
  redis.execute_with_uncertainty do
    redis.watch(key)

    if redis.zrank(key, member).nil?
      if options[:score] and options[:score].respond_to?(:call)
        score = options[:score].call.to_i
      elsif options[:score] && options[:score].respond_to?(:to_i)
        score = options[:score].to_i
      else
        score = redis.zcard(key).to_i
      end

      redis.multi_with_caution(false) do
        redis.zadd(key, score.to_s, member)
      end
    end
  end
ensure
  redis.execute_with_uncertainty(0) do
    redis.unwatch
  end
end

.cap(key, size) ⇒ Object



32
33
34
35
36
# File 'lib/redness/red_set.rb', line 32

def self.cap(key, size)
  redis.execute_with_uncertainty(0) do
    redis.zremrangebyrank(key, 0, -(size + 1))
  end
end

.count(key) ⇒ Object



79
80
81
82
83
# File 'lib/redness/red_set.rb', line 79

def self.count(key)
  redis.execute_with_uncertainty(0) do
    redis.zcard(key)
  end
end

.get(key, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/redness/red_set.rb', line 44

def self.get(key, options = {})
  lower_bound = options[:lower] || 0
  upper_bound = options[:upper] || -1
  with_scores = options[:with_scores] || false
  scoring     = options[:scoring] || lambda {|x| x}

  redis.execute_with_uncertainty([]) do
    results = redis.zrevrange(key, lower_bound, upper_bound, :with_scores => with_scores)
    if with_scores
      [].tap do |memo|
        if Red.client_version.first < 3
          results.each_slice(2) do |slice|
            memo << [slice[0].to_i, scoring.call(slice[1].to_i)]
          end
        else
          results.each do |member, score|
            memo << [member.to_i, scoring.call(score)]
          end
        end
      end
    else
      results.map(&:to_i)
    end
  end
end

.get_strings(key, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/redness/red_set.rb', line 70

def self.get_strings(key, options = {})
  lower_bound = options[:lower] || 0
  upper_bound = options[:upper] || -1

  redis.execute_with_uncertainty do
    redis.zrevrange(key, lower_bound, upper_bound)
  end
end

.redisObject



4
5
6
# File 'lib/redness/red_set.rb', line 4

def self.redis
  @redis ||= Red.new
end

.remove(key, member) ⇒ Object



38
39
40
41
42
# File 'lib/redness/red_set.rb', line 38

def self.remove(key, member)
  redis.execute_with_uncertainty do
    redis.zrem(key, member)
  end
end

.score(key, member) ⇒ Object



85
86
87
88
89
# File 'lib/redness/red_set.rb', line 85

def self.score(key, member)
  redis.execute_with_uncertainty(nil) do
    redis.zscore(key, member)
  end
end

Instance Method Details

#add(value, options = {}) ⇒ Object



95
96
97
# File 'lib/redness/red_set.rb', line 95

def add(value, options = {})
  self.class.add(@key, value, options)
end

#remove(value) ⇒ Object



103
104
105
# File 'lib/redness/red_set.rb', line 103

def remove(value)
  self.class.remove(@key, value)
end

#valueObject



99
100
101
# File 'lib/redness/red_set.rb', line 99

def value
  self.class.get(@key)
end