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

Instance Method Details

#acl(subcommand, *args) ⇒ Object

Control ACL configuration (convenience method).

Examples:

List all categories

valkey.acl(:cat)
  # => ["keyspace", "read", ...]

Delete a user

valkey.acl(:deluser, "alice")
  # => 1

Test command execution

valkey.acl(:dryrun, "alice", "get", "key1")
  # => "OK"

Generate a password

valkey.acl(:genpass)
  # => "dd721260..."

Get user info

valkey.acl(:getuser, "alice")
  # => [...]

List all ACL rules

valkey.acl(:list)
  # => ["user default on ...", ...]

Reload ACL from file

valkey.acl(:load)
  # => "OK"

Get ACL log

valkey.acl(:log)
  # => [...]

Save ACL to file

valkey.acl(:save)
  # => "OK"

Set user rules

valkey.acl(:setuser, "alice", "on", ">password")
  # => "OK"

List all users

valkey.acl(:users)
  # => ["default", "alice"]

Get current username

valkey.acl(:whoami)
  # => "default"

Parameters:

  • subcommand (String, Symbol)

    the subcommand (cat, deluser, dryrun, genpass, getuser, list, load, log, save, setuser, users, whoami)

  • args (Array)

    arguments for the subcommand

Returns:

  • (Object)

    depends on subcommand



490
491
492
493
# File 'lib/valkey/commands/server_commands.rb', line 490

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.

Examples:

List all categories

valkey.acl_cat
  # => ["keyspace", "read", "write", ...]

List commands in a category

valkey.acl_cat("dangerous")
  # => ["flushdb", "flushall", ...]

Parameters:

  • category (String) (defaults to: nil)

    optional category name to list commands

Returns:

  • (Array<String>)

    array of categories or commands

See Also:



265
266
267
268
# File 'lib/valkey/commands/server_commands.rb', line 265

def acl_cat(category = nil)
  args = category ? [category] : []
  send_command(RequestType::ACL_CAT, args)
end

#acl_deluser(*usernames) ⇒ Integer

Remove the specified ACL users.

Examples:

Delete a user

valkey.acl_deluser("alice")
  # => 1

Delete multiple users

valkey.acl_deluser("alice", "bob")
  # => 2

Parameters:

  • usernames (Array<String>)

    one or more usernames to delete

Returns:

  • (Integer)

    number of users deleted

See Also:



283
284
285
# File 'lib/valkey/commands/server_commands.rb', line 283

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.

Examples:

Test if user can run a command

valkey.acl_dryrun("alice", "get", "key1")
  # => "OK"

Test a command that would be denied

valkey.acl_dryrun("alice", "set", "key1", "value")
  # => "This user has no permissions to run the 'set' command"

Parameters:

  • username (String)

    the username to test

  • command (String)

    the command to test

  • args (Array<String>)

    command arguments

Returns:

  • (String)

    "OK" if allowed, or error message if denied

See Also:



302
303
304
305
# File 'lib/valkey/commands/server_commands.rb', line 302

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.

Examples:

Generate a password with default length

valkey.acl_genpass
  # => "dd721260bfe1b3d9601e7fbab36de6d04e2e67b0ef1c53de59d45950db0dd3cc"

Generate a password with specific bit length

valkey.acl_genpass(32)
  # => "355ef3dd"

Parameters:

  • bits (Integer) (defaults to: nil)

    optional number of bits (default: 256)

Returns:

  • (String)

    the generated password

See Also:



320
321
322
323
# File 'lib/valkey/commands/server_commands.rb', line 320

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.

Examples:

Get user rules

valkey.acl_getuser("alice")
  # => ["flags" => ["on", "allkeys"], "passwords" => [...], ...]

Parameters:

  • username (String)

    the username to query

Returns:

  • (Array, nil)

    array of user properties, or nil if user doesn't exist

See Also:



335
336
337
# File 'lib/valkey/commands/server_commands.rb', line 335

def acl_getuser(username)
  send_command(RequestType::ACL_GET_USER, [username])
end

#acl_listArray<String>

List the current ACL rules in ACL config file format.

Examples:

List all ACL rules

valkey.acl_list
  # => ["user default on nopass ~* &* +@all", "user alice on ..."]

