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:



10
11
12
# File 'lib/oxblood/commands/keys.rb', line 10

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:



20
21
22
# File 'lib/oxblood/commands/keys.rb', line 20

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:



32
33
34
# File 'lib/oxblood/commands/keys.rb', line 32

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:



44
45
46
# File 'lib/oxblood/commands/keys.rb', line 44

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:



56
57
58
# File 'lib/oxblood/commands/keys.rb', line 56

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:



64
65
66
# File 'lib/oxblood/commands/keys.rb', line 64

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:



75
76
77
# File 'lib/oxblood/commands/keys.rb', line 75

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:



88
89
90
# File 'lib/oxblood/commands/keys.rb', line 88

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:



97
98
99
# File 'lib/oxblood/commands/keys.rb', line 97

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:



108
109
110
# File 'lib/oxblood/commands/keys.rb', line 108

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:



119
120
121
# File 'lib/oxblood/commands/keys.rb', line 119

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:



130
131
132
# File 'lib/oxblood/commands/keys.rb', line 130

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:



139
140
141
# File 'lib/oxblood/commands/keys.rb', line 139

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:



152
153
154
# File 'lib/oxblood/commands/keys.rb', line 152

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:



166
167
168
# File 'lib/oxblood/commands/keys.rb', line 166

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:



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

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

  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:



198
199
200
# File 'lib/oxblood/commands/keys.rb', line 198

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:



208
209
210
# File 'lib/oxblood/commands/keys.rb', line 208

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