Module: Oxblood::Commands::Keys

Included in:
Oxblood::Commands
Defined in:
lib/oxblood/commands/keys.rb

Instance Method Summary collapse

Instance Method Details

#del(*keys) ⇒ Integer

Delete a key

Parameters:

  • keys (String, Array<String>)

    to delete

Returns:

  • (Integer)

    the number of keys that were removed

See Also:


12
13
14
# File 'lib/oxblood/commands/keys.rb', line 12

def del(*keys)
  run(*keys.unshift(:DEL))
end

#dump(key) ⇒ String

Return a serialized version of the value stored at specified key.

Parameters:

  • key (String)

Returns:

  • (String)

    serialized value

See Also:


22
23
24
# File 'lib/oxblood/commands/keys.rb', line 22

def dump(key)
  run(:DUMP, key)
end

#exists(*keys) ⇒ Integer

Determine if a key exists

Parameters:

  • keys (String, Array<String>)

    to check

Returns:

  • (Integer)

    the number of keys existing among the ones specified as arguments. Keys mentioned multiple times and existing are counted multiple times.

See Also:


34
35
36
# File 'lib/oxblood/commands/keys.rb', line 34

def exists(*keys)
  run(*keys.unshift(:EXISTS))
end

#expire(key, seconds) ⇒ Integer

Set a key's time to live in seconds

Parameters:

  • key (String)

    to expire

  • seconds (Integer)

    number of seconds

Returns:

  • (Integer)

    1 if the timeout was set. 0 if key does not exist or the timeout could not be set.

See Also:


46
47
48
# File 'lib/oxblood/commands/keys.rb', line 46

def expire(key, seconds)
  run(:EXPIRE, key, seconds)
end

#expireat(key, timestamp) ⇒ Integer

Set the expiration for a key as a UNIX timestamp

Parameters:

  • key (String)
  • timestamp (Integer)

    in UNIX format

Returns:

  • (Integer)

    1 if the timeout was set. 0 if key does not exist or the timeout could not be set.

See Also:


58
59
60
# File 'lib/oxblood/commands/keys.rb', line 58

def expireat(key, timestamp)
  run(:EXPIREAT, key, timestamp)
end

#keys(pattern) ⇒ Object

Find all keys matching the given pattern

Parameters:

  • pattern (String)

    used to match keys

See Also:


66
67
68
# File 'lib/oxblood/commands/keys.rb', line 66

def keys(pattern)
  run(:KEYS, pattern)
end

#move(key, db) ⇒ Integer

Move a key to another database

Parameters:

  • key (String)
  • db (Integer)

    index

Returns:

  • (Integer)

    1 if key was moved and 0 otherwise.

See Also:


77
78
79
# File 'lib/oxblood/commands/keys.rb', line 77

def move(key, db)
  run(:MOVE, key, db)
end

#object(subcommand, key) ⇒ Integer, ...

Inspect the internals of Redis objects

Parameters:

  • subcommand (String)

    `REFCOUNT`, `ENCODING`, `IDLETIME`

  • key (String)

Returns:

  • (Integer)

    in case of `REFCOUNT` and `IDLETIME` subcommands

  • (String)

    in case of `ENCODING` subcommand

  • (nil)

    if object you try to inspect is missing

See Also:


90
91
92
# File 'lib/oxblood/commands/keys.rb', line 90

def object(subcommand, key)
  run(:OBJECT, subcommand, key)
end

#persist(key) ⇒ Integer

Remove expiration from a key

Parameters:

  • key (String)

Returns:

  • (Integer)

    1 if the timeout was removed and 0 otherwise

See Also:


99
100
101
# File 'lib/oxblood/commands/keys.rb', line 99

def persist(key)
  run(:PERSIST, key)
end

#pexpire(key, milliseconds) ⇒ Integer

Set a key's time to live in milliseconds

Parameters:

  • key (String)
  • milliseconds (Integer)

Returns:

  • (Integer)

    1 if the timeout was set and 0 otherwise

See Also:


110
111
112
# File 'lib/oxblood/commands/keys.rb', line 110

def pexpire(key, milliseconds)
  run(:PEXPIRE, key, milliseconds)