Returns:

  • (Array<String>)

    array of ACL rules

See Also:



348
349
350
# File 'lib/valkey/commands/server_commands.rb', line 348

def acl_list
  send_command(RequestType::ACL_LIST)
end

#acl_loadString

Reload the ACL rules from the configured ACL file.

Examples:

Reload ACL from file

valkey.acl_load
  # => "OK"

Returns:

  • (String)

    "OK"

See Also:



361
362
363
# File 'lib/valkey/commands/server_commands.rb', line 361

def acl_load
  send_command(RequestType::ACL_LOAD)
end

#acl_log(count_or_reset = nil) ⇒ Array<Hash>, String

List latest ACL security events.

Examples:

Get recent ACL log entries

valkey.acl_log
  # => [{"count" => 1, "reason" => "auth", ...}, ...]

Get specific number of log entries

valkey.acl_log(10)
  # => [...]

Reset the ACL log

valkey.acl_log("RESET")
  # => "OK"

Parameters:

  • count_or_reset (Integer, String) (defaults to: nil)

    number of entries or "RESET" to clear the log

Returns:

  • (Array<Hash>, String)

    array of log entries, or "OK" if reset

See Also:



381
382
383
384
# File 'lib/valkey/commands/server_commands.rb', line 381

def acl_log(count_or_reset = nil)
  args = count_or_reset ? [count_or_reset] : []
  send_command(RequestType::ACL_LOG, args)
end

#acl_saveString

Save the current ACL rules to the configured ACL file.

Examples:

Save ACL to file

valkey.acl_save
  # => "OK"

Returns:

  • (String)

    "OK"

See Also:



395
396
397
# File 'lib/valkey/commands/server_commands.rb', line 395

def acl_save
  send_command(RequestType::ACL_SAVE)
end

#acl_setuser(username, *rules) ⇒ String

Modify or create ACL rules for a user.

Examples:

Create a user with password

valkey.acl_setuser("alice", "on", ">password", "~*", "+@all")
  # => "OK"

Create a read-only user

valkey.acl_setuser("bob", "on", ">pass123", "~*", "+@read")
  # => "OK"

Disable a user

valkey.acl_setuser("alice", "off")
  # => "OK"

Parameters:

  • username (String)

    the username to modify or create

  • rules (Array<String>)

    ACL rules to apply

Returns:

  • (String)

    "OK"

See Also:



416
417
418
419
# File 'lib/valkey/commands/server_commands.rb', line 416

def acl_setuser(username, *rules)
  command_args = [username] + rules
  send_command(RequestType::ACL_SET_USER, command_args)
end

#acl_usersArray<String>

List all configured ACL users.

Examples:

List all users

valkey.acl_users
  # => ["default", "alice", "bob"]

Returns:

  • (Array<String>)

    array of usernames

See Also:



430
431
432
# File 'lib/valkey/commands/server_commands.rb', line 430

def acl_users
  send_command(RequestType::ACL_USERS)
end

#acl_whoamiString

Return the username of the current connection.

Examples:

Get current username

valkey.acl_whoami
  # => "default"

Returns:

  • (String)

    the current username

See Also:



443
444
445
# File 'lib/valkey/commands/server_commands.rb', line 443

def acl_whoami
  send_command(RequestType::ACL_WHOAMI)
end

#bgrewriteaof(route: nil) ⇒ String

Asynchronously rewrite the append-only file.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (String)


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.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (String)


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.

Examples:

command(:count)                    # => 234
command(:list)                     # => ["GET", "SET", ...]
command(:info, "GET", "SET")       # => [[...], [...]]

Parameters:

  • subcommand (Symbol, String)

    The COMMAND subcommand to run, e.g. :count, :docs, :info, :list

  • args (Array)

    Arguments for the subcommand

Returns:

  • (Object)

    Depends on subcommand



850
851
852
# File 'lib/valkey/commands/server_commands.rb', line 850

def command(subcommand, *args)
  send("command_#{subcommand.to_s.downcase}", *args)
end

#command_Array

Return details about every Redis command.

Examples:

valkey.command
  # => [["GET", 2, ["readonly", "fast"], 1, 1, 1], ...]

Returns:

  • (Array)

    array of command information arrays

See Also:



