module Redis::Commands::Keys

def _scan(command, cursor, args, match: nil, count: nil, type: nil, &block)

def _scan(command, cursor, args, match: nil, count: nil, type: nil, &block)
  # SSCAN/ZSCAN/HSCAN already prepend the key to +args+.
  args << cursor
  args << "MATCH" << match if match
  args << "COUNT" << Integer(count) if count
  args << "TYPE" << type if type
  send_command([command] + args, &block)
end

def copy(source, destination, db: nil, replace: false)

Returns:
  • (Boolean) - whether the key was copied or not

Parameters:
  • replace (Boolean) -- removes the `destination` key before copying value to it
  • db (Integer) --
  • destination (String) --
  • source (String) --

Other tags:
    Example: Copy a value to a key in another database -
    Example: Copy a value to another key -
def copy(source, destination, db: nil, replace: false)
  command = [:copy, source, destination]
  command << "DB" << db if db
  command << "REPLACE" if replace
  send_command(command, &Boolify)
end

def del(*keys)

Returns:
  • (Integer) - number of keys that were deleted

Parameters:
  • keys (String, Array) --
def del(*keys)
  keys.flatten!(1)
  return 0 if keys.empty?
  send_command([:del] + keys)
end

def dump(key)

Returns:
  • (String) - serialized_value

Parameters:
  • key (String) --
def dump(key)
  send_command([:dump, key])
end

def exists(*keys)

Returns:
  • (Integer) -

Parameters:
  • keys (String, Array) --
def exists(*keys)
  send_command([:exists, *keys])
end

def exists?(*keys)

Returns:
  • (Boolean) -

Parameters:
  • keys (String, Array) --
def exists?(*keys)
  send_command([:exists, *keys]) do |value|
    value > 0
  end
end

def expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil)

Returns:
  • (Boolean) - whether the timeout was set or not

Parameters:
  • options (Hash) --
  • seconds (Integer) -- time to live
  • key (String) --
def expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil)
  args = [:expire, key, Integer(seconds)]
  args << "NX" if nx
  args << "XX" if xx
  args << "GT" if gt
  args << "LT" if lt
  send_command(args, &Boolify)
end

def expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil)

Returns:
  • (Boolean) - whether the timeout was set or not

Parameters:
  • options (Hash) --
  • unix_time (Integer) -- expiry time specified as a UNIX timestamp
  • key (String) --
def expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil)
  args = [:expireat, key, Integer(unix_time)]
  args << "NX" if nx
  args << "XX" if xx
  args << "GT" if gt
  args << "LT" if lt
  send_command(args, &Boolify)
end

def expiretime(key)

Returns:
  • (Integer) - expiry time specified as number of seconds from UNIX Epoch

Parameters:
  • key (String) --
def expiretime(key)
  send_command([:expiretime, key])
end

def keys(pattern = "*")

Returns:
  • (Array) -

Parameters:
  • pattern (String) --
def keys(pattern = "*")
  send_command([:keys, pattern]) do |reply|
    if reply.is_a?(String)
      reply.split(" ")
    else
      reply
    end
  end
end

def migrate(key, options)

Returns:
  • (String) - `"OK"`

Parameters:
  • options (Hash) --
  • key (String, Array) --
def migrate(key, options)
  args = [:migrate]
  args << (options[:host] || raise(':host not specified'))
  args << (options[:port] || raise(':port not specified'))
  args << (key.is_a?(String) ? key : '')
  args << (options[:db] || @client.db).to_i
  args << (options[:timeout] || @client.timeout).to_i
  args << 'COPY' if options[:copy]
  args << 'REPLACE' if options[:replace]
  args += ['KEYS', *key] if key.is_a?(Array)
  send_command(args)
end

def move(key, db)

Returns:
  • (Boolean) - whether the key was moved or not

Parameters:
  • db (Integer) --
  • key (String) --

Other tags:
    Example: Move a key to another database -
def move(key, db)
  send_command([:move, key, db], &Boolify)
end

def object(*args)

def object(*args)
  send_command([:object] + args)
end

def persist(key)

Returns:
  • (Boolean) - whether the timeout was removed or not

Parameters:
  • key (String) --
def persist(key)
  send_command([:persist, key], &Boolify)
end

def pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil)

Returns:
  • (Boolean) - whether the timeout was set or not

Parameters:
  • options (Hash) --
  • milliseconds (Integer) -- time to live
  • key (String) --
def pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil)
  args = [:pexpire, key, Integer(milliseconds)]
  args << "NX" if nx
  args << "XX" if xx
  args << "GT" if gt
  args << "LT" if lt
  send_command(args, &Boolify)
end

def pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil)

Returns:
  • (Boolean) - whether the timeout was set or not

Parameters:
  • options (Hash) --
  • ms_unix_time (Integer) -- expiry time specified as number of milliseconds from UNIX Epoch.
  • key (String) --
def pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil)
  args = [:pexpireat, key, Integer(ms_unix_time)]
  args << "NX" if nx
  args << "XX" if xx
  args << "GT" if gt
  args << "LT" if lt
  send_command(args, &Boolify)
end

def pexpiretime(key)

Returns:
  • (Integer) - expiry time specified as number of milliseconds from UNIX Epoch

Parameters:
  • key (String) --
def pexpiretime(key)
  send_command([:pexpiretime, key])
end

def pttl(key)

Returns:
  • (Integer) - remaining time to live in milliseconds

Parameters:
  • key (String) --
def pttl(key)
  send_command([:pttl, key])
end

def randomkey

Returns:
  • (String) -
def randomkey
  send_command([:randomkey])
end

def rename(old_name, new_name)

Returns:
  • (String) - `OK`

Parameters:
  • new_name (String) --
  • old_name (String) --
def rename(old_name, new_name)
  send_command([:rename, old_name, new_name])
end

def renamenx(old_name, new_name)

Returns:
  • (Boolean) - whether the key was renamed or not

Parameters:
  • new_name (String) --
  • old_name (String) --
def renamenx(old_name, new_name)
  send_command([:renamenx, old_name, new_name], &Boolify)
end

def restore(key, ttl, serialized_value, replace: nil)

Returns:
  • (String) - `"OK"`

Raises:
  • (Redis::CommandError) -

Parameters:
  • options (Hash) --
  • serialized_value (String) --
  • ttl (String) --
  • key (String) --
def restore(key, ttl, serialized_value, replace: nil)
  args = [:restore, key, ttl, serialized_value]
  args << 'REPLACE' if replace
  send_command(args)
end

def scan(cursor, **options)

Returns:
  • (String, Array) - the next cursor and all found keys

Parameters:
  • options (Hash) --
  • cursor (String, Integer) -- the cursor of the iteration

Other tags:
    Example: Retrieve a batch of keys of a certain type -
    Example: Retrieve a batch of keys matching a pattern -
    Example: Retrieve the first batch of keys -
def scan(cursor, **options)
  _scan(:scan, cursor, [], **options)
end

def scan_each(**options, &block)

Returns:
  • (Enumerator) - an enumerator for all found keys

Parameters:
  • options (Hash) --

Other tags:
    Example: Execute block for each key of a type -
    Example: Execute block for each key matching a pattern -
    Example: Retrieve all of the keys (with possible duplicates) -
def scan_each(**options, &block)
  return to_enum(:scan_each, **options) unless block_given?
  cursor = 0
  loop do
    cursor, keys = scan(cursor, **options)
    keys.each(&block)
    break if cursor == "0"
  end
end

def sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil)

Returns:
  • (Array, Array>, Integer) -

Parameters:
  • options (Hash) --
  • key (String) --

Other tags:
    Example: Store an alphabetically descending list in "target" -
    Example: Retrieve the first 2 elements from an alphabetically sorted "list" -
def sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil)
  args = [:sort, key]
  args << "BY" << by if by
  if limit
    args << "LIMIT"
    args.concat(limit)
  end
  get = Array(get)
  get.each do |item|
    args << "GET" << item
  end
  args.concat(order.split(" ")) if order
  args << "STORE" << store if store
  send_command(args) do |reply|
    if get.size > 1 && !store
      reply.each_slice(get.size).to_a if reply
    else
      reply
    end
  end
end

def ttl(key)

Returns:
  • (Integer) - remaining time to live in seconds.

Parameters:
  • key (String) --
def ttl(key)
  send_command([:ttl, key])
end

def type(key)

Returns:
  • (String) - `string`, `list`, `set`, `zset`, `hash` or `none`

Parameters:
  • key (String) --
def type(key)
  send_command([:type, key])
end

def unlink(*keys)

Returns:
  • (Integer) - number of keys that were unlinked

Parameters:
  • keys (String, Array) --
def unlink(*keys)
  send_command([:unlink] + keys)
end