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

#bgrewriteaofString

Asynchronously rewrite the append-only file.

Returns:

  • (String)

    OK

[ GitHub ]

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

def bgrewriteaof
  send_command([:bgrewriteaof])
end

#bgsaveString

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

#dbsizeInteger

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 220

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

#lastsaveInteger

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

#saveString

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 169

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 187

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 196

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 203

def sync
  send_command([: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

[ GitHub ]

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

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