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

#bgrewriteaofString

Asynchronously rewrite the append-only file.

Returns:

  • (String)

    ‘OK`



13
14
15
# File 'lib/valkey/commands/server_commands.rb', line 13

def bgrewriteaof
  send_command(RequestType::BG_REWRITE_AOF)
end

#bgsaveString

Asynchronously save the dataset to disk.

Returns:

  • (String)

    ‘OK`



20
21
22
# File 'lib/valkey/commands/server_commands.rb', line 20

def bgsave
  send_command(RequestType::BG_SAVE)
end

#client(subcommand, *args) ⇒ Object

Send a generic CLIENT subcommand.

Parameters:

  • subcommand (Symbol, String)

    The CLIENT subcommand to run, e.g. :list, :id, :kill, etc.

  • args (Array)

    Arguments for the subcommand

Returns:

  • (Object)

    Depends on subcommand



100
101
102
# File 'lib/valkey/commands/server_commands.rb', line 100

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

#client_caching(*args) ⇒ String

Enable or disable client query caching.

Parameters:

  • args (Array)

    Caching subcommand arguments

Returns:

  • (String)

    Server response



237
238
239
# File 'lib/valkey/commands/server_commands.rb', line 237

def client_caching(*args)
  send_command(RequestType::CLIENT_CACHING, args)
end

#client_get_nameString?

Get the name of the current connection.

Examples:

name = client_get_name

Returns:

  • (String, nil)

    Client name or nil if not set



124
125
126
# File 'lib/valkey/commands/server_commands.rb', line 124

def client_get_name
  send_command(RequestType::CLIENT_GET_NAME)
end

#client_getredirInteger

Get the client ID that the current client is redirected to.

Returns:

  • (Integer)

    Client ID, or 0 if not redirected



244
245
246
# File 'lib/valkey/commands/server_commands.rb', line 244

def client_getredir
  send_command(RequestType::CLIENT_GET_REDIR)
end

#client_idInteger

Get the current connection’s client ID.

Examples:

id = client_id

Returns:

  • (Integer)

    Client ID



161
162
163
# File 'lib/valkey/commands/server_commands.rb', line 161

def client_id
  send_command(RequestType::CLIENT_ID)
end

#client_infoString

Get information about the current client connection.

Returns:

  • (String)

    Client info key-value pairs



221
222
223
# File 'lib/valkey/commands/server_commands.rb', line 221

def client_info
  send_command(RequestType::CLIENT_INFO)
end

#client_kill(*args) ⇒ Integer

Kill client connections by address or ID.

Examples:

client_kill("addr", "127.0.0.1:6379")

Parameters:

  • args (Array<String>)

    Kill filters such as “addr”, “id”, etc.

Returns:

  • (Integer)

    Number of clients killed



144
145
146
# File 'lib/valkey/commands/server_commands.rb', line 144

def client_kill(*args)
  send_command(RequestType::CLIENT_KILL, args)
end

#client_kill_simple(*args) ⇒ Integer

Simplified client kill command, similar to ‘client_kill`.

Parameters:

  • args (Array<String>)

    Kill filters

Returns:

  • (Integer)

    Number of clients killed



152
153
154
# File 'lib/valkey/commands/server_commands.rb', line 152

def client_kill_simple(*args)
  send_command(RequestType::CLIENT_KILL, args)
end

#client_listArray<Hash>

Get a list of client connections.

Examples:

clients = client_list
clients.each { |client| puts client["id"] }

Returns:

  • (Array<Hash>)

    List of clients, each represented as a Hash of attributes



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

def client_list
  send_command(RequestType::CLIENT_LIST) do |reply|
    reply.lines.map do |line|
      entries = line.chomp.split(/[ =]/)
      Hash[entries.each_slice(2).to_a]
    end
  end
end

#client_no_evict(*args) ⇒ String

Enable or disable the no-eviction flag for the current client.

Parameters:

  • mode (Symbol, String)

    :on or :off

Returns:

  • (String)

    Server response



252
253
254
# File 'lib/valkey/commands/server_commands.rb', line 252

def client_no_evict(*args)
  send_command(RequestType::CLIENT_NO_EVICT, args)
end

#client_no_touch(*args) ⇒ String

Enable or disable the no-touch flag for the current client.

Parameters:

  • mode (Symbol, String)

    :on or :off

Returns:

  • (String)

    Server response



260
261
262
# File 'lib/valkey/commands/server_commands.rb', line 260

def client_no_touch(*args)
  send_command(RequestType::CLIENT_NO_TOUCH, args)
end

#client_pause(*args) ⇒ String

Pause processing of commands from clients for a given time.

Examples:

client_pause(1000, :all)

Parameters:

  • timeout (Integer)

    Time in milliseconds to pause clients

  • mode (Symbol)

    Pause mode, e.g., ‘:all` to pause all clients or `:write` to pause writes only

Returns:

  • (String)

    “OK”



182
183
184
# File 'lib/valkey/commands/server_commands.rb', line 182

def client_pause(*args)
  send_command(RequestType::CLIENT_PAUSE, args)
end

#client_reply(*args) ⇒ String

Control client reply behavior (e.g., ON, OFF, SKIP).

Parameters:

  • args (Array)

    Reply mode arguments

Returns:

  • (String)

    Server response



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

def client_reply(*args)
  send_command(RequestType::CLIENT_REPLY, args)
end

#client_set_info(*args) ⇒ String

Set client information fields.

Parameters:

  • args (Array)

    Key-value pairs to set client info fields

Returns:

  • (String)

    Server response



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

def client_set_info(*args)
  send_command(RequestType::CLIENT_SET_INFO, args)