863
864
865
# File 'lib/valkey/commands/server_commands.rb', line 863

def command_
  send_command(RequestType::COMMAND_)
end

#command_countInteger

Return the total number of commands in the server.

Examples:

valkey.command_count
  # => 234

Returns:

  • (Integer)

    total number of commands

See Also:



876
877
878
# File 'lib/valkey/commands/server_commands.rb', line 876

def command_count
  send_command(RequestType::COMMAND_COUNT)
end

#command_docs(*commands) ⇒ Array

Return documentary information about one or more commands.

Examples:

Get docs for specific commands

valkey.command_docs("GET", "SET")
  # => [{"summary" => "...", "since" => "1.0.0", ...}, ...]

Get docs for all commands

valkey.command_docs
  # => [{"summary" => "...", ...}, ...]

Parameters:

  • commands (Array<String>)

    command names to get documentation for

Returns:

  • (Array)

    array of command documentation hashes

See Also:



893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/valkey/commands/server_commands.rb', line 893

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.

Examples:

valkey.command_get_keys("GET", "mykey")
  # => [0]
valkey.command_get_keys("MSET", "key1", "val1", "key2", "val2")
  # => [0, 2]

Parameters:

  • command (String, Array<String>)

    the command and its arguments

Returns:

  • (Array)

    array of key positions

See Also:



923
924
925
# File 'lib/valkey/commands/server_commands.rb', line 923

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.

Examples:

valkey.command_get_keys_and_flags("GET", "mykey")
  # => [[0, ["RW", "access"]], ...]

Parameters:

  • command (String, Array<String>)

    the command and its arguments

Returns:

  • (Array)

    array of [key_position, flags] pairs

See Also:



937
938
939
# File 'lib/valkey/commands/server_commands.rb', line 937

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.

Examples:

Get info for specific commands

valkey.command_info("GET", "SET")
  # => [[...], [...]]

Get info for all commands

valkey.command_info
  # => [[...], ...]

Parameters:

  • commands (Array<String>)

    command names to get information for

Returns:

  • (Array)

    array of command information arrays

See Also:



954
955
956
957
# File 'lib/valkey/commands/server_commands.rb', line 954

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.

Examples:

Get all commands

valkey.command_list
  # => ["GET", "SET", "DEL", ...]

Filter by module with pattern

valkey.command_list(filterby: "json", pattern: "json.*")
  # => ["json.get", "json.set", ...]

Filter by ACL category

valkey.command_list(aclcat: "read")
  # => ["GET", "MGET", ...]

Parameters:

  • options (Hash)

    optional filters

    • :filterby => String: filter by module name (e.g., "json")
    • :aclcat => String: filter by ACL category
    • :pattern => String: pattern to match (used with filterby)

Returns:

  • (Array<String>)

    array of command names

See Also:



978
979
980
981
982
983
984
985
986
# File 'lib/valkey/commands/server_commands.rb', line 978

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.

Parameters:

  • action (Symbol)

    e.g. :get, :set, :resetstat

Returns:

  • (String, Hash)

    string reply, or hash when retrieving more than one property with CONFIG GET



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, String

Get server configuration parameters.

Sends the CONFIG GET command with the given arguments.

Examples:

Get all configuration parameters

config_get('*')

Get a specific parameter

config_get('maxmemory')

Parameters:

  • args (Array<String>)

    Configuration parameters to get

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Hash, String)


48
49
50
51
52
53
54
55
56
# File 'lib/valkey/commands/server_commands.rb', line 48

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.

Examples:

config_resetstat

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing.

Returns:

  • (String)

    Returns "OK" if successful



81
82
83
# File 'lib/valkey/commands/server_commands.rb', line 81

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.

Examples:

config_rewrite

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing.

Returns:

  • (String)

    Returns "OK" if successful



94
95
96
# File 'lib/valkey/commands/server_commands.rb', line 94

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.

Examples:

Set maxmemory to 100mb

config_set('maxmemory', '100mb')

Parameters:

  • args (Array<String>)

    Key-value pairs to set configuration

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing.

Returns:

  • (String)

    Returns "OK" if successful



68
69
70
# File 'lib/valkey/commands/server_commands.rb', line 68

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.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Integer)


