Module: Valkey::Commands::ConnectionCommands

Included in:
Valkey::Commands
Defined in:
lib/valkey/commands/connection_commands.rb

Overview

This module contains commands related to connection management.

Instance Method Summary collapse

Instance Method Details

#auth(*args) ⇒ String

Authenticate to the server.

Parameters:

  • args (Array<String>)

    includes both username and password or only password

Returns:

  • (String)

    ‘OK`



15
16
17
# File 'lib/valkey/commands/connection_commands.rb', line 15

def auth(*args)
  send_command(RequestType::AUTH, args)
end

#client(subcommand, *args) ⇒ Object

Send a generic CLIENT subcommand.

Examples:

client(:id)                  # => 12345
client(:set_name, "my_app")  # => "OK"
client(:list)                # => [{"id" => "1", ...}, ...]

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



96
97
98
# File 'lib/valkey/commands/connection_commands.rb', line 96

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

#client_caching(mode) ⇒ String

Enable/disable client caching.

Parameters:

  • mode (String)

    Caching mode (YES, NO)

Returns:

  • (String)

    ‘OK`



243
244
245
# File 'lib/valkey/commands/connection_commands.rb', line 243

def client_caching(mode)
  send_command(RequestType::CLIENT_CACHING, [mode])
end

#client_get_nameString?

Get the current client’s name.

Returns:

  • (String, nil)

    Client name or nil if not set



110
111
112
# File 'lib/valkey/commands/connection_commands.rb', line 110

def client_get_name
  send_command(RequestType::CLIENT_GET_NAME)
end

#client_getredirInteger

Get the client ID used for tracking redirection.

Returns:

  • (Integer)

    Client ID for tracking redirection



273
274
275
# File 'lib/valkey/commands/connection_commands.rb', line 273

def client_getredir
  send_command(RequestType::CLIENT_GET_REDIR)
end

#client_idInteger

Get the current client’s ID.

Returns:

  • (Integer)

    Unique client ID



103
104
105
# File 'lib/valkey/commands/connection_commands.rb', line 103

def client_id
  send_command(RequestType::CLIENT_ID)
end

#client_infoString

Get information about the current client connection.

Returns:

  • (String)

    Client connection information



148
149
150
# File 'lib/valkey/commands/connection_commands.rb', line 148

def client_info
  send_command(RequestType::CLIENT_INFO)
end

#client_kill(addr = nil, **options) ⇒ Integer

Kill client connections.

Parameters:

  • addr (String) (defaults to: nil)

    Client address (ip:port)

  • options (Hash)

    Optional filters (id, type, user, addr, laddr, skipme)

Returns:

  • (Integer)

    Number of clients killed



157
158
159
160
161
162
163
# File 'lib/valkey/commands/connection_commands.rb', line 157

def client_kill(addr = nil, **options)
  if addr && options.empty?
    send_command(RequestType::CLIENT_KILL_SIMPLE, [addr])
  else
    send_command(RequestType::CLIENT_KILL, build_client_kill_args(addr, options))
  end
end

#client_kill_simple(addr) ⇒ String

Kill a client connection by address (simple form).

Parameters:

  • addr (String)

    Client address (ip:port)

Returns:

  • (String)

    ‘OK`



169
170
171
# File 'lib/valkey/commands/connection_commands.rb', line 169

def client_kill_simple(addr)
  send_command(RequestType::CLIENT_KILL_SIMPLE, [addr])
end

#client_list(type: nil, ids: nil) ⇒ Array<Hash>

Get a list of client connections.

Parameters:

  • type (String) (defaults to: nil)

    Optional filter by client type (normal, master, slave, pubsub)

  • ids (Array<String>) (defaults to: nil)

    Optional filter by client IDs

Returns:

  • (Array<Hash>)

    List of clients, each represented as a Hash of attributes



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/valkey/commands/connection_commands.rb', line 127

def client_list(type: nil, ids: nil)
  args = []

  args << "TYPE" << type if type

  if ids
    args << "ID"
    args.concat(Array(ids))
  end

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

#client_no_evict(mode) ⇒ String

Enable/disable client no-evict mode.

Parameters:

  • mode (String)

    Mode (ON, OFF)

Returns:

  • (String)

    ‘OK`



281
282
283
# File 'lib/valkey/commands/connection_commands.rb', line 281

def client_no_evict(mode)
  send_command(RequestType::CLIENT_NO_EVICT, [mode.to_s.upcase])
end

#client_no_touch(mode) ⇒ String

Enable/disable client no-touch mode.

Parameters:

  • mode (String)

    Mode (ON, OFF)

Returns:

  • (String)

    ‘OK`



289
290
291
# File 'lib/valkey/commands/connection_commands.rb', line 289

def client_no_touch(mode)
  send_command(RequestType::CLIENT_NO_TOUCH, [mode.to_s.upcase])
end

#client_pause(timeout, mode = nil) ⇒ String

Pause client processing.

Parameters:

  • timeout (Integer)

    Pause duration in milliseconds

  • mode (String) (defaults to: nil)

    Optional mode (WRITE, ALL)

Returns:

  • (String)

    ‘OK`



198
199
200
201
202
# File 'lib/valkey/commands/connection_commands.rb', line 198

def client_pause(timeout, mode = nil)
  args = [timeout]
  args << mode if mode
  send_command(RequestType::CLIENT_PAUSE, args)
end

#client_reply(mode) ⇒ String

Configure client reply mode.

Parameters:

  • mode (String)

    Reply mode (ON, OFF, SKIP)

