Module: RedisCluster::Function::Key

Included in:
RedisCluster::Function
Defined in:
lib/redis_cluster/function/key.rb

Overview

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]

Instance Method Summary collapse

Instance Method Details

#del(key) ⇒ Boolean

Delete one key.

Parameters:

Returns:

  • (Boolean)

    whether the key was deleted or not



20
21
22
# File 'lib/redis_cluster/function/key.rb', line 20

def del(key)
  call(key, [:del, key], transform: Redis::Boolify)
end

#dump(key) ⇒ String

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

Parameters:

Returns:

  • (String)

    serialized_value



103
104
105
# File 'lib/redis_cluster/function/key.rb', line 103

def dump(key)
  call(key, [:dump, key], read: true)
end

#exists(key) ⇒ Boolean

Determine if a key exists.

Parameters:

Returns:

  • (Boolean)


46
47
48
# 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) ⇒ Boolean

Set a key’s time to live in seconds.

Parameters:

  • key (String)
  • seconds (Fixnum)

    time to live

Returns:

  • (Boolean)

    whether the timeout was set or not



29
30
31
# 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) ⇒ Boolean

Set a key’s time to live in milliseconds.

Parameters:

  • key (String)
  • milliseconds (Fixnum)

    time to live

Returns:

  • (Boolean)

    whether the timeout was set or not



38
39
40
# File 'lib/redis_cluster/function/key.rb', line 38

def pexpire(key, milliseconds)
  call(key, [:pexpire, key, milliseconds], transform: Redis::Boolify)
end

#pttl(key) ⇒ Fixnum

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

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.

Parameters:

Returns:

  • (Fixnum)

    remaining time to live in milliseconds



72
73
74
# 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 = {}) ⇒ String

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

Parameters:

  • key (String)
  • ttl (String)
  • serialized_value (String)
  • options (Hash)
    • ‘replace: true`: replace existing key

Returns:



92
93
94
95
96
97
# 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) ⇒ Fixnum

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

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.

Parameters:

Returns:

  • (Fixnum)

    remaining time to live in seconds.



59
60
61
# File 'lib/redis_cluster/function/key.rb', line 59

def ttl(key)
  call(key, [:ttl, key], read: true)
end

#type(key) ⇒ String

Determine the type stored at key.

Parameters:

Returns:

  • (String)

    ‘string`, `list`, `set`, `zset`, `hash` or `none`



80
81
82
# File 'lib/redis_cluster/function/key.rb', line 80

def type(key)
  call(key, [:type, key], read: true)
end