end

#pexpireat(key, timestamp) ⇒ Integer

Set the expiration for a key as a UNIX timestamp specified in milliseconds

Parameters:

  • key (String)
  • timestamp (Integer)

    in milliseconds

Returns:

  • (Integer)

    1 if the timeout was set and 0 otherwise

See Also:


121
122
123
# File 'lib/oxblood/commands/keys.rb', line 121

def pexpireat(key, timestamp)
  run(:PEXPIREAT, key, timestamp)
end

#pttl(key) ⇒ Integer

Get the time to live for a key in milliseconds

Parameters:

  • key (String)

Returns:

  • (Integer)

    TTL in milliseconds, or a negative value in order to signal an error

See Also:


132
133
134
# File 'lib/oxblood/commands/keys.rb', line 132

def pttl(key)
  run(:PTTL, key)
end

#randomkeyString?

Return a random key from the keyspace

Returns:

  • (String)

    the random key

  • (nil)

    if database is empty

See Also:


141
142
143
# File 'lib/oxblood/commands/keys.rb', line 141

def randomkey
  run(:RANDOMKEY)
end

#rename(key, newkey) ⇒ String, RError

Rename a key

Parameters:

  • key (String)

    to rename

  • newkey (String)

Returns:

  • (String)

    OK in case of success

  • (RError)

    if key does not exist. Before Redis 3.2.0, an error is returned if source and destination names are the same.

See Also:


154
155
156
# File 'lib/oxblood/commands/keys.rb', line 154

def rename(key, newkey)
  run(:RENAME, key, newkey)
end

#renamenx(key, newkey) ⇒ Integer, RError

Rename a key, only if the new key does not exist

Parameters:

  • key (String)

    to rename

  • newkey (String)

Returns:

  • (Integer)

    1 if key was renamed to newkey. 0 if newkey already exists.

  • (RError)

    if key does not exist. Before Redis 3.2.0, an error is returned if source and destination names are the same.

See Also:


168
169
170
# File 'lib/oxblood/commands/keys.rb', line 168

def renamenx(key, newkey)
  run(:RENAMENX, key, newkey)
end

#restore(key, ttl, serialized_value, opts = {}) ⇒ String, RError

Create a key using the provided serialized value, previously obtained using DUMP

Parameters:

  • key (String)
  • ttl (Integer)

    expire time in milliseconds

  • serialized_value (String)

    obtained using DUMP command

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :replace (Boolean) — default: false

    Override key if it already exists

Returns:

  • (String)

    OK on success

  • (RError)

    if replace is false and key already exists or RDB version and data checksum don't match.

See Also:


186
187
188
189
190
191
# File 'lib/oxblood/commands/keys.rb', line 186

def restore(key, ttl, serialized_value, opts = {})
  args = [:RESTORE, key, ttl, serialized_value]
  args << :REPLACE if opts[:replace]

  run(*args)
end

#scan(cursor, opts = {}) ⇒ Array

Incrementally iterate the keys space

Parameters:

  • cursor (Integer)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :count (Integer)

    Amount of work that should be done at every call in order to retrieve elements from the collection.

  • :match (String)

Returns:

  • (Array)

    two elements array, where the first element is String representing an unsigned 64 bit number (the cursor), and the second element is an Array of elements.

See Also:


227
228
229
230
231
# File 'lib/oxblood/commands/keys.rb', line 227

def scan(cursor, opts = {})
  args = [:SCAN, cursor]
  Scan.merge_opts!(args, opts)
  run(*args)
end

#ttl(key) ⇒ Integer

Get the time to live for a key

Parameters:

  • key (String)

Returns:

  • (Integer)

    TTL in seconds, or a negative value in order to signal an error

See Also:


200
201
202
# File 'lib/oxblood/commands/keys.rb', line 200

def ttl(key)
  run(:TTL, key)
end

#type(key) ⇒ String

Determine the type stored at key

Parameters:

  • key (String)

Returns:

  • (String)

    type of key, or none when key does not exist.

See Also:


210
211
212
# File 'lib/oxblood/commands/keys.rb', line 210

def type(key)
  run(:TYPE, key)
end