Returns:

  • (String)

    ‘OK`



215
216
217
# File 'lib/valkey/commands/connection_commands.rb', line 215

def client_reply(mode)
  send_command(RequestType::CLIENT_REPLY, [mode])
end

#client_set_info(attr, value) ⇒ String

Set client connection information.

Parameters:

  • attr (String)

    Attribute to set (lib-name, lib-ver)

  • value (String)

    Value to set for the attribute

Returns:

  • (String)

    ‘OK`



235
236
237
# File 'lib/valkey/commands/connection_commands.rb', line 235

def client_set_info(attr, value)
  send_command(RequestType::CLIENT_SET_INFO, [attr, value])
end

#client_set_name(name) ⇒ String

Set the current client’s name.

Parameters:

  • name (String)

    New name for the client connection

Returns:

  • (String)

    ‘OK`



118
119
120
# File 'lib/valkey/commands/connection_commands.rb', line 118

def client_set_name(name)
  send_command(RequestType::CLIENT_SET_NAME, [name])
end

#client_tracking(status, *args, **options) ⇒ String

Configure client tracking.

Examples:

Positional style

client_tracking("ON", "OPTIN")

Keyword style

client_tracking("ON", optin: true, redirect: 123)

Parameters:

  • status (String)

    Tracking status (ON, OFF)

  • args (Array)

    Additional positional arguments (REDIRECT, PREFIX, BCAST, OPTIN, OPTOUT, NOLOOP)

  • options (Hash)

    Optional parameters (for keyword argument style)

Returns:

  • (String)

    ‘OK`



257
258
259
260
261
# File 'lib/valkey/commands/connection_commands.rb', line 257

def client_tracking(status, *args, **options)
  cmd_args = [status]
  cmd_args.concat(args.any? ? args : build_client_tracking_args(options))
  send_command(RequestType::CLIENT_TRACKING, cmd_args)
end

#client_tracking_infoArray

Get client tracking information.

Returns:

  • (Array)

    Tracking information



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

def client_tracking_info
  send_command(RequestType::CLIENT_TRACKING_INFO)
end

#client_unblock(client_id, unblock_type = nil) ⇒ Integer

Unblock a client blocked in a blocking operation.

Parameters:

  • client_id (Integer)

    ID of the client to unblock

  • unblock_type (String) (defaults to: nil)

    Optional unblock type (TIMEOUT, ERROR)

Returns:

  • (Integer)

    1 if client was unblocked, 0 otherwise



224
225
226
227
228
# File 'lib/valkey/commands/connection_commands.rb', line 224

def client_unblock(client_id, unblock_type = nil)
  args = [client_id]
  args << unblock_type if unblock_type
  send_command(RequestType::CLIENT_UNBLOCK, args)
end

#client_unpauseString

Unpause client processing.

Returns:

  • (String)

    ‘OK`



207
208
209
# File 'lib/valkey/commands/connection_commands.rb', line 207

def client_unpause
  send_command(RequestType::CLIENT_UNPAUSE)
end

#echo(value) ⇒ String

Echo the given string.

Parameters:

  • value (String)

Returns:

  • (String)


31
32
33
# File 'lib/valkey/commands/connection_commands.rb', line 31

def echo(value)
  send_command(RequestType::ECHO, [value])
end

#hello(protover = 3, **options) ⇒ Array

Switch to a different protocol version and handshake with the server.

Parameters:

  • protover (Integer) (defaults to: 3)

    Protocol version (2 or 3)

  • options (Hash)

    Optional parameters like AUTH, SETNAME

Returns:

  • (Array)

    Server information as flat array (TODO: should be Hash for RESP3)



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/valkey/commands/connection_commands.rb', line 67

def hello(protover = 3, **options)
  args = [protover]

  if options[:auth]
    args << "AUTH"
    args.concat(Array(options[:auth]))
  end

  args << "SETNAME" << options[:setname] if options[:setname]

  send_command(RequestType::HELLO, args)
end

#ping(message = nil) ⇒ String

Ping the server.

Parameters:

  • message (optional, String) (defaults to: nil)

Returns:

  • (String)

    ‘PONG`



23
24
25
# File 'lib/valkey/commands/connection_commands.rb', line 23

def ping(message = nil)
  send_command(RequestType::PING, [message].compact)
end

#quitString

Deprecated.

The QUIT command is deprecated since Redis 7.2.0 / Valkey 7.2+. Clients should use the ‘close` method directly instead. This avoids lingering TIME_WAIT sockets on the server side.

Close the connection.

Returns:

  • (String)

    ‘OK` or nil if connection already closed

See Also:



51
52
53
54
55
56
57
58
59
60
# File 'lib/valkey/commands/connection_commands.rb', line 51

def quit
  # For compatibility, we still support QUIT but recommend using close() instead
  send_command(RequestType::QUIT)
rescue ConnectionError
  # Server closes connection immediately after QUIT
  nil
ensure
  # Clean up our side of the connection
  close if respond_to?(:close)
end

#resetString

Reset the connection state.

Returns:

  • (String)

    ‘RESET`



83
84
85
# File 'lib/valkey/commands/connection_commands.rb', line 83

def reset
  send_command(RequestType::RESET)
end

#select(db) ⇒ String

Change the selected database for the current connection.

Parameters:

  • db (Integer)

    zero-based index of the DB to use (0 to 15)

Returns:

  • (String)

    ‘OK`



39
40
41
# File 'lib/valkey/commands/connection_commands.rb', line 39

def select(db)
  send_command(RequestType::SELECT, [db])
end