Module: Swagger::Commands::SortedSets

Included in:
Redis
Defined in:
lib/swagger/commands/sorted_sets.rb

Constant Summary collapse

KEY_TYPE =
'sorted_set'

Instance Method Summary collapse

Instance Method Details

#zadd(key, score, value) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/swagger/commands/sorted_sets.rb', line 7

def zadd(key, score, value)
  record = ResqueValue.find_or_initialize_by_key_and_key_type_and_value(
    key.to_s, KEY_TYPE, value)
  record.score = score
  record.save!
  record
end

#zcard(key) ⇒ Object



19
20
21
# File 'lib/swagger/commands/sorted_sets.rb', line 19

def zcard(key)
  ResqueValue.count :conditions => conditions(key)
end

#zrange(key, start, stop) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/swagger/commands/sorted_sets.rb', line 23

def zrange(key, start, stop)
  Helpers.select_values(
    'SELECT value FROM resque_values ' \
    "WHERE key = #{key} " \
    'ORDER BY score'
  )[start..stop]
end

#zrangebyscore(key, min, max, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/swagger/commands/sorted_sets.rb', line 31

def zrangebyscore(key, min, max, options = {})
  min = ResqueValue.minimum(:score, :conditions => conditions(key)) || 0 if min == '-inf'
  max = ResqueValue.maximum(:score, :conditions => conditions(key)) || 0 if max == '+inf'

  Helpers.select_values(
    [].tap do |sql|
      sql << 'SELECT value FROM resque_values'
      sql << "WHERE `key` = '#{key}'"
      sql << "AND score BETWEEN #{min} AND #{max}"
      sql << 'ORDER BY score'
      if limit = options[:limit]
        sql << "LIMIT #{limit.join(', ')}"
      end
    end.join(' ')
  )
end

#zrem(key, value) ⇒ Object



15
16
17
# File 'lib/swagger/commands/sorted_sets.rb', line 15

def zrem(key, value)
  ResqueValue.delete_all conditions(key).merge(:value => value)
end