module RedisCluster::Function::Key

Key implement redis keys commands. There will be some adjustment for cluster. see redis.io/commands#generic. Most of the code are copied from github.com/redis/redis-rb/blob/master/lib/redis.rb.

SETTER = [:del, :expire, :pexpire, :restore] GETTER = [:exists, :ttl, :pttl, :type]

Public Instance Methods

del(key) click to toggle source

Delete one key.

@param [String] key @return [Boolean] whether the key was deleted or not

# File lib/redis_cluster/function/key.rb, line 20
def del(key)
  call(key, [:del, key], transform: Redis::Boolify)
end
dump(key) click to toggle source

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

@param [String] key @return [String] serialized_value

# File lib/redis_cluster/function/key.rb, line 103
def dump(key)
  call(key, [:dump, key], read: true)
end
exists(key) click to toggle source

Determine if a key exists.

@param [String] key @return [Boolean]

# File lib/redis_cluster/function/key.rb, line 46
def exists(key)
  call(key, [:exists, key], transform: Redis::Boolify, read: true)
end
expire(key, seconds) click to toggle source

Set a key's time to live in seconds.

@param [String] key @param [Fixnum] seconds time to live @return [Boolean] whether the timeout was set or not

# File lib/redis_cluster/function/key.rb, line 29
def expire(key, seconds)
  call(key, [:expire, key, seconds], transform: Redis::Boolify)
end
pexpire(key, milliseconds) click to toggle source

Set a key's time to live in milliseconds.

@param [String] key @param [Fixnum] milliseconds time to live @return [Boolean] whether the timeout was set or not

# File lib/redis_cluster/function/key.rb, line 38
def pexpire(key, milliseconds)
  call(key, [:pexpire, key, milliseconds], transform: Redis::Boolify)
end
pttl(key) click to toggle source

Get the time to live (in milliseconds) for a key.

@param [String] key @return [Fixnum] remaining time to live in milliseconds

Starting with Redis 2.8 the return value in case of error changed:

- The command returns -2 if the key does not exist.
- The command returns -1 if the key exists but has no associated expire.
# File lib/redis_cluster/function/key.rb, line 72
def pttl(key)
  call(key, [:pttl, key], read: true)
end
restore(key, ttl, serialized_value, option = {}) click to toggle source

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

@param [String] key @param [String] ttl @param [String] serialized_value @param [Hash] options

- `replace: true`: replace existing key

@return [String] `“OK”`

# File lib/redis_cluster/function/key.rb, line 92
def restore(key, ttl, serialized_value, option = {})
  args = [:restore, key, ttl, serialized_value]
  args << 'REPLACE' if option[:replace]

  call(key, args)
end
ttl(key) click to toggle source

Get the time to live (in seconds) for a key.

@param [String] key @return [Fixnum] remaining time to live in seconds.

Starting with Redis 2.8 the return value in case of error changed:

- The command returns -2 if the key does not exist.
- The command returns -1 if the key exists but has no associated expire.
# File lib/redis_cluster/function/key.rb, line 59
def ttl(key)
  call(key, [:ttl, key], read: true)
end
type(key) click to toggle source

Determine the type stored at key.

@param [String] key @return [String] `string`, `list`, `set`, `zset`, `hash` or `none`

# File lib/redis_cluster/function/key.rb, line 80
def type(key)
  call(key, [:type, key], read: true)
end