Module: Valkey::Commands::ListCommands
- Included in:
- Valkey::Commands
- Defined in:
- lib/valkey/commands/list_commands.rb
Overview
this module contains commands related to list data type.
Instance Method Summary collapse
-
#blmove(source, destination, where_source, where_destination, timeout: 0) ⇒ nil, String
Remove the first/last element in a list and append/prepend it to another list and return it, or block until one is available.
-
#blmpop(timeout, *keys, modifier: "LEFT", count: nil) ⇒ Array<String, Array<String, Float>>
Pops one or more elements from the first non-empty list key from the list of provided key names.
-
#blpop(*args) ⇒ nil, [String, String]
Remove and get the first element in a list, or block until one is available.
-
#brpop(*args) ⇒ nil, [String, String]
Remove and get the last element in a list, or block until one is available.
-
#brpoplpush(source, destination, timeout: 0) ⇒ nil, String
Pop a value from a list, push it to another list and return it; or block until one is available.
-
#lindex(key, index) ⇒ String
Get an element from a list by its index.
-
#linsert(key, where, pivot, value) ⇒ Integer
Insert an element before or after another element in a list.
-
#llen(key) ⇒ Integer
Get the length of a list.
-
#lmove(source, destination, where_source, where_destination) ⇒ nil, String
Remove the first/last element in a list, append/prepend it to another list and return it.
-
#lmpop(*keys, modifier: "LEFT", count: nil) ⇒ Array<String, Array<String, Float>>
Pops one or more elements from the first non-empty list key from the list of provided key names.
-
#lpop(key, count = nil) ⇒ nil, ...
Remove and get the first elements in a list.
-
#lpush(key, value) ⇒ Integer
Prepend one or more values to a list, creating the list if it doesn’t exist.
-
#lpushx(key, value) ⇒ Integer
Prepend a value to a list, only if the list exists.
-
#lrange(key, start, stop) ⇒ Array<String>
Get a range of elements from a list.
-
#lrem(key, count, value) ⇒ Integer
Remove elements from a list.
-
#lset(key, index, value) ⇒ String
Set the value of an element in a list by its index.
-
#ltrim(key, start, stop) ⇒ String
Trim a list to the specified range.
-
#rpop(key, count = nil) ⇒ nil, ...
Remove and get the last elements in a list.
-
#rpoplpush(source, destination) ⇒ nil, String
Remove the last element in a list, append it to another list and return it.
-
#rpush(key, value) ⇒ Integer
Append one or more values to a list, creating the list if it doesn’t exist.
-
#rpushx(key, value) ⇒ Integer
Append a value to a list, only if the list exists.
Instance Method Details
#blmove(source, destination, where_source, where_destination, timeout: 0) ⇒ nil, String
Remove the first/last element in a list and append/prepend it to another list and return it, or block until one is available.
59 60 61 62 63 64 |
# File 'lib/valkey/commands/list_commands.rb', line 59 def blmove(source, destination, where_source, where_destination, timeout: 0) where_source, where_destination = _normalize_move_wheres(where_source, where_destination) args = [:blmove, source, destination, where_source, where_destination, timeout] send_command(RequestType::BLMOVE, args) end |
#blmpop(timeout, *keys, modifier: "LEFT", count: nil) ⇒ Array<String, Array<String, Float>>
Pops one or more elements from the first non-empty list key from the list of provided key names. If lists are empty, blocks until timeout has passed.
213 214 215 216 217 218 219 220 |
# File 'lib/valkey/commands/list_commands.rb', line 213 def blmpop(timeout, *keys, modifier: "LEFT", count: nil) raise ArgumentError, "Pick either LEFT or RIGHT" unless %w[LEFT RIGHT].include?(modifier) args = [timeout, keys.size, *keys, modifier] args << "COUNT" << Integer(count) if count send_command(RequestType::BLMPOP, args) end |
#blpop(*args) ⇒ nil, [String, String]
Remove and get the first element in a list, or block until one is available.
158 159 160 |
# File 'lib/valkey/commands/list_commands.rb', line 158 def blpop(*args) _bpop(:blpop, args) end |
#brpop(*args) ⇒ nil, [String, String]
Remove and get the last element in a list, or block until one is available.
174 175 176 |
# File 'lib/valkey/commands/list_commands.rb', line 174 def brpop(*args) _bpop(RequestType::BRPOP, args.flatten) end |
#brpoplpush(source, destination, timeout: 0) ⇒ nil, String
Pop a value from a list, push it to another list and return it; or block until one is available.
189 190 191 192 |
# File 'lib/valkey/commands/list_commands.rb', line 189 def brpoplpush(source, destination, timeout: 0) args = [:brpoplpush, source, destination, timeout] send_blocking_command(RequestType::BRPOPLPUSH, args, timeout) end |
#lindex(key, index) ⇒ String
Get an element from a list by its index.
255 256 257 |
# File 'lib/valkey/commands/list_commands.rb', line 255 def lindex(key, index) send_command(RequestType::LINDEX, [key, Integer(index)]) end |
#linsert(key, where, pivot, value) ⇒ Integer
Insert an element before or after another element in a list.
267 268 269 |
# File 'lib/valkey/commands/list_commands.rb', line 267 def linsert(key, where, pivot, value) send_command(RequestType::LINSERT, [key, where, pivot, value]) end |
#llen(key) ⇒ Integer
Get the length of a list.
14 15 16 |
# File 'lib/valkey/commands/list_commands.rb', line 14 def llen(key) send_command(RequestType::LLEN, [key]) end |
#lmove(source, destination, where_source, where_destination) ⇒ nil, String
This command comes in place of the now deprecated RPOPLPUSH. Doing LMOVE RIGHT LEFT is equivalent.
Remove the first/last element in a list, append/prepend it to another list and return it.
31 32 33 34 35 |
# File 'lib/valkey/commands/list_commands.rb', line 31 def lmove(source, destination, where_source, where_destination) where_source, where_destination = _normalize_move_wheres(where_source, where_destination) send_command(RequestType::LMOVE, [source, destination, where_source, where_destination]) end |
#lmpop(*keys, modifier: "LEFT", count: nil) ⇒ Array<String, Array<String, Float>>
Pops one or more elements from the first non-empty list key from the list of provided key names.
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/valkey/commands/list_commands.rb', line 239 def lmpop(*keys, modifier: "LEFT", count: nil) raise ArgumentError, "Pick either LEFT or RIGHT" unless %w[LEFT RIGHT].include?(modifier) args = [keys.size, *keys, modifier] args << "COUNT" << Integer(count) if count # pp args send_command(RequestType::LMPOP, args) end |
#lpop(key, count = nil) ⇒ nil, ...
Remove and get the first elements in a list.
111 112 113 114 115 |
# File 'lib/valkey/commands/list_commands.rb', line 111 def lpop(key, count = nil) args = [key] args << Integer(count) if count send_command(RequestType::LPOP, args) end |
#lpush(key, value) ⇒ Integer
Prepend one or more values to a list, creating the list if it doesn’t exist
71 72 73 |
# File 'lib/valkey/commands/list_commands.rb', line 71 def lpush(key, value) send_command(RequestType::LPUSH, [key, *value]) end |
#lpushx(key, value) ⇒ Integer
Prepend a value to a list, only if the list exists.
80 81 82 |
# File 'lib/valkey/commands/list_commands.rb', line 80 def lpushx(key, value) send_command(RequestType::LPUSHX, [key, value]) end |
#lrange(key, start, stop) ⇒ Array<String>
Get a range of elements from a list.
277 278 279 |
# File 'lib/valkey/commands/list_commands.rb', line 277 def lrange(key, start, stop) send_command(RequestType::LRANGE, [key, Integer(start), Integer(stop)]) end |
#lrem(key, count, value) ⇒ Integer
Remove elements from a list.
290 291 292 |
# File 'lib/valkey/commands/list_commands.rb', line 290 def lrem(key, count, value) send_command(RequestType::LREM, [key, Integer(count), value]) end |
#lset(key, index, value) ⇒ String
Set the value of an element in a list by its index.
300 301 302 |
# File 'lib/valkey/commands/list_commands.rb', line 300 def lset(key, index, value) send_command(RequestType::LSET, [key, Integer(index), value]) end |
#ltrim(key, start, stop) ⇒ String
Trim a list to the specified range.
310 311 312 |
# File 'lib/valkey/commands/list_commands.rb', line 310 def ltrim(key, start, stop) send_command(RequestType::LTRIM, [key, Integer(start), Integer(stop)]) end |
#rpop(key, count = nil) ⇒ nil, ...
Remove and get the last elements in a list.
122 123 124 125 126 |
# File 'lib/valkey/commands/list_commands.rb', line 122 def rpop(key, count = nil) args = [key] args << Integer(count) if count send_command(RequestType::RPOP, args) end |
#rpoplpush(source, destination) ⇒ nil, String
Remove the last element in a list, append it to another list and return it.
133 134 135 |
# File 'lib/valkey/commands/list_commands.rb', line 133 def rpoplpush(source, destination) send_command(RequestType::RPOPLPUSH, [source, destination]) end |
#rpush(key, value) ⇒ Integer
Append one or more values to a list, creating the list if it doesn’t exist
89 90 91 92 93 94 95 |
# File 'lib/valkey/commands/list_commands.rb', line 89 def rpush(key, value) value = [value] unless value.is_a?(Array) args = [key] + value send_command(RequestType::RPUSH, args) end |
#rpushx(key, value) ⇒ Integer
Append a value to a list, only if the list exists.
102 103 104 |
# File 'lib/valkey/commands/list_commands.rb', line 102 def rpushx(key, value) send_command(RequestType::RPUSHX, [key, value]) end |