Module: Redis::Commands::Keys
- Included in:
- Redis::Commands
- Defined in:
- lib/redis/commands/keys.rb
Instance Method Summary collapse
-
#copy(source, destination, db: nil, replace: false) ⇒ Boolean
Copy a value from one key to another.
-
#del(*keys) ⇒ Integer
Delete one or more keys.
-
#dump(key) ⇒ String
Return a serialized version of the value stored at a key.
-
#exists(*keys) ⇒ Integer
Determine how many of the keys exists.
-
#exists?(*keys) ⇒ Boolean
Determine if any of the keys exists.
-
#expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set a key’s time to live in seconds.
-
#expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set the expiration for a key as a UNIX timestamp.
-
#expiretime(key) ⇒ Integer
Get a key’s expiry time specified as number of seconds from UNIX Epoch.
-
#keys(pattern = "*") ⇒ Array<String>
Find all keys matching the given pattern.
-
#migrate(key, options) ⇒ String
Transfer a key from the connected instance to another instance.
-
#move(key, db) ⇒ Boolean
Move a key to another database.
- #object(*args) ⇒ Object
-
#persist(key) ⇒ Boolean
Remove the expiration from a key.
-
#pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set a key’s time to live in milliseconds.
-
#pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set the expiration for a key as number of milliseconds from UNIX Epoch.
-
#pexpiretime(key) ⇒ Integer
Get a key’s expiry time specified as number of milliseconds from UNIX Epoch.
-
#pttl(key) ⇒ Integer
Get the time to live (in milliseconds) for a key.
-
#randomkey ⇒ String
Return a random key from the keyspace.
-
#rename(old_name, new_name) ⇒ String
Rename a key.
-
#renamenx(old_name, new_name) ⇒ Boolean
Rename a key, only if the new key does not exist.
-
#restore(key, ttl, serialized_value, replace: nil) ⇒ String
Create a key using the serialized value, previously obtained using DUMP.
-
#scan(cursor, **options) ⇒ String+
Scan the keyspace.
-
#scan_each(**options, &block) ⇒ Enumerator
Scan the keyspace.
-
#sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil) ⇒ Array<String>, ...
Sort the elements in a list, set or sorted set.
-
#ttl(key) ⇒ Integer
Get the time to live (in seconds) for a key.
-
#type(key) ⇒ String
Determine the type stored at key.
-
#unlink(*keys) ⇒ Integer
Unlink one or more keys.
Instance Method Details
#copy(source, destination, db: nil, replace: false) ⇒ Boolean
Copy a value from one key to another.
343 344 345 346 347 348 349 |
# File 'lib/redis/commands/keys.rb', line 343 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 |
#del(*keys) ⇒ Integer
Delete one or more keys.
248 249 250 251 252 253 |
# File 'lib/redis/commands/keys.rb', line 248 def del(*keys) keys.flatten!(1) return 0 if keys.empty? send_command([:del] + keys) end |
#dump(key) ⇒ String
Return a serialized version of the value stored at a key.
199 200 201 |
# File 'lib/redis/commands/keys.rb', line 199 def dump(key) send_command([:dump, key]) end |
#exists(*keys) ⇒ Integer
Determine how many of the keys exists.
267 268 269 |
# File 'lib/redis/commands/keys.rb', line 267 def exists(*keys) send_command([:exists, *keys]) end |
#exists?(*keys) ⇒ Boolean
Determine if any of the keys exists.
275 276 277 278 279 |
# File 'lib/redis/commands/keys.rb', line 275 def exists?(*keys) send_command([:exists, *keys]) do |value| value > 0 end end |
#expire(key, seconds, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set a key’s time to live in seconds.
78 79 80 81 82 83 84 85 86 |
# File 'lib/redis/commands/keys.rb', line 78 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 |
#expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set the expiration for a key as a UNIX timestamp.
98 99 100 101 102 103 104 105 106 |
# File 'lib/redis/commands/keys.rb', line 98 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 |
#expiretime(key) ⇒ Integer
Get a key’s expiry time specified as number of seconds from UNIX Epoch
112 113 114 |
# File 'lib/redis/commands/keys.rb', line 112 def expiretime(key) send_command([:expiretime, key]) end |
#keys(pattern = "*") ⇒ Array<String>
Find all keys matching the given pattern.
285 286 287 288 289 290 291 292 293 |
# File 'lib/redis/commands/keys.rb', line 285 def keys(pattern = "*") send_command([:keys, pattern]) do |reply| if reply.is_a?(String) reply.split(" ") else reply end end end |
#migrate(key, options) ⇒ String
Transfer a key from the connected instance to another instance.
230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/redis/commands/keys.rb', line 230 def migrate(key, ) args = [:migrate] args << ([:host] || raise(':host not specified')) args << ([:port] || raise(':port not specified')) args << (key.is_a?(String) ? key : '') args << ([:db] || @client.db).to_i args << ([:timeout] || @client.timeout).to_i args << 'COPY' if [:copy] args << 'REPLACE' if [:replace] args += ['KEYS', *key] if key.is_a?(Array) send_command(args) end |
#move(key, db) ⇒ Boolean
Move a key to another database.
314 315 316 |
# File 'lib/redis/commands/keys.rb', line 314 def move(key, db) send_command([:move, key, db], &Boolify) end |
#object(*args) ⇒ Object
351 352 353 |
# File 'lib/redis/commands/keys.rb', line 351 def object(*args) send_command([:object] + args) end |
#persist(key) ⇒ Boolean
Remove the expiration from a key.
64 65 66 |
# File 'lib/redis/commands/keys.rb', line 64 def persist(key) send_command([:persist, key], &Boolify) end |
#pexpire(key, milliseconds, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set a key’s time to live in milliseconds.
142 143 144 145 146 147 148 149 150 |
# File 'lib/redis/commands/keys.rb', line 142 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 |
#pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil) ⇒ Boolean
Set the expiration for a key as number of milliseconds from UNIX Epoch.
162 163 164 165 166 167 168 169 170 |
# File 'lib/redis/commands/keys.rb', line 162 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 |
#pexpiretime(key) ⇒ Integer
Get a key’s expiry time specified as number of milliseconds from UNIX Epoch
176 177 178 |
# File 'lib/redis/commands/keys.rb', line 176 def pexpiretime(key) send_command([:pexpiretime, key]) end |
#pttl(key) ⇒ Integer
Get the time to live (in milliseconds) for a key.
In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has no associated expire.
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.
191 192 193 |
# File 'lib/redis/commands/keys.rb', line 191 def pttl(key) send_command([:pttl, key]) end |
#randomkey ⇒ String
Return a random key from the keyspace.
358 359 360 |
# File 'lib/redis/commands/keys.rb', line 358 def randomkey send_command([:randomkey]) end |
#rename(old_name, new_name) ⇒ String
Rename a key. If the new key already exists it is overwritten.
367 368 369 |
# File 'lib/redis/commands/keys.rb', line 367 def rename(old_name, new_name) send_command([:rename, old_name, new_name]) end |
#renamenx(old_name, new_name) ⇒ Boolean
Rename a key, only if the new key does not exist.
376 377 378 |
# File 'lib/redis/commands/keys.rb', line 376 def renamenx(old_name, new_name) send_command([:renamenx, old_name, new_name], &Boolify) end |
#restore(key, ttl, serialized_value, replace: nil) ⇒ String
Create a key using the serialized value, previously obtained using DUMP.
212 213 214 215 216 217 |
# File 'lib/redis/commands/keys.rb', line 212 def restore(key, ttl, serialized_value, replace: nil) args = [:restore, key, ttl, serialized_value] args << 'REPLACE' if replace send_command(args) end |
#scan(cursor, **options) ⇒ String+
Scan the keyspace
25 26 27 |
# File 'lib/redis/commands/keys.rb', line 25 def scan(cursor, **) _scan(:scan, cursor, [], **) end |
#scan_each(**options, &block) ⇒ Enumerator
Scan the keyspace
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/redis/commands/keys.rb', line 49 def scan_each(**, &block) return to_enum(:scan_each, **) unless block_given? cursor = 0 loop do cursor, keys = scan(cursor, **) keys.each(&block) break if cursor == "0" end end |
#sort(key, by: nil, limit: nil, get: nil, order: nil, store: nil) ⇒ Array<String>, ...
Sort the elements in a list, set or sorted set.
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/redis/commands/keys.rb', line 405 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 |
#ttl(key) ⇒ Integer
Get the time to live (in seconds) for a key.
In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has no associated expire.
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.
128 129 130 |
# File 'lib/redis/commands/keys.rb', line 128 def ttl(key) send_command([:ttl, key]) end |
#type(key) ⇒ String
Determine the type stored at key.
435 436 437 |
# File 'lib/redis/commands/keys.rb', line 435 def type(key) send_command([:type, key]) end |
#unlink(*keys) ⇒ Integer
Unlink one or more keys.
259 260 261 |
# File 'lib/redis/commands/keys.rb', line 259 def unlink(*keys) send_command([:unlink] + keys) end |