102
103
104
# File 'lib/valkey/commands/server_commands.rb', line 102

def dbsize(route: nil)
  send_command(RequestType::DB_SIZE, [], route: route)
end

#debug(*args) ⇒ Object

RequestType::DEBUG not exist



246
247
248
# File 'lib/valkey/commands/server_commands.rb', line 246

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.

Examples:

Perform a failover

valkey.failover
  # => "OK"

Failover to a specific replica

valkey.failover(to: "127.0.0.1 6380")
  # => "OK"

Force failover

valkey.failover(force: true)
  # => "OK"

Abort failover

valkey.failover(abort: true)
  # => "OK"

Failover with timeout

valkey.failover(timeout: 5000)
  # => "OK"

Parameters:

  • options (Hash)

    optional parameters

    • :to => String: host and port of the replica to promote (e.g., "host port")
    • :force => Boolean: force failover even if master is reachable
    • :abort => Boolean: abort an ongoing failover
    • :timeout => Integer: timeout in milliseconds

Returns:

  • (String)

    "OK"

See Also:



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/valkey/commands/server_commands.rb', line 521

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.

Parameters:

  • options (Hash) (defaults to: nil)
    • :async => Boolean: async flush (default: false)
  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing.

Returns:

  • (String)

    OK



112
113
114
115
116
117
118
# File 'lib/valkey/commands/server_commands.rb', line 112

def flushall(options = nil, route: nil)
  if options && options[: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.

Parameters:

  • options (Hash) (defaults to: nil)
    • :async => Boolean: async flush (default: false)
  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing.

Returns:

  • (String)

    OK



126
127
128
129
130
131
132
# File 'lib/valkey/commands/server_commands.rb', line 126

def flushdb(options = nil, route: nil)
  if options && options[: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.

Parameters:

  • cmd (String, Symbol, nil) (defaults to: nil)

    section name (e.g. "commandstats")

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Hash)


139
140
141
142
143
144
145
146
147
148
149
# File 'lib/valkey/commands/server_commands.rb', line 139

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.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Integer)


172
173
174
# File 'lib/valkey/commands/server_commands.rb', line 172

def lastsave(route: nil)
  send_command(RequestType::LAST_SAVE, [], route: route)
end

#latency_doctorString

Return a human-readable latency analysis report.

Examples:

valkey.latency_doctor
  # => "Dave, I observed latency events in this Redis instance..."

Returns:

  • (String)

    human-readable latency analysis

See Also:



679
680
681
# File 'lib/valkey/commands/server_commands.rb', line 679

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.

Examples:

valkey.latency_graph("command")
  # => "command - high 500 ms, low 501 ms (all time high 500 ms)\n..."

Parameters:

  • event (String)

    the event name to graph

Returns:

  • (String)

    ASCII-art graph of latency samples

See Also:



693
694
695
# File 'lib/valkey/commands/server_commands.rb', line 693

def latency_graph(event)
  send_command(RequestType::LATENCY_GRAPH, [event])
end

#latency_histogram(*commands) ⇒ Array

Return a latency histogram for the specified commands.

Examples:

Get histogram for all commands

valkey.latency_histogram
  # => [["SET", [["0-1", 100], ["2-3", 50], ...]], ...]

Get histogram for specific commands

valkey.latency_histogram("SET", "GET")
  # => [["SET", [["0-1", 100], ...]], ["GET", [["0-1", 200], ...]]]

Parameters:

  • commands (Array<String>)

    optional command names to get histograms for

Returns:

  • (Array)

    array of latency histogram entries

See Also:



710
711
712
713
# File 'lib/valkey/commands/server_commands.rb', line 710

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.

Examples:

valkey.latency_history("command")
  # => [[1234567890, 100], [1234567891, 150], ...]

Parameters:

  • event (String)

    the event name to get history for

Returns:

  • (Array)

    array of [timestamp, latency] pairs

See Also:



725
726
727
# File 'lib/valkey/commands/server_commands.rb', line 725

def latency_history(event)
  send_command(RequestType::LATENCY_HISTORY, [event])
end

#latency_latestArray

Return the latest latency samples for all events.

Examples:

valkey.latency_latest
  # => [["command", 1234567890, 100, 200], ["fast-command", 1234567891, 50, 100]]

