Module: Valkey::Commands::ServerCommands
- Included in:
- Valkey::Commands
- Defined in:
- lib/valkey/commands/server_commands.rb
Overview
this module contains commands related to server management.
Instance Method Summary collapse
-
#acl(subcommand, *args) ⇒ Object
Control ACL configuration (convenience method).
-
#acl_cat(category = nil) ⇒ Array<String>
List the ACL categories or the commands inside a category.
-
#acl_deluser(*usernames) ⇒ Integer
Remove the specified ACL users.
-
#acl_dryrun(username, command, *args) ⇒ String
Simulate the execution of a command by a user without actually running it.
-
#acl_genpass(bits = nil) ⇒ String
Generate a random password.
-
#acl_getuser(username) ⇒ Array?
Get the rules for a specific ACL user.
-
#acl_list ⇒ Array<String>
List the current ACL rules in ACL config file format.
-
#acl_load ⇒ String
Reload the ACL rules from the configured ACL file.
-
#acl_log(count_or_reset = nil) ⇒ Array<Hash>, String
List latest ACL security events.
-
#acl_save ⇒ String
Save the current ACL rules to the configured ACL file.
-
#acl_setuser(username, *rules) ⇒ String
Modify or create ACL rules for a user.
-
#acl_users ⇒ Array<String>
List all configured ACL users.
-
#acl_whoami ⇒ String
Return the username of the current connection.
-
#bgrewriteaof(route: nil) ⇒ String
Asynchronously rewrite the append-only file.
-
#bgsave(route: nil) ⇒ String
Asynchronously save the dataset to disk.
-
#command(subcommand, *args) ⇒ Object
Send a generic COMMAND subcommand.
-
#command_ ⇒ Array
Return details about every Redis command.
-
#command_count ⇒ Integer
Return the total number of commands in the server.
-
#command_docs(*commands) ⇒ Array
Return documentary information about one or more commands.
-
#command_get_keys(*command) ⇒ Array
Extract keys from a full Redis command.
-
#command_get_keys_and_flags(*command) ⇒ Array
Extract keys and their flags from a full Redis command.
-
#command_info(*commands) ⇒ Array
Return information about one or more commands.
-
#command_list(filterby: nil, aclcat: nil, pattern: nil) ⇒ Array<String>
Return an array of command names.
-
#config(action, *args) ⇒ String, Hash
Get or set server configuration parameters.
-
#config_get(*args, route: nil) ⇒ Hash
Get server configuration parameters.
-
#config_resetstat(route: nil) ⇒ String
Reset the server's statistics.
-
#config_rewrite(route: nil) ⇒ String
Rewrite the server configuration file.
-
#config_set(*args, route: nil) ⇒ String
Set server configuration parameters.
-
#dbsize(route: nil) ⇒ Integer
Return the number of keys in the selected database.
-
#failover(to: nil, force: false, abort: false, timeout: nil) ⇒ String
Perform a manual failover of a master to one of its replicas.
-
#flushall(options = nil, route: nil) ⇒ String
Remove all keys from all databases.
-
#flushdb(options = nil, route: nil) ⇒ String
Remove all keys from the current database.
-
#info(cmd = nil, route: nil) ⇒ Hash
Get information and statistics about the server.
-
#lastsave(route: nil) ⇒ Integer
Get the UNIX time stamp of the last successful save to disk.
-
#latency_doctor ⇒ String
Return a human-readable latency analysis report.
-
#latency_graph(event) ⇒ String
Return an ASCII-art graph of the latency samples for the specified event.
-
#latency_histogram(*commands) ⇒ Array
Return a latency histogram for the specified commands.
-
#latency_history(event) ⇒ Array
Return the latency time series for the specified event.
-
#latency_latest ⇒ Array
Return the latest latency samples for all events.
-
#latency_reset(*events) ⇒ Integer
Reset latency data for all events or specific events.
-
#lolwut(version = nil, route: nil) ⇒ String
Display some computer art and the Valkey version.
-
#memory_doctor ⇒ String
Return a human-readable memory problems report.
-
#memory_malloc_stats ⇒ String
Return memory allocator statistics.
-
#memory_purge ⇒ String
Ask the allocator to release memory back to the operating system.
-
#memory_stats ⇒ Hash
Return memory usage statistics.
-
#memory_usage(key, samples: nil) ⇒ Integer?
Return the memory usage in bytes of a key and its value.
-
#psync(replication_id, offset) ⇒ String
Internal command used for replication (partial resynchronization).
-
#replconf(*args) ⇒ String, Array
Internal command used for replication configuration.
-
#replicaof(host, port) ⇒ String
Make the server a replica of another instance, or promote it as master.
-
#restore_asking(key, ttl, serialized_value, replace: false, absttl: false, idletime: nil, freq: nil) ⇒ String
Internal command used in cluster mode for key migration with ASKING flag.
-
#role ⇒ Array
Return the role of the instance in the context of replication.
-
#save(route: nil) ⇒ String
Synchronously save the dataset to disk.
-
#slaveof(host, port) ⇒ Object
Make the server a slave of another instance, or promote it as master.
-
#slowlog(subcommand, length = nil) ⇒ Array<Array>, ...
Interact with the slowlog (get, len, reset).
-
#swapdb(index1, index2) ⇒ String
Swap two databases.
-
#sync ⇒ Object
Internal command used for replication.
-
#time(route: nil) ⇒ Array<String>, Hash{String => Array<String>}
Return the server time.
Instance Method Details
#acl(subcommand, *args) ⇒ Object
Control ACL configuration (convenience method).
483 484 485 486 |
# File 'lib/valkey/commands/server_commands.rb', line 483 def acl(subcommand, *args) subcommand = subcommand.to_s.downcase send("acl_#{subcommand}", *args) end |
#acl_cat(category = nil) ⇒ Array<String>
List the ACL categories or the commands inside a category.
258 259 260 261 |
# File 'lib/valkey/commands/server_commands.rb', line 258 def acl_cat(category = nil) args = category ? [category] : [] send_command(RequestType::ACL_CAT, args) end |
#acl_deluser(*usernames) ⇒ Integer
Remove the specified ACL users.
276 277 278 |
# File 'lib/valkey/commands/server_commands.rb', line 276 def acl_deluser(*usernames) send_command(RequestType::ACL_DEL_USER, usernames) end |
#acl_dryrun(username, command, *args) ⇒ String
Simulate the execution of a command by a user without actually running it.
295 296 297 298 |
# File 'lib/valkey/commands/server_commands.rb', line 295 def acl_dryrun(username, command, *args) command_args = [username, command] + args send_command(RequestType::ACL_DRY_RUN, command_args) end |
#acl_genpass(bits = nil) ⇒ String
Generate a random password.
313 314 315 316 |
# File 'lib/valkey/commands/server_commands.rb', line 313 def acl_genpass(bits = nil) args = bits ? [bits] : [] send_command(RequestType::ACL_GEN_PASS, args) end |
#acl_getuser(username) ⇒ Array?
Get the rules for a specific ACL user.
328 329 330 |
# File 'lib/valkey/commands/server_commands.rb', line 328 def acl_getuser(username) send_command(RequestType::ACL_GET_USER, [username]) end |
#acl_list ⇒ Array<String>
List the current ACL rules in ACL config file format.
341 342 343 |
# File 'lib/valkey/commands/server_commands.rb', line 341 def acl_list send_command(RequestType::ACL_LIST) end |
#acl_load ⇒ String
Reload the ACL rules from the configured ACL file.
354 355 356 |
# File 'lib/valkey/commands/server_commands.rb', line 354 def acl_load send_command(RequestType::ACL_LOAD) end |
#acl_log(count_or_reset = nil) ⇒ Array<Hash>, String
List latest ACL security events.
374 375 376 377 |
# File 'lib/valkey/commands/server_commands.rb', line 374 def acl_log(count_or_reset = nil) args = count_or_reset ? [count_or_reset] : [] send_command(RequestType::ACL_LOG, args) end |
#acl_save ⇒ String
Save the current ACL rules to the configured ACL file.
388 389 390 |
# File 'lib/valkey/commands/server_commands.rb', line 388 def acl_save send_command(RequestType::ACL_SAVE) end |
#acl_setuser(username, *rules) ⇒ String
Modify or create ACL rules for a user.
409 410 411 412 |
# File 'lib/valkey/commands/server_commands.rb', line 409 def acl_setuser(username, *rules) command_args = [username] + rules send_command(RequestType::ACL_SET_USER, command_args) end |
#acl_users ⇒ Array<String>
List all configured ACL users.
423 424 425 |
# File 'lib/valkey/commands/server_commands.rb', line 423 def acl_users send_command(RequestType::ACL_USERS) end |
#acl_whoami ⇒ String
Return the username of the current connection.
436 437 438 |
# File 'lib/valkey/commands/server_commands.rb', line 436 def acl_whoami send_command(RequestType::ACL_WHOAMI) end |
#bgrewriteaof(route: nil) ⇒ String
Asynchronously rewrite the append-only file.
14 15 16 |
# File 'lib/valkey/commands/server_commands.rb', line 14 def bgrewriteaof(route: nil) send_command(RequestType::BG_REWRITE_AOF, [], route: route) end |
#bgsave(route: nil) ⇒ String
Asynchronously save the dataset to disk.
22 23 24 |
# File 'lib/valkey/commands/server_commands.rb', line 22 def bgsave(route: nil) send_command(RequestType::BG_SAVE, [], route: route) end |
#command(subcommand, *args) ⇒ Object
Send a generic COMMAND subcommand.
843 844 845 |
# File 'lib/valkey/commands/server_commands.rb', line 843 def command(subcommand, *args) send("command_#{subcommand.to_s.downcase}", *args) end |
#command_ ⇒ Array
Return details about every Redis command.
856 857 858 |
# File 'lib/valkey/commands/server_commands.rb', line 856 def command_ send_command(RequestType::COMMAND_) end |
#command_count ⇒ Integer
Return the total number of commands in the server.
869 870 871 |
# File 'lib/valkey/commands/server_commands.rb', line 869 def command_count send_command(RequestType::COMMAND_COUNT) end |
#command_docs(*commands) ⇒ Array
Return documentary information about one or more commands.
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
# File 'lib/valkey/commands/server_commands.rb', line 886 def command_docs(*commands) args = commands.empty? ? [] : commands send_command(RequestType::COMMAND_DOCS, args) do |reply| if reply.is_a?(Array) # Convert array of arrays to array of hashes reply.map do |cmd_doc| if cmd_doc.is_a?(Array) Hash[*cmd_doc] else cmd_doc end end else reply end end end |
#command_get_keys(*command) ⇒ Array
Extract keys from a full Redis command.
916 917 918 |
# File 'lib/valkey/commands/server_commands.rb', line 916 def command_get_keys(*command) send_command(RequestType::COMMAND_GET_KEYS, command) end |
#command_get_keys_and_flags(*command) ⇒ Array
Extract keys and their flags from a full Redis command.
930 931 932 |
# File 'lib/valkey/commands/server_commands.rb', line 930 def command_get_keys_and_flags(*command) send_command(RequestType::COMMAND_GET_KEYS_AND_FLAGS, command) end |
#command_info(*commands) ⇒ Array
Return information about one or more commands.
947 948 949 950 |
# File 'lib/valkey/commands/server_commands.rb', line 947 def command_info(*commands) args = commands.empty? ? [] : commands send_command(RequestType::COMMAND_INFO, args) end |
#command_list(filterby: nil, aclcat: nil, pattern: nil) ⇒ Array<String>
Return an array of command names.
971 972 973 974 975 976 977 978 979 |
# File 'lib/valkey/commands/server_commands.rb', line 971 def command_list(filterby: nil, aclcat: nil, pattern: nil) args = [] if aclcat args << "FILTERBY" << "ACLCAT" << aclcat elsif filterby && pattern args << "FILTERBY" << filterby << pattern end send_command(RequestType::COMMAND_LIST, args) end |
#config(action, *args) ⇒ String, Hash
Get or set server configuration parameters.
31 32 33 |
# File 'lib/valkey/commands/server_commands.rb', line 31 def config(action, *args) send("config_#{action.to_s.downcase}", *args) end |
#config_get(*args, route: nil) ⇒ Hash
Get server configuration parameters.
Sends the CONFIG GET command with the given arguments.
50 51 52 53 54 55 56 57 58 |
# File 'lib/valkey/commands/server_commands.rb', line 50 def config_get(*args, route: nil) send_command(RequestType::CONFIG_GET, args, route: route) do |reply| if reply.is_a?(Array) Hash[*reply] else reply end end end |
#config_resetstat(route: nil) ⇒ String
Reset the server's statistics.
Sends the CONFIG RESETSTAT command.
83 84 85 |
# File 'lib/valkey/commands/server_commands.rb', line 83 def config_resetstat(route: nil) send_command(RequestType::CONFIG_RESET_STAT, [], route: route) end |
#config_rewrite(route: nil) ⇒ String
Rewrite the server configuration file.
Sends the CONFIG REWRITE command.
96 97 98 |
# File 'lib/valkey/commands/server_commands.rb', line 96 def config_rewrite(route: nil) send_command(RequestType::CONFIG_REWRITE, [], route: route) end |
#config_set(*args, route: nil) ⇒ String
Set server configuration parameters.
Sends the CONFIG SET command with the given key-value pairs.
70 71 72 |
# File 'lib/valkey/commands/server_commands.rb', line 70 def config_set(*args, route: nil) send_command(RequestType::CONFIG_SET, args, route: route) end |
#dbsize(route: nil) ⇒ Integer
Return the number of keys in the selected database.
105 106 107 |
# File 'lib/valkey/commands/server_commands.rb', line 105 def dbsize(route: nil) send_command(RequestType::DB_SIZE, [], route: route) end |
#failover(to: nil, force: false, abort: false, timeout: nil) ⇒ String
Perform a manual failover of a master to one of its replicas.
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/valkey/commands/server_commands.rb', line 514 def failover(to: nil, force: false, abort: false, timeout: nil) args = [] if abort # ABORT is mutually exclusive: cancels an ongoing failover and # ignores any other arguments (matches the core GLIDE clients). args << "ABORT" else if to host, port = to.split args << "TO" << host << port # FORCE is only valid in combination with TO. args << "FORCE" if force end args << "TIMEOUT" << timeout.to_s if timeout end send_command(RequestType::FAIL_OVER, args) end |
#flushall(options = nil, route: nil) ⇒ String
Remove all keys from all databases.
115 116 117 118 119 120 121 |
# File 'lib/valkey/commands/server_commands.rb', line 115 def flushall( = nil, route: nil) if && [:async] send_command(RequestType::FLUSH_ALL, ["async"], route: route) else send_command(RequestType::FLUSH_ALL, [], route: route) end end |
#flushdb(options = nil, route: nil) ⇒ String
Remove all keys from the current database.
129 130 131 132 133 134 135 |
# File 'lib/valkey/commands/server_commands.rb', line 129 def flushdb( = nil, route: nil) if && [:async] send_command(RequestType::FLUSH_DB, ["async"], route: route) else send_command(RequestType::FLUSH_DB, [], route: route) end end |
#info(cmd = nil, route: nil) ⇒ Hash
Get information and statistics about the server.
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/valkey/commands/server_commands.rb', line 145 def info(cmd = nil, route: nil) send_command(RequestType::INFO, [cmd].compact, route: route) do |reply| if reply.is_a?(Hash) reply.transform_values { |v| parse_info_reply(v, cmd) } elsif reply.is_a?(String) parse_info_reply(reply, cmd) else reply end end end |
#lastsave(route: nil) ⇒ Integer
Get the UNIX time stamp of the last successful save to disk.
178 179 180 |
# File 'lib/valkey/commands/server_commands.rb', line 178 def lastsave(route: nil) send_command(RequestType::LAST_SAVE, [], route: route) end |
#latency_doctor ⇒ String
Return a human-readable latency analysis report.
672 673 674 |
# File 'lib/valkey/commands/server_commands.rb', line 672 def latency_doctor send_command(RequestType::LATENCY_DOCTOR) end |
#latency_graph(event) ⇒ String
Return an ASCII-art graph of the latency samples for the specified event.
686 687 688 |
# File 'lib/valkey/commands/server_commands.rb', line 686 def latency_graph(event) send_command(RequestType::LATENCY_GRAPH, [event]) end |
#latency_histogram(*commands) ⇒ Array
Return a latency histogram for the specified commands.
703 704 705 706 |
# File 'lib/valkey/commands/server_commands.rb', line 703 def latency_histogram(*commands) args = commands.empty? ? [] : commands send_command(RequestType::LATENCY_HISTOGRAM, args) end |
#latency_history(event) ⇒ Array
Return the latency time series for the specified event.
718 719 720 |
# File 'lib/valkey/commands/server_commands.rb', line 718 def latency_history(event) send_command(RequestType::LATENCY_HISTORY, [event]) end |
#latency_latest ⇒ Array
Return the latest latency samples for all events.
732 733 734 |
# File 'lib/valkey/commands/server_commands.rb', line 732 def latency_latest send_command(RequestType::LATENCY_LATEST) end |
#latency_reset(*events) ⇒ Integer
Reset latency data for all events or specific events.
750 751 752 753 |
# File 'lib/valkey/commands/server_commands.rb', line 750 def latency_reset(*events) args = events.empty? ? [] : events send_command(RequestType::LATENCY_RESET, args) end |
#lolwut(version = nil, route: nil) ⇒ String
Display some computer art and the Valkey version.
546 547 548 549 |
# File 'lib/valkey/commands/server_commands.rb', line 546 def lolwut(version = nil, route: nil) args = version ? ["VERSION", version.to_s] : [] send_command(RequestType::LOLWUT, args, route: route) end |
#memory_doctor ⇒ String
Return a human-readable memory problems report.
764 765 766 |
# File 'lib/valkey/commands/server_commands.rb', line 764 def memory_doctor send_command(RequestType::MEMORY_DOCTOR) end |
#memory_malloc_stats ⇒ String
Return memory allocator statistics.
777 778 779 |
# File 'lib/valkey/commands/server_commands.rb', line 777 def memory_malloc_stats send_command(RequestType::MEMORY_MALLOC_STATS) end |
#memory_purge ⇒ String
Ask the allocator to release memory back to the operating system.
790 791 792 |
# File 'lib/valkey/commands/server_commands.rb', line 790 def memory_purge send_command(RequestType::MEMORY_PURGE) end |
#memory_stats ⇒ Hash
Return memory usage statistics.
803 804 805 806 807 808 809 810 811 |
# File 'lib/valkey/commands/server_commands.rb', line 803 def memory_stats send_command(RequestType::MEMORY_STATS) do |reply| if reply.is_a?(Array) Hash[*reply] else reply end end end |
#memory_usage(key, samples: nil) ⇒ Integer?
Return the memory usage in bytes of a key and its value.
828 829 830 831 832 |
# File 'lib/valkey/commands/server_commands.rb', line 828 def memory_usage(key, samples: nil) args = [key] args << "SAMPLES" << samples.to_s if samples send_command(RequestType::MEMORY_USAGE, args) end |
#psync(replication_id, offset) ⇒ String
Internal command used for replication (partial resynchronization).
562 563 564 |
# File 'lib/valkey/commands/server_commands.rb', line 562 def psync(replication_id, offset) send_command(RequestType::PSYNC, [replication_id, offset.to_s]) end |
#replconf(*args) ⇒ String, Array
Internal command used for replication configuration.
579 580 581 |
# File 'lib/valkey/commands/server_commands.rb', line 579 def replconf(*args) send_command(RequestType::REPL_CONF, args) end |
#replicaof(host, port) ⇒ String
Make the server a replica of another instance, or promote it as master.
597 598 599 |
# File 'lib/valkey/commands/server_commands.rb', line 597 def replicaof(host, port) send_command(RequestType::REPLICA_OF, [host, port.to_s]) end |
#restore_asking(key, ttl, serialized_value, replace: false, absttl: false, idletime: nil, freq: nil) ⇒ String
Internal command used in cluster mode for key migration with ASKING flag.
621 622 623 624 625 626 627 628 |
# File 'lib/valkey/commands/server_commands.rb', line 621 def restore_asking(key, ttl, serialized_value, replace: false, absttl: false, idletime: nil, freq: nil) args = [key, ttl.to_s, serialized_value] args << "REPLACE" if replace args << "ABSTTL" if absttl args << "IDLETIME" << idletime.to_s if idletime args << "FREQ" << freq.to_s if freq send_command(RequestType::RESTORE_ASKING, args) end |
#role ⇒ Array
Return the role of the instance in the context of replication.
644 645 646 |
# File 'lib/valkey/commands/server_commands.rb', line 644 def role send_command(RequestType::ROLE) end |
#save(route: nil) ⇒ String
Synchronously save the dataset to disk.
186 187 188 |
# File 'lib/valkey/commands/server_commands.rb', line 186 def save(route: nil) send_command(RequestType::SAVE, [], route: route) end |
#slaveof(host, port) ⇒ Object
Make the server a slave of another instance, or promote it as master.
191 192 193 |
# File 'lib/valkey/commands/server_commands.rb', line 191 def slaveof(host, port) send_command(RequestType::SLAVE_OF, [host, port]) end |
#slowlog(subcommand, length = nil) ⇒ Array<Array>, ...
Interact with the slowlog (get, len, reset)
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/valkey/commands/server_commands.rb', line 208 def slowlog(subcommand, length = nil) case subcommand.to_s.downcase when "get" args = [] args << Integer(length) if length send_command(RequestType::SLOWLOG_GET, args) when "len" send_command(RequestType::SLOWLOG_LEN) when "reset" send_command(RequestType::SLOWLOG_RESET) else raise ArgumentError, "Unknown SLOWLOG subcommand: #{subcommand}" end end |
#swapdb(index1, index2) ⇒ String
Swap two databases.
659 660 661 |
# File 'lib/valkey/commands/server_commands.rb', line 659 def swapdb(index1, index2) send_command(RequestType::SWAP_DB, [index1.to_s, index2.to_s]) end |
#sync ⇒ Object
Internal command used for replication.
224 225 226 |
# File 'lib/valkey/commands/server_commands.rb', line 224 def sync send_command(RequestType::SYNC) end |
#time(route: nil) ⇒ Array<String>, Hash{String => Array<String>}
Return the server time.
239 240 241 |
# File 'lib/valkey/commands/server_commands.rb', line 239 def time(route: nil) send_command(RequestType::TIME, [], route: route) end |