Module: Protocol::Redis::Methods::SortedSets

Defined in:
lib/protocol/redis/methods/sorted_sets.rb

Instance Method Summary collapse

Instance Method Details

#zadd(key, score, member, *others, update: nil, change: false, increment: false) ⇒ Object

Add one or more members to a sorted set, or update its score if it already exists. O(log(N)) for each item added, where N is the number of elements in the sorted set.

Parameters:

  • key (Key)
  • score (Double)
  • member (String)
  • others (Array)

    an array of score, member elements.

  • update (Boolean, nil) (defaults to: nil)

    If true, only update elements that already exist (never add elements). If false, don’t update existing elements (only add new elements).

  • change (Boolean) (defaults to: false)

    Modify the return value from the number of new elements added, to the total number of elements changed; changed elements are new elements added and elements already existing for which the score was updated.

  • increment (Boolean) (defaults to: false)

    When this option is specified ZADD acts like ZINCRBY; only one score-element pair can be specified in this mode.

See Also:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 40

def zadd(key, score, member, *others, update: nil, change: false, increment: false)
  arguments = ["ZADD", key]
  
  if update == true
    arguments << "XX"
  elsif update == false
    arguments << "NX"
  end
  
  arguments << "CH" if change
  arguments << "INCR" if increment
  
  arguments << score << member
  arguments.concat(others)
  
  call(*arguments)
end

#zrange(key, start, stop, with_scores: false) ⇒ Object

Return a range of members in a sorted set, by index. O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

Parameters:

  • key (Key)
  • start (Integer)
  • stop (Integer)
  • with_scores (Boolean) (defaults to: false)

    Return the scores of the elements together with the elements.

See Also:



64
65
66
67
68
69
70
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 64

def zrange(key, start, stop, with_scores: false)
  arguments = [start, stop]
  
  arguments << "WITHSCORES" if with_scores
  
  call("ZRANGE", key, *arguments)
end

#zrem(key, member) ⇒ Object

Remove one or more members from a sorted set. O(M*log(N)) with N being the number of elements in the sorted set and M the number of elements to be removed.

Parameters:

  • key (Key)
  • member (String)

See Also:



76
77
78
# File 'lib/protocol/redis/methods/sorted_sets.rb', line 76

def zrem(key, member)
  call("ZREM", key, member)
end