end

#client_set_name(*args) ⇒ String

Set the name of the current connection.

Examples:

client_set_name("my_client")

Parameters:

  • name (String)

    New name for the client connection

Returns:

  • (String)

    “OK” if successful



134
135
136
# File 'lib/valkey/commands/server_commands.rb', line 134

def client_set_name(*args)
  send_command(RequestType::CLIENT_SET_NAME, args)
end

#client_tracking(*args) ⇒ String

Enable or disable client tracking.

Parameters:

  • args (Array)

    Tracking subcommand arguments

Returns:

  • (String)

    Server response



199
200
201
# File 'lib/valkey/commands/server_commands.rb', line 199

def client_tracking(*args)
  send_command(RequestType::CLIENT_TRACKING, args)
end

#client_tracking_infoArray

Get information about client tracking.

Returns:

  • (Array)

    Tracking info



206
207
208
# File 'lib/valkey/commands/server_commands.rb', line 206

def client_tracking_info
  send_command(RequestType::CLIENT_TRACKING_INFO)
end

#client_unblock(*args) ⇒ Integer

Unblock a client by client ID.

Examples:

client_unblock(42)

Parameters:

  • client_id (Integer)

    ID of the client to unblock

Returns:

  • (Integer)

    1 if unblocked, 0 if no client was blocked



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

def client_unblock(*args)
  send_command(RequestType::CLIENT_UNBLOCK, args)
end

#client_unpauseString

Resume processing of commands from clients after a pause.

Examples:

client_unpause

Returns:

  • (String)

    “OK”



191
192
193
# File 'lib/valkey/commands/server_commands.rb', line 191

def client_unpause
  send_command(RequestType::CLIENT_UNPAUSE)
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`



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

Note:

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.

Examples:

Get all configuration parameters

config_get('*')

Get a specific parameter

config_get('maxmemory')

Parameters:

  • args (Array<String>)

    Configuration parameters to get

Returns:

  • (Hash, String)

    Returns a Hash if multiple parameters are requested, otherwise returns a String with the value.



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_resetstatString

Reset the server’s statistics.

Sends the CONFIG RESETSTAT command.

Examples:

config_resetstat

Returns:

  • (String)

    Returns “OK” if successful



79
80
81
# File 'lib/valkey/commands/server_commands.rb', line 79

def config_resetstat
  send_command(RequestType::CONFIG_RESET_STAT)
end

#config_rewriteString

Rewrite the server configuration file.

Sends the CONFIG REWRITE command.

Examples:

config_rewrite

Returns:

  • (String)

    Returns “OK” if successful



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.

Examples:

Set maxmemory to 100mb

config_set('maxmemory', '100mb')

Parameters:

  • args (Array<String>)

    Key-value pairs to set configuration

Returns:

  • (String)

    Returns “OK” if successful



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

def config_set(*args)
  send_command(RequestType::CONFIG_SET, args)
end

#dbsizeInteger

Return the number of keys in the selected database.

Returns:

  • (Integer)


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

def dbsize
  send_command(RequestType::DB_SIZE)
end

#debug(*args) ⇒ Object

RequestType::DEBUG not exist



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

def debug(*args)
  send_command(RequestType::DEBUG, args)
end

#flushall(options = nil) ⇒ String

Remove all keys from all databases.

Parameters:

  • options (Hash) (defaults to: nil)
    • ‘:async => Boolean`: async flush (default: false)

Returns:

  • (String)

    ‘OK`



276
277
278
279
280
281
282
# File 'lib/valkey/commands/server_commands.rb', line 276

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

Parameters:

  • options (Hash) (defaults to: nil)
    • ‘:async => Boolean`: async flush (default: false)

Returns:

  • (String)

    ‘OK`



289
290
291
292
293
294
295
# File 'lib/valkey/commands/server_commands.rb', line 289

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

Parameters:

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

    e.g. “commandstats”

Returns:

  • (Hash<String, String>)


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/valkey/commands/server_commands.rb', line 301

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 = Hash[reply.map do |k, v|
          v = v.split(",").map { |e| e.split("=") }
          [k[/^cmdstat_(.*)$/, 1], Hash[v]]
        end]
      end
    end

    reply
  end
end

#lastsaveInteger

Get the UNIX time stamp of the last successful save to disk.

Returns:

  • (Integer)


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

def lastsave
  send_command(RequestType::LAST_SAVE)
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



332
333
334
335
336
337
338
339
340
# File 'lib/valkey/commands/server_commands.rb', line 332

def monitor
  synchronize do |client|
    client = client.pubsub
    client.call_v([:monitor])
    loop do
      yield client.next_event
    end
  end
end

#saveString

Synchronously save the dataset to disk.

Returns:

  • (String)


345
346
347
# File 'lib/valkey/commands/server_commands.rb', line 345

def save
  send_command(RequestType::SAVE)
end

#shutdownObject

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



350
351
352
353
354
355
356
357
358
359
# File 'lib/valkey/commands/server_commands.rb', line 350

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.



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

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



371
372
373
374
375
# File 'lib/valkey/commands/server_commands.rb', line 371

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

#syncObject

Internal command used for replication.



378
379
380
# File 'lib/valkey/commands/server_commands.rb', line 378

def sync
  send_command(RequestType::SYNC)
end

#timeArray<Integer>

Return the server time.

Examples:

r.time # => [ 1333093196, 606806 ]

Returns:

  • (Array<Integer>)

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



389
390
391
# File 'lib/valkey/commands/server_commands.rb', line 389

def time
  send_command(RequestType::TIME)
end