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 ⇒ String
Asynchronously rewrite the append-only file.
-
#bgsave ⇒ 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) ⇒ Hash, String
Get server configuration parameters.
-
#config_resetstat ⇒ String
Reset the server’s statistics.
-
#config_rewrite ⇒ String
Rewrite the server configuration file.
-
#config_set(*args) ⇒ String
Set server configuration parameters.
-
#dbsize ⇒ Integer
Return the number of keys in the selected database.
-
#debug(*args) ⇒ Object
RequestType::DEBUG not exist.
-
#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) ⇒ String
Remove all keys from all databases.
-
#flushdb(options = nil) ⇒ String
Remove all keys from the current database.
-
#info(cmd = nil) ⇒ Hash<String, String>
Get information and statistics about the server.
-
#lastsave ⇒ 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) ⇒ 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.
-
#monitor {|line| ... } ⇒ Object
Listen for all requests received by the server in real time.
-
#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 ⇒ String
Synchronously save the dataset to disk.
-
#shutdown ⇒ Object
Synchronously save the dataset to disk and then shut down the server.
-
#slaveof(host, port) ⇒ Object
Make the server a slave of another instance, or promote it as master.
-
#slowlog(subcommand, length = nil) ⇒ Array<String>, ...
Interact with the slowlog (get, len, reset).
-
#swapdb(index1, index2) ⇒ String
Swap two databases.
-
#sync ⇒ Object
Internal command used for replication.
-
#time ⇒ Array<Integer>
Return the server time.
Instance Method Details
#acl(subcommand, *args) ⇒ Object
Control ACL configuration (convenience method).
469 470 471 472 |
# File 'lib/valkey/commands/server_commands.rb', line 469 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.
244 245 246 247 |
# File 'lib/valkey/commands/server_commands.rb', line 244 def acl_cat(category = nil) args = category ? [category] : [] send_command(RequestType::ACL_CAT, args) end |
#acl_deluser(*usernames) ⇒ Integer
Remove the specified ACL users.
262 263 264 |
# File 'lib/valkey/commands/server_commands.rb', line 262 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.
281 282 283 284 |
# File 'lib/valkey/commands/server_commands.rb', line 281 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.
299 300 301 302 |
# File 'lib/valkey/commands/server_commands.rb', line 299 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.
314 315 316 |
# File 'lib/valkey/commands/server_commands.rb', line 314 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.
327 328 329 |
# File 'lib/valkey/commands/server_commands.rb', line 327 def acl_list send_command(RequestType::ACL_LIST) end |
#acl_load ⇒ String
Reload the ACL rules from the configured ACL file.
340 341 342 |
# File 'lib/valkey/commands/server_commands.rb', line 340 def acl_load send_command(RequestType::ACL_LOAD) end |
#acl_log(count_or_reset = nil) ⇒ Array<Hash>, String
List latest ACL security events.
360 361 362 363 |
# File 'lib/valkey/commands/server_commands.rb', line 360 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.
374 375 376 |
# File 'lib/valkey/commands/server_commands.rb', line 374 def acl_save send_command(RequestType::ACL_SAVE) end |
#acl_setuser(username, *rules) ⇒ String
Modify or create ACL rules for a user.
395 396 397 398 |
# File 'lib/valkey/commands/server_commands.rb', line 395 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.
409 410 411 |
# File 'lib/valkey/commands/server_commands.rb', line 409 def acl_users send_command(RequestType::ACL_USERS) end |
#acl_whoami ⇒ String
Return the username of the current connection.
422 423 424 |
# File 'lib/valkey/commands/server_commands.rb', line 422 def acl_whoami send_command(RequestType::ACL_WHOAMI) end |
#bgrewriteaof ⇒ String
Asynchronously rewrite the append-only file.
13 14 15 |
# File 'lib/valkey/commands/server_commands.rb', line 13 def bgrewriteaof send_command(RequestType::BG_REWRITE_AOF) end |
#bgsave ⇒ String
Asynchronously save the dataset to disk.
20 21 22 |
# File 'lib/valkey/commands/server_commands.rb', line 20 def bgsave send_command(RequestType::BG_SAVE) end |
#command(subcommand, *args) ⇒ Object
Send a generic COMMAND subcommand.
822 823 824 |
# File 'lib/valkey/commands/server_commands.rb', line 822 def command(subcommand, *args) send("command_#{subcommand.to_s.downcase}", *args) end |
#command_ ⇒ Array
Return details about every Redis command.
835 836 837 |
# File 'lib/valkey/commands/server_commands.rb', line 835 def command_ send_command(RequestType::COMMAND_) end |
#command_count ⇒ Integer
Return the total number of commands in the server.
848 849 850 |
# File 'lib/valkey/commands/server_commands.rb', line 848 def command_count send_command(RequestType::COMMAND_COUNT) end |
#command_docs(*commands) ⇒ Array
Return documentary information about one or more commands.
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
# File 'lib/valkey/commands/server_commands.rb', line 865 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.
895 896 897 |
# File 'lib/valkey/commands/server_commands.rb', line 895 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.
909 910 911 |
# File 'lib/valkey/commands/server_commands.rb', line 909 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.
926 927 928 929 |
# File 'lib/valkey/commands/server_commands.rb', line 926 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.
950 951 952 953 954 955 956 957 958 |
# File 'lib/valkey/commands/server_commands.rb', line 950 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.
29 30 31 |
# File 'lib/valkey/commands/server_commands.rb', line 29 def config(action, *args) send("config_#{action.to_s.downcase}", *args) end |
#config_get(*args) ⇒ Hash, String
Returns a Hash with parameter names as keys and values as values when multiple params requested.
Get server configuration parameters.
Sends the CONFIG GET command with the given arguments.
48 49 50 51 52 53 54 55 56 |
# File 'lib/valkey/commands/server_commands.rb', line 48 def config_get(*args) send_command(RequestType::CONFIG_GET, args) do |reply| if reply.is_a?(Array) Hash[*reply] else reply end end end |
#config_resetstat ⇒ String
Reset the server’s statistics.
Sends the CONFIG RESETSTAT command.
79 80 81 |
# File 'lib/valkey/commands/server_commands.rb', line 79 def config_resetstat send_command(RequestType::CONFIG_RESET_STAT) end |
#config_rewrite ⇒ String
Rewrite the server configuration file.
Sends the CONFIG REWRITE command.
91 92 93 |
# File 'lib/valkey/commands/server_commands.rb', line 91 def config_rewrite send_command(RequestType::CONFIG_REWRITE) end |
#config_set(*args) ⇒ String
Set server configuration parameters.
Sends the CONFIG SET command with the given key-value pairs.
67 68 69 |
# File 'lib/valkey/commands/server_commands.rb', line 67 def config_set(*args) send_command(RequestType::CONFIG_SET, args) end |
#dbsize ⇒ Integer
Return the number of keys in the selected database.
98 99 100 |
# File 'lib/valkey/commands/server_commands.rb', line 98 def dbsize send_command(RequestType::DB_SIZE) end |
#debug(*args) ⇒ Object
RequestType::DEBUG not exist
225 226 227 |
# File 'lib/valkey/commands/server_commands.rb', line 225 def debug(*args) send_command(RequestType::DEBUG, args) end |
#failover(to: nil, force: false, abort: false, timeout: nil) ⇒ String
Perform a manual failover of a master to one of its replicas.
500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/valkey/commands/server_commands.rb', line 500 def failover(to: nil, force: false, abort: false, timeout: nil) args = [] if to host, port = to.split args << "TO" << host << port end args << "FORCE" if force args << "ABORT" if abort args << "TIMEOUT" << timeout.to_s if timeout send_command(RequestType::FAIL_OVER, args) end |
#flushall(options = nil) ⇒ String
Remove all keys from all databases.
107 108 109 110 111 112 113 |
# File 'lib/valkey/commands/server_commands.rb', line 107 def flushall( = nil) if && [:async] send_command(RequestType::FLUSH_ALL, ["async"]) else send_command(RequestType::FLUSH_ALL) end end |
#flushdb(options = nil) ⇒ String
Remove all keys from the current database.
120 121 122 123 124 125 126 |
# File 'lib/valkey/commands/server_commands.rb', line 120 def flushdb( = nil) if && [:async] send_command(RequestType::FLUSH_DB, ["async"]) else send_command(RequestType::FLUSH_DB) end end |
#info(cmd = nil) ⇒ Hash<String, String>
Get information and statistics about the server.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/valkey/commands/server_commands.rb', line 132 def info(cmd = nil) send_command(RequestType::INFO, [cmd].compact) do |reply| if reply.is_a?(String) reply = Utils::HashifyInfo.call(reply) if cmd && cmd.to_s == "commandstats" # Extract nested hashes for INFO COMMANDSTATS reply = reply.to_h do |k, v| v = v.split(",").map { |e| e.split("=") } [k[/^cmdstat_(.*)$/, 1], v.to_h] end end end reply end end |
#lastsave ⇒ Integer
Get the UNIX time stamp of the last successful save to disk.
153 154 155 |
# File 'lib/valkey/commands/server_commands.rb', line 153 def lastsave send_command(RequestType::LAST_SAVE) end |
#latency_doctor ⇒ String
Return a human-readable latency analysis report.
651 652 653 |
# File 'lib/valkey/commands/server_commands.rb', line 651 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.
665 666 667 |
# File 'lib/valkey/commands/server_commands.rb', line 665 def latency_graph(event) send_command(RequestType::LATENCY_GRAPH, [event]) end |
#latency_histogram(*commands) ⇒ Array
Return a latency histogram for the specified commands.
682 683 684 685 |
# File 'lib/valkey/commands/server_commands.rb', line 682 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.
697 698 699 |
# File 'lib/valkey/commands/server_commands.rb', line 697 def latency_history(event) send_command(RequestType::LATENCY_HISTORY, [event]) end |
#latency_latest ⇒ Array
Return the latest latency samples for all events.
711 712 713 |
# File 'lib/valkey/commands/server_commands.rb', line 711 def latency_latest send_command(RequestType::LATENCY_LATEST) end |
#latency_reset(*events) ⇒ Integer
Reset latency data for all events or specific events.
729 730 731 732 |
# File 'lib/valkey/commands/server_commands.rb', line 729 def latency_reset(*events) args = events.empty? ? [] : events send_command(RequestType::LATENCY_RESET, args) end |
#lolwut(version = nil) ⇒ String
Display some computer art and the Valkey version.
525 526 527 528 |
# File 'lib/valkey/commands/server_commands.rb', line 525 def lolwut(version = nil) args = version ? ["VERSION", version.to_s] : [] send_command(RequestType::LOLWUT, args) end |
#memory_doctor ⇒ String
Return a human-readable memory problems report.
743 744 745 |
# File 'lib/valkey/commands/server_commands.rb', line 743 def memory_doctor send_command(RequestType::MEMORY_DOCTOR) end |
#memory_malloc_stats ⇒ String
Return memory allocator statistics.
756 757 758 |
# File 'lib/valkey/commands/server_commands.rb', line 756 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.
769 770 771 |
# File 'lib/valkey/commands/server_commands.rb', line 769 def memory_purge send_command(RequestType::MEMORY_PURGE) end |
#memory_stats ⇒ Hash
Return memory usage statistics.
782 783 784 785 786 787 788 789 790 |
# File 'lib/valkey/commands/server_commands.rb', line 782 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.
807 808 809 810 811 |
# File 'lib/valkey/commands/server_commands.rb', line 807 def memory_usage(key, samples: nil) args = [key] args << "SAMPLES" << samples.to_s if samples send_command(RequestType::MEMORY_USAGE, args) end |
#monitor {|line| ... } ⇒ Object
Listen for all requests received by the server in real time.
There is no way to interrupt this command.
163 164 165 166 167 168 169 170 171 |
# File 'lib/valkey/commands/server_commands.rb', line 163 def monitor synchronize do |client| client = client.pubsub client.call_v([:monitor]) loop do yield client.next_event end end end |
#psync(replication_id, offset) ⇒ String
Internal command used for replication (partial resynchronization).
541 542 543 |
# File 'lib/valkey/commands/server_commands.rb', line 541 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.
558 559 560 |
# File 'lib/valkey/commands/server_commands.rb', line 558 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.
576 577 578 |
# File 'lib/valkey/commands/server_commands.rb', line 576 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.
600 601 602 603 604 605 606 607 |
# File 'lib/valkey/commands/server_commands.rb', line 600 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.
623 624 625 |
# File 'lib/valkey/commands/server_commands.rb', line 623 def role send_command(RequestType::ROLE) end |
#save ⇒ String
Synchronously save the dataset to disk.
176 177 178 |
# File 'lib/valkey/commands/server_commands.rb', line 176 def save send_command(RequestType::SAVE) end |
#shutdown ⇒ Object
Synchronously save the dataset to disk and then shut down the server.
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/valkey/commands/server_commands.rb', line 181 def shutdown synchronize do |client| client.disable_reconnection do client.call_v([:shutdown]) rescue ConnectionError # This means Redis has probably exited. nil end end end |
#slaveof(host, port) ⇒ Object
Make the server a slave of another instance, or promote it as master.
193 194 195 |
# File 'lib/valkey/commands/server_commands.rb', line 193 def slaveof(host, port) send_command(RequestType::SLAVE_OF, [host, port]) end |
#slowlog(subcommand, length = nil) ⇒ Array<String>, ...
Interact with the slowlog (get, len, reset)
202 203 204 205 206 |
# File 'lib/valkey/commands/server_commands.rb', line 202 def slowlog(subcommand, length = nil) args = [:slowlog, subcommand] args << Integer(length) if length send_command(args) end |
#swapdb(index1, index2) ⇒ String
Swap two databases.
638 639 640 |
# File 'lib/valkey/commands/server_commands.rb', line 638 def swapdb(index1, index2) send_command(RequestType::SWAP_DB, [index1.to_s, index2.to_s]) end |
#sync ⇒ Object
Internal command used for replication.
209 210 211 |
# File 'lib/valkey/commands/server_commands.rb', line 209 def sync send_command(RequestType::SYNC) end |
#time ⇒ Array<Integer>
Return the server time.
220 221 222 |
# File 'lib/valkey/commands/server_commands.rb', line 220 def time send_command(RequestType::TIME) end |