Module: Valkey::Commands::GenericCommands
- Included in:
- Valkey::Commands
- Defined in:
- lib/valkey/commands/generic_commands.rb
Overview
this module contains generic commands that are not specific to any data type
Instance Method Summary collapse
- #_scan(command, cursor, args, match: nil, count: nil, type: nil, &block) ⇒ Object
-
#call(*argv, **kwargs) ⇒ Object
Send any command as plain arguments and get the raw reply back, with no per-command method needed.
-
#call_v(argv) ⇒ Object
Send any command as a single Array of arguments and get the raw reply back.
-
#copy(source, destination, db: nil, replace: false) ⇒ Boolean
Copy a value from one key to another.
-
#del(*keys) ⇒ Integer
Delete one or more keys.
-
#dump(key) ⇒ String
Return a serialized version of the value stored at a key.
-
#exists(*keys) ⇒ Integer
Determine how many of the keys exists.
-
#exists?(*keys) ⇒ Boolean
Determine if any of the keys exists.
-
#expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set a key's time to live in seconds.
-
#expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set the expiration for a key as a UNIX timestamp.
-
#expiretime(key) ⇒ Integer
Get a key's expiry time specified as number of seconds from UNIX Epoch.
-
#keys(pattern = "*") ⇒ Array<String>
Find all keys matching the given pattern.
-
#migrate(key, options) ⇒ String
Transfer a key from the connected instance to another instance.
-
#move(key, db) ⇒ Boolean
Move a key to another database.
- #object(subcommand, *args) ⇒ Object
-
#persist(key) ⇒ Boolean
Remove the expiration from a key.
-
#pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set a key's time to live in milliseconds.
-
#pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set the expiration for a key as number of milliseconds from UNIX Epoch.
-
#pexpiretime(key) ⇒ Integer
Get a key's expiry time specified as number of milliseconds from UNIX Epoch.
-
#pttl(key) ⇒ Integer
Get the time to live (in milliseconds) for a key.
-
#randomkey ⇒ String
Return a random key from the keyspace.
-
#rename(old_name, new_name) ⇒ String
Rename a key.
-
#renamenx(old_name, new_name) ⇒ Boolean
Rename a key, only if the new key does not exist.
-
#restore(key, ttl, serialized_value, replace: nil) ⇒ String
Create a key using the serialized value, previously obtained using DUMP.
-
#scan(cursor, **options) ⇒ String+
Scan the keyspace.
-
#scan_each(**options, &block) ⇒ Enumerator
Scan the keyspace.
-
#sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil) ⇒ Array<String>, ...
Sort the elements in a list, set or sorted set.
- #touch(*keys) ⇒ Object
-
#ttl(key) ⇒ Integer
Get the time to live (in seconds) for a key.
-
#type(key) ⇒ String
Determine the type stored at key.
-
#unlink(*keys) ⇒ Integer
Unlink one or more keys.
-
#wait(numreplicas, timeout) ⇒ Integer
Block until all the previous write commands are successfully transferred and acknowledged by at least the specified number of replicas.
-
#waitaof(numlocal, numreplicas, timeout) ⇒ Array<Integer>
Block until all previous write commands are fsynced to the AOF of the local server and/or at least the specified number of replicas.
Instance Method Details
#_scan(command, cursor, args, match: nil, count: nil, type: nil, &block) ⇒ Object
521 522 523 524 525 526 527 528 529 530 |
# File 'lib/valkey/commands/generic_commands.rb', line 521 def _scan(command, cursor, args, match: nil, count: nil, type: nil, &block) # SSCAN/ZSCAN/HSCAN already prepend the key to +args+. args << cursor args << "MATCH" << match if match args << "COUNT" << Integer(count) if count args << "TYPE" << type if type send_command(command, args, &block) end |
#call(*argv, **kwargs) ⇒ Object
Send any command as plain arguments and get the raw reply back, with no
per-command method needed. Escape hatch for commands without a dedicated
method yet, matching redis-client's #call.
558 559 560 |
# File 'lib/valkey/commands/generic_commands.rb', line 558 def call(*argv, **kwargs) send_command(RequestType::CUSTOM_COMMAND, flatten_call_args(argv).concat(call_flags(kwargs))) end |
#call_v(argv) ⇒ Object
Send any command as a single Array of arguments and get the raw reply back.
Same as #call but takes the whole command as one Array instead of splatted
args, useful when the command is built dynamically. Matches redis-client's
#call_v — no keyword flags.
574 575 576 |
# File 'lib/valkey/commands/generic_commands.rb', line 574 def call_v(argv) send_command(RequestType::CUSTOM_COMMAND, flatten_call_args(argv)) end |
#copy(source, destination, db: nil, replace: false) ⇒ Boolean
Copy a value from one key to another.
376 377 378 379 380 381 382 |
# File 'lib/valkey/commands/generic_commands.rb', line 376 def copy(source, destination, db: nil, replace: false) args = [source, destination] args << "DB" << db if db args << "REPLACE" if replace send_command(RequestType::COPY, args) end |
#del(*keys) ⇒ Integer
Delete one or more keys.
297 298 299 300 301 302 |
# File 'lib/valkey/commands/generic_commands.rb', line 297 def del(*keys) keys.flatten!(1) return 0 if keys.empty? send_command(RequestType::DEL, keys) end |
#dump(key) ⇒ String
Return a serialized version of the value stored at a key.
213 214 215 |
# File 'lib/valkey/commands/generic_commands.rb', line 213 def dump(key) send_command(RequestType::DUMP, [key]) end |
#exists(*keys) ⇒ Integer
Determine how many of the keys exists.
316 317 318 |
# File 'lib/valkey/commands/generic_commands.rb', line 316 def exists(*keys) send_command(RequestType::EXISTS, keys.flatten) end |
#exists?(*keys) ⇒ Boolean
Determine if any of the keys exists.
324 325 326 |
# File 'lib/valkey/commands/generic_commands.rb', line 324 def exists?(*keys) send_command(RequestType::EXISTS, keys.flatten, &:positive?) end |
#expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set a key's time to live in seconds.
92 93 94 95 96 97 98 99 100 |
# File 'lib/valkey/commands/generic_commands.rb', line 92 def expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil) args = [key, Integer(seconds)] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt send_command(RequestType::EXPIRE, args) end |
#expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set the expiration for a key as a UNIX timestamp.
112 113 114 115 116 117 118 119 120 |
# File 'lib/valkey/commands/generic_commands.rb', line 112 def expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil) args = [key, Integer(unix_time)] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt send_command(RequestType::EXPIRE_AT, args) end |
#expiretime(key) ⇒ Integer
Get a key's expiry time specified as number of seconds from UNIX Epoch
126 127 128 |
# File 'lib/valkey/commands/generic_commands.rb', line 126 def expiretime(key) send_command(RequestType::EXPIRE_TIME, [key]) end |
#keys(pattern = "*") ⇒ Array<String>
Find all keys matching the given pattern.
283 284 285 286 287 288 289 290 291 |
# File 'lib/valkey/commands/generic_commands.rb', line 283 def keys(pattern = "*") send_command(RequestType::KEYS, [pattern]) do |reply| if reply.is_a?(String) reply.split else reply end end end |
#migrate(key, options) ⇒ String
Transfer a key from the connected instance to another instance.
256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/valkey/commands/generic_commands.rb', line 256 def migrate(key, ) args = [] args << ([:host] || raise(ArgumentError, ':host not specified')) args << ([:port] || raise(ArgumentError, ':port not specified')).to_s args << (key.is_a?(String) ? key : '') args << ([:db] || 0).to_s args << ([:timeout] || 0).to_s args << 'COPY' if [:copy] args << 'REPLACE' if [:replace] args += ['KEYS', *key] if key.is_a?(Array) send_command(RequestType::MIGRATE, args) end |
#move(key, db) ⇒ Boolean
Move a key to another database.
347 348 349 |
# File 'lib/valkey/commands/generic_commands.rb', line 347 def move(key, db) send_command(RequestType::MOVE, [key, db]) end |
#object(subcommand, *args) ⇒ Object
384 385 386 387 388 389 390 391 392 393 |
# File 'lib/valkey/commands/generic_commands.rb', line 384 def object(subcommand, *args) map = { refcount: RequestType::OBJECT_REF_COUNT, encoding: RequestType::OBJECT_ENCODING, idletime: RequestType::OBJECT_IDLE_TIME, freq: RequestType::OBJECT_FREQ } send_command(map[subcommand.to_sym], args.flatten) end |
#persist(key) ⇒ Boolean
Remove the expiration from a key.
78 79 80 |
# File 'lib/valkey/commands/generic_commands.rb', line 78 def persist(key) send_command(RequestType::PERSIST, [key]) end |
#pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set a key's time to live in milliseconds.
156 157 158 159 160 161 162 163 164 |
# File 'lib/valkey/commands/generic_commands.rb', line 156 def pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil) args = [key, Integer(milliseconds)] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt send_command(RequestType::PEXPIRE, args) end |
#pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set the expiration for a key as number of milliseconds from UNIX Epoch.
176 177 178 179 180 181 182 183 184 |
# File 'lib/valkey/commands/generic_commands.rb', line 176 def pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil) args = [key, Integer(ms_unix_time)] args << "NX" if nx args << "XX" if xx args << "GT" if gt args << "LT" if lt send_command(RequestType::PEXPIRE_AT, args) end |
#pexpiretime(key) ⇒ Integer
Get a key's expiry time specified as number of milliseconds from UNIX Epoch
190 191 192 |
# File 'lib/valkey/commands/generic_commands.rb', line 190 def pexpiretime(key) send_command(RequestType::PEXPIRE_TIME, [key]) end |
#pttl(key) ⇒ Integer
Get the time to live (in milliseconds) for a key.
In valkey 2.6 or older the command returns -1 if the key does not exist or if the key exist but has no associated expire.
Starting with valkey 2.8 the return value in case of error changed:
- The command returns -2 if the key does not exist.
- The command returns -1 if the key exists but has no associated expire.
205 206 207 |
# File 'lib/valkey/commands/generic_commands.rb', line 205 def pttl(key) send_command(RequestType::PTTL, [key]) end |
#randomkey ⇒ String
Return a random key from the keyspace.
398 399 400 |
# File 'lib/valkey/commands/generic_commands.rb', line 398 def randomkey send_command(RequestType::RANDOM_KEY) end |
#rename(old_name, new_name) ⇒ String
Rename a key. If the new key already exists it is overwritten.
407 408 409 |
# File 'lib/valkey/commands/generic_commands.rb', line 407 def rename(old_name, new_name) send_command(RequestType::RENAME, [old_name, new_name]) end |
#renamenx(old_name, new_name) ⇒ Boolean
Rename a key, only if the new key does not exist.
416 417 418 |
# File 'lib/valkey/commands/generic_commands.rb', line 416 def renamenx(old_name, new_name) send_command(RequestType::RENAME_NX, [old_name, new_name]) end |
#restore(key, ttl, serialized_value, replace: nil) ⇒ String
Create a key using the serialized value, previously obtained using DUMP.
226 227 228 229 230 231 |
# File 'lib/valkey/commands/generic_commands.rb', line 226 def restore(key, ttl, serialized_value, replace: nil) args = [key, ttl, serialized_value] args << 'REPLACE' if replace send_command(RequestType::RESTORE, args) end |
#scan(cursor, **options) ⇒ String+
Standalone mode only. glide-core has no defined default route for SCAN in cluster mode, so each call may land on a different node with no cursor continuity between them — results are undefined (missed or duplicated keys) rather than merely partial.
Scan the keyspace
35 36 37 |
# File 'lib/valkey/commands/generic_commands.rb', line 35 def scan(cursor, **) _scan(RequestType::SCAN, cursor, [], **) end |
#scan_each(**options, &block) ⇒ Enumerator
Standalone mode only. Built on #scan, which has undefined routing behavior in cluster mode (see its note).
Scan the keyspace
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/valkey/commands/generic_commands.rb', line 63 def scan_each(**, &block) return to_enum(:scan_each, **) unless block_given? cursor = 0 loop do cursor, keys = scan(cursor, **) keys.each(&block) break if cursor == "0" end end |
#sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil) ⇒ Array<String>, ...
Sort the elements in a list, set or sorted set.
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/valkey/commands/generic_commands.rb', line 445 def sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil) args = [key] args << "BY" << by if by if limit args << "LIMIT" args.concat(limit) end get = Array(get) get.each do |item| args << "GET" << item end args.concat(order.split) if order args << "STORE" << store if store send_command(RequestType::SORT, args) do |reply| if get.size > 1 && !store reply.each_slice(get.size).to_a if reply else reply end end end |
#touch(*keys) ⇒ Object
471 472 473 |
# File 'lib/valkey/commands/generic_commands.rb', line 471 def touch(*keys) send_command(RequestType::TOUCH, keys.flatten) end |
#ttl(key) ⇒ Integer
Get the time to live (in seconds) for a key.
In valkey 2.6 or older the command returns -1 if the key does not exist or if the key exist but has no associated expire.
Starting with valkey 2.8 the return value in case of error changed:
- The command returns -2 if the key does not exist.
- The command returns -1 if the key exists but has no associated expire.
142 143 144 |
# File 'lib/valkey/commands/generic_commands.rb', line 142 def ttl(key) send_command(RequestType::TTL, [key]) end |
#type(key) ⇒ String
Determine the type stored at key.
517 518 519 |
# File 'lib/valkey/commands/generic_commands.rb', line 517 def type(key) send_command(RequestType::TYPE, [key]) end |
#unlink(*keys) ⇒ Integer
Unlink one or more keys.
308 309 310 |
# File 'lib/valkey/commands/generic_commands.rb', line 308 def unlink(*keys) send_command(RequestType::UNLINK, keys.flatten) end |
#wait(numreplicas, timeout) ⇒ Integer
Block until all the previous write commands are successfully transferred and acknowledged by at least the specified number of replicas.
487 488 489 |
# File 'lib/valkey/commands/generic_commands.rb', line 487 def wait(numreplicas, timeout) send_command(RequestType::WAIT, [numreplicas.to_s, timeout.to_s]) end |
#waitaof(numlocal, numreplicas, timeout) ⇒ Array<Integer>
Block until all previous write commands are fsynced to the AOF of the local server and/or at least the specified number of replicas.
509 510 511 |
# File 'lib/valkey/commands/generic_commands.rb', line 509 def waitaof(numlocal, numreplicas, timeout) send_command(RequestType::WAIT_AOF, [numlocal.to_s, numreplicas.to_s, timeout.to_s]) end |