123456789_123456789_123456789_123456789_123456789_

Module: Redis::Commands::Server

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/redis/commands/server.rb

Instance Method Summary

Instance Method Details

#bgrewriteaof ⇒ String

Asynchronously rewrite the append-only file.

Returns:

  • (String) —

    OK

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 9

def bgrewriteaof
  send_command([:bgrewriteaof])
end

#bgsave ⇒ String

Asynchronously save the dataset to disk.

Returns:

  • (String) —

    OK

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 16

def bgsave
  send_command([:bgsave])
end

#client(subcommand, *args) ⇒ String, Hash

Manage client connections.

Parameters:

  • subcommand (String, Symbol) —

    e.g. kill, list, getname, setname

Returns:

  • (String, Hash) —

    depends on subcommand

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 39

def client(subcommand, *args)
  send_command([:client, subcommand] + args) do |reply|
    if subcommand.to_s == "list"
      reply.lines.map do |line|
        entries = line.chomp.split(/[ =]/)
        Hash[entries.each_slice(2).to_a]
      end
    else
      reply
    end
  end
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

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 25

def config(action, *args)
  send_command([:config, action] + args) do |reply|
    if reply.is_a?(Array) && action == :get
      Hashify.call(reply)
    else
      reply
    end
  end
end

#dbsize ⇒ Integer

Return the number of keys in the selected database.

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 55

def dbsize
  send_command([:dbsize])
end

#debug(*args)

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 256

def debug(*args)
  send_command([: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

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 64

def flushall(options = nil)
  if options && options[:async]
    send_command(%i[flushall async])
  else
    send_command([:flushall])
  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

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 77

def flushdb(options = nil)
  if options && options[:async]
    send_command(%i[flushdb async])
  else
    send_command([:flushdb])
  end
end

#info(*sections) ⇒ Hash<String, String>

Get information and statistics about the server.

Examples:

Default sections

redis.info

One section

redis.info(:commandstats)
  # => { "get" => { "calls" => "2", ... }, ... }

Several sections at once (Redis 7.0)

redis.info(:server, :clients)

Parameters:

  • sections (Array<String, Symbol>) —

    zero or more sections, e.g. server, clients, commandstats, all, everything. Passing more than one section requires ::Redis 7.0.

Returns:

  • (Hash<String, String>) —

    the parsed INFO output. When commandstats is the only section, the reply is nested per command instead: { "get" => { "calls" => "2", ... } }.

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 99

def info(*sections)
  sections.flatten!(1)
  sections.compact!
  send_command([:info].concat(sections)) do |reply|
    if reply.is_a?(String)
      reply = HashifyInfo.call(reply)

      if sections.size == 1 && sections.first.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

#lastsave ⇒ Integer

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

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 122

def lastsave
  send_command([:lastsave])
end

#monitor {|line| ... }

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

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 132

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

#save ⇒ String

Synchronously save the dataset to disk.

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 145

def save
  send_command([:save])
end

#shutdown(save: nil, now: false, force: false, abort: false) ⇒ nil, String

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

Examples:

Default: save if a save point is configured, then exit

redis.shutdown
  # => nil

Skip the RDB save and don't wait for lagging replicas (Redis 7.0)

redis.shutdown(save: false, now: true)

Cancel an in-progress shutdown (Redis 7.0)

redis.shutdown(abort: true)
  # => "OK"

Parameters:

  • save (Boolean, nil) —

    true forces an RDB save (SAVE), false skips it (NOSAVE); nil leaves the decision to the server's save points

  • now (Boolean) —

    skip waiting for lagging replicas (Redis 7.0)

  • force (Boolean) —

    ignore errors that would normally prevent the shutdown, such as a failed RDB or AOF write (Redis 7.0)

  • abort (Boolean) —

    cancel a shutdown that is waiting for replicas (Redis 7.0)

Returns:

  • (nil) —

    when the server shut down (the connection is closed without a reply)

  • (String) —

    OK when abort: cancelled a pending shutdown

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 205

def shutdown(save: nil, now: false, force: false, abort: false)
  args = [:shutdown]
  args << (save ? "SAVE" : "NOSAVE") unless save.nil?
  args << "NOW" if now
  args << "FORCE" if force
  args << "ABORT" if abort

  synchronize do |client|
    client.disable_reconnection do
      client.call_v(args)
    rescue ConnectionError
      # This means Redis has probably exited.
      nil
    end
  end
end

#slaveof(host, port)

Make the server a slave of another instance, or promote it as master.

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 223

def slaveof(host, port)
  send_command([:slaveof, 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

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 232

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

#sync

Internal command used for replication.

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 239

def sync
  send_command([:sync])
end

#time ⇒ Array<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

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 250

def time
  send_command([:time]) do |reply|
    reply&.map(&:to_i)
  end
end

#waitaof(numlocal, numreplicas, timeout) ⇒ Array<Integer>

Block until the write commands previously sent on this connection have been fsynced to the append-only file of the local server and/or of the given number of replicas.

The reply always reports how many local servers and replicas had fsynced the writes when the command returned, whether or not the requested levels were reached, so compare it against what you asked for. Inside multi the command does not block.

On Redis::Cluster the command is sent to every primary and each count is the minimum across them (the server's agg_min policy): local is 1 only if every primary fsynced, and replicas is the fewest acks any shard received. This per-connection guarantee does not hold inside pipelined/multi on cluster: WAITAOF carries no write flag, so the driver routes it to an arbitrary node (a random primary, or a replica when replica: true) rather than the node(s) that carried the pipeline's writes, so the reply is shape-correct but unrelated to those writes.

Examples:

Wait for the local AOF fsync

redis.set("foo", "bar")
redis.waitaof(1, 0, 0)
  # => [1, 0]

Wait at most one second for one replica

redis.waitaof(0, 1, 1000)
  # => [1, 0]

Parameters:

  • numlocal (Integer) —

    1 to wait for the local AOF fsync, 0 to skip it. Requires appendonly yes on the server when set.

  • numreplicas (Integer) —

    number of replicas that must acknowledge the fsync

  • timeout (Integer) —

    maximum time to wait in milliseconds, 0 blocks indefinitely

Returns:

  • (Array<Integer>) —

    [local, replicas]: the number of local servers (0 or 1) and replicas that had fsynced the writes

[ GitHub ]

  
# File 'lib/redis/commands/server.rb', line 179

def waitaof(numlocal, numreplicas, timeout)
  timeout = Integer(timeout)
  command = [:waitaof, Integer(numlocal), Integer(numreplicas), timeout]
  send_blocking_command(command, timeout / 1000.0)
end