Returns:

  • (Array)

    array of event information arrays Each entry is [event_name, timestamp, latest_latency, max_latency]

See Also:



739
740
741
# File 'lib/valkey/commands/server_commands.rb', line 739

def latency_latest
  send_command(RequestType::LATENCY_LATEST)
end

#latency_reset(*events) ⇒ Integer

Reset latency data for all events or specific events.

Examples:

Reset all latency data

valkey.latency_reset
  # => 3

Reset specific events

valkey.latency_reset("command", "fast-command")
  # => 2

Parameters:

  • events (Array<String>)

    optional event names to reset If no events are specified, resets all latency data

Returns:

  • (Integer)

    number of events reset

See Also:



757
758
759
760
# File 'lib/valkey/commands/server_commands.rb', line 757

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.

Examples:

valkey.lolwut
  # => "Valkey ver. 7.0.0\n..."

With version

valkey.lolwut(5)
  # => "Valkey ver. 7.0.0\n..."

Parameters:

  • version (Integer, nil) (defaults to: nil)

    optional version number for different art

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (String)

    ASCII art and version information

See Also:



553
554
555
556
# File 'lib/valkey/commands/server_commands.rb', line 553

def lolwut(version = nil, route: nil)
  args = version ? ["VERSION", version.to_s] : []
  send_command(RequestType::LOLWUT, args, route: route)
end

#memory_doctorString

Return a human-readable memory problems report.

Examples:

valkey.memory_doctor
  # => "Hi Sam, this is the Valkey memory doctor..."

Returns:

  • (String)

    human-readable memory analysis report

See Also:



771
772
773
# File 'lib/valkey/commands/server_commands.rb', line 771

def memory_doctor
  send_command(RequestType::MEMORY_DOCTOR)
end

#memory_malloc_statsString

Return memory allocator statistics.

Examples:

valkey.memory_malloc_stats
  # => "___ Begin jemalloc statistics ___..."

Returns:

  • (String)

    memory allocator statistics

See Also:



784
785
786
# File 'lib/valkey/commands/server_commands.rb', line 784

def memory_malloc_stats
  send_command(RequestType::MEMORY_MALLOC_STATS)
end

#memory_purgeString

Ask the allocator to release memory back to the operating system.

Examples:

valkey.memory_purge
  # => "OK"

Returns:

  • (String)

    "OK"

See Also:



797
798
799
# File 'lib/valkey/commands/server_commands.rb', line 797

def memory_purge
  send_command(RequestType::MEMORY_PURGE)
end

#memory_statsHash

Return memory usage statistics.

Examples:

valkey.memory_stats
  # => {"peak.allocated" => "1048576", "total.allocated" => "524288", ...}

Returns:

  • (Hash)

    memory usage statistics

See Also:



810
811
812
813
814
815
816
817
818
# File 'lib/valkey/commands/server_commands.rb', line 810

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.

Examples:

Get memory usage for a key

valkey.memory_usage("mykey")
  # => 1024

Get memory usage with custom samples

valkey.memory_usage("mykey", samples: 10)
  # => 2048

Parameters:

  • key (String)

    the key to check

  • options (Hash)

    optional parameters

    • :samples => Integer: number of samples for nested data structures (default: 5)

Returns:

  • (Integer, nil)

    memory usage in bytes, or nil if key doesn't exist

See Also:



835
836
837
838
839
# File 'lib/valkey/commands/server_commands.rb', line 835

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.

Yields:

  • a block to be called for every line of output

Yield Parameters:

  • line (String)

    timestamp and command that was executed



182
183
184
185
186
187
188
189
190
# File 'lib/valkey/commands/server_commands.rb', line 182

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).

Examples:

valkey.psync("?", -1)
  # => "+FULLRESYNC ..."

Parameters:

  • replication_id (String)

    the replication ID

  • offset (Integer)

    the replication offset

Returns:

  • (String)

    replication stream data

See Also:



569
570
571
# File 'lib/valkey/commands/server_commands.rb', line 569

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.

Examples:

Set listening port

valkey.replconf("listening-port", "6380")
  # => "OK"

Send ACK

valkey.replconf("ACK", "1000")
  # => "OK"

Parameters:

  • args (Array<String>)

    configuration arguments

