Module: Valkey::Commands::SortedSetCommands
- Included in:
- Valkey::Commands
- Defined in:
- lib/valkey/commands/sorted_set_commands.rb
Instance Method Summary collapse
-
#zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) ⇒ Boolean, ...
Add one or more members to a sorted set, or update the score for members that already exist.
-
#zcard(key) ⇒ Integer
Get the number of members in a sorted set.
-
#zincrby(key, increment, member) ⇒ Float
Increment the score of a member in a sorted set.
-
#zrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set.
-
#zrem(key, member) ⇒ Boolean, Integer
Remove one or more members from a sorted set.
-
#zrevrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set, with scores ordered from high to low.
-
#zscore(key, member) ⇒ Float
Get the score associated with the given member in a sorted set.
Instance Method Details
#zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) ⇒ Boolean, ...
Add one or more members to a sorted set, or update the score for members that already exist.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 53 def zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) command_args = [key] command_args << "NX" if nx command_args << "XX" if xx command_args << "LT" if lt command_args << "GT" if gt command_args << "CH" if ch command_args << "INCR" if incr if args.size == 1 && args[0].is_a?(Array) members_to_add = args[0] return 0 if members_to_add.empty? # Variadic: return float if INCR, integer if !INCR send_command(RequestType::Z_ADD, command_args + members_to_add.flatten, &(incr ? Utils::Floatify : nil)) elsif args.size == 2 # Single pair: return float if INCR, boolean if !INCR send_command(RequestType::Z_ADD, command_args + args.flatten.flatten, &(incr ? Utils::Floatify : Utils::Boolify)) else raise ArgumentError, "wrong number of arguments" end end |
#zcard(key) ⇒ Integer
Get the number of members in a sorted set.
14 15 16 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 14 def zcard(key) send_command(RequestType::Z_CARD, [key]) end |
#zincrby(key, increment, member) ⇒ Float
Increment the score of a member in a sorted set.
86 87 88 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 86 def zincrby(key, increment, member) send_command(RequestType::Z_INCR_BY, [key, increment, member], &Utils::Floatify) end |
#zrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set.
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 151 def zrank(key, member, withscore: false, with_score: withscore) args = [key, member] if with_score args << "WITHSCORE" block = Utils::FloatifyPair end send_command(RequestType::Z_RANK, args, &block) end |
#zrem(key, member) ⇒ Boolean, Integer
Remove one or more members from a sorted set.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 107 def zrem(key, member) if member.is_a?(Array) members_to_remove = member return 0 if members_to_remove.empty? end send_command(RequestType::Z_REM, [key, member].flatten) do |reply| if member.is_a? Array # Variadic: return integer reply else # Single argument: return boolean Utils::Boolify.call(reply) end end end |
#zrevrank(key, member, withscore: false, with_score: withscore) ⇒ Integer, [Integer, Float]
Determine the index of a member in a sorted set, with scores ordered from high to low.
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 178 def zrevrank(key, member, withscore: false, with_score: withscore) args = [key, member] if with_score args << "WITHSCORE" block = Utils::FloatifyPair end send_command(RequestType::Z_REV_RANK, args, &block) end |
#zscore(key, member) ⇒ Float
Get the score associated with the given member in a sorted set.
132 133 134 |
# File 'lib/valkey/commands/sorted_set_commands.rb', line 132 def zscore(key, member) send_command(RequestType::Z_SCORE, [key, member], &Utils::Floatify) end |