Returns:

  • (String, Array)

    depends on the configuration command

See Also:



586
587
588
# File 'lib/valkey/commands/server_commands.rb', line 586

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.

Examples:

Make server a replica

valkey.replicaof("127.0.0.1", 6379)
  # => "OK"

Promote to master

valkey.replicaof("NO", "ONE")
  # => "OK"

Parameters:

  • host (String)

    the master host (use "NO" with "ONE" to promote to master)

  • port (String, Integer)

    the master port (use "ONE" with "NO" to promote to master)

Returns:

  • (String)

    "OK"

See Also:



604
605
606
# File 'lib/valkey/commands/server_commands.rb', line 604

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.

Examples:

Restore a key with ASKING

valkey.restore_asking("mykey", 0, serialized_data)
  # => "OK"

Restore with replace

valkey.restore_asking("mykey", 0, serialized_data, replace: true)
  # => "OK"

Parameters:

  • key (String)

    the key to restore

  • ttl (Integer)

    time to live in milliseconds (0 for no expiry)

  • serialized_value (String)

    the serialized value from DUMP

  • options (Hash)

    optional parameters

    • :replace => Boolean: replace existing key
    • :absttl => Boolean: ttl is an absolute timestamp
    • :idletime => Integer: set idle time in seconds
    • :freq => Integer: set LFU frequency

Returns:

  • (String)

    "OK"

See Also:



628
629
630
631
632
633
634
635
# File 'lib/valkey/commands/server_commands.rb', line 628

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

#roleArray

Return the role of the instance in the context of replication.

Examples:

Get role on master

valkey.role
  # => ["master", 3129659, [["127.0.0.1", "6380", "3129659"]]]

Get role on replica

valkey.role
  # => ["slave", "127.0.0.1", 6379, "connected", 3129659]

Returns:

  • (Array)

    role information

    • For master: ["master", replication_offset, [replica1, replica2, ...]]
    • For replica: ["slave", master_host, master_port, replication_state, replication_offset]

See Also:



651
652
653
# File 'lib/valkey/commands/server_commands.rb', line 651

def role
  send_command(RequestType::ROLE)
end

#save(route: nil) ⇒ String

Synchronously save the dataset to disk.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing.

Returns:

  • (String)


196
197
198
# File 'lib/valkey/commands/server_commands.rb', line 196

def save(route: nil)
  send_command(RequestType::SAVE, [], route: route)
end

#shutdownObject

Synchronously save the dataset to disk and then shut down the server.



201
202
203
204
205
206
207
208
209
210
# File 'lib/valkey/commands/server_commands.rb', line 201

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.



213
214
215
# File 'lib/valkey/commands/server_commands.rb', line 213

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)

Parameters:

  • subcommand (String)

    e.g. get, len, reset

  • length (Integer) (defaults to: nil)

    maximum number of entries to return

Returns:

  • (Array<String>, Integer, String)

    depends on subcommand



222
223
224
225
226
# File 'lib/valkey/commands/server_commands.rb', line 222

def slowlog(subcommand, length = nil)
  args = [:slowlog, subcommand]
  args << Integer(length) if length
  send_command(args)
end

#swapdb(index1, index2) ⇒ String

Swap two databases.

Examples:

Swap database 0 and 1

valkey.swapdb(0, 1)
  # => "OK"

Parameters:

  • index1 (Integer)

    first database index

  • index2 (Integer)

    second database index

Returns:

  • (String)

    "OK"

See Also:



666
667
668
# File 'lib/valkey/commands/server_commands.rb', line 666

def swapdb(index1, index2)
  send_command(RequestType::SWAP_DB, [index1.to_s, index2.to_s])
end

#syncObject

Internal command used for replication.



229
230
231
# File 'lib/valkey/commands/server_commands.rb', line 229

def sync
  send_command(RequestType::SYNC)
end

#time(route: nil) ⇒ Array<Integer>

Return the server time.

Examples:

r.time # => [ 1333093196, 606806 ]

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Array<Integer>)

    tuple of seconds since UNIX epoch and microseconds in the current second



241
242
243
# File 'lib/valkey/commands/server_commands.rb', line 241

def time(route: nil)
  send_command(RequestType::TIME, [], route: route)
end