Module: Protocol::Redis::Methods::Generic
- Defined in:
- lib/protocol/redis/methods/generic.rb
Overview
Methods for interacting with Redis keys.
Instance Method Summary collapse
-
#del(*keys) ⇒ Object
Delete a key.
-
#dump(key) ⇒ Object
Return a serialized version of the value stored at the specified key.
-
#exists(key, *keys) ⇒ Object
Determine if a key exists.
-
#exists?(key, *keys) ⇒ Boolean
Boolean oversion of ‘exists`.
-
#expire(key, seconds) ⇒ Object
Set a key’s time to live in seconds.
-
#expireat(key, time) ⇒ Object
Set the expiration for a key as a UNIX timestamp.
-
#keys(pattern) ⇒ Object
Find all keys matching the given pattern.
-
#migrate(host, port, destination = 0, keys:, timeout: 0, copy: false, replace: false, auth: nil) ⇒ Object
Atomically transfer a key from a Redis instance to another one.
-
#move(key, db) ⇒ Object
Move a key to another database.
-
#object(subcommand, *arguments) ⇒ Object
Inspect the internals of Redis objects.
-
#persist(key) ⇒ Object
Remove the expiration from a key.
-
#pexpire(key, milliseconds) ⇒ Object
Set a key’s time to live in milliseconds.
-
#pexpireat(key, time) ⇒ Object
Set the expiration for a key as a UNIX timestamp specified in milliseconds.
-
#pttl(key) ⇒ Object
Get the time to live for a key in milliseconds.
-
#randomkey ⇒ Object
Return a random key from the keyspace.
-
#rename(key, new_key) ⇒ Object
Rename a key.
-
#renamenx(key, new_key) ⇒ Object
Rename a key, only if the new key does not exist.
-
#restore(key, serialized_value, ttl = 0) ⇒ Object
Create a key using the provided serialized value, previously obtained using DUMP.
-
#scan(cursor, match: nil, count: nil, type: nil) ⇒ Object
Incrementally iterate the keys space.
-
#sort(key, by: nil, offset: nil, count: nil, get: nil, order: "ASC", alpha: false, store: nil) ⇒ Object
Sort the elements in a list, set or sorted set.
-
#touch(key, *keys) ⇒ Object
Alters the last access time of a key(s).
-
#ttl(key) ⇒ Object
Get the time to live for a key.
-
#type(key) ⇒ Object
Determine the type stored at key.
-
#unlink(key) ⇒ Object
Delete a key asynchronously in another thread.
-
#wait(numreplicas, timeout = 0) ⇒ Object
Wait for the synchronous replication of all the write commands sent in the context of the current connection.
Instance Method Details
#del(*keys) ⇒ Object
Delete a key. O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1). See <redis.io/commands/del> for more details.
17 18 19 20 21 |
# File 'lib/protocol/redis/methods/generic.rb', line 17 def del(*keys) if keys.any? call("DEL", *keys) end end |
#dump(key) ⇒ Object
Return a serialized version of the value stored at the specified key. O(1) to access the key and additional O(N*M) to serialized it, where N is the number of Redis objects composing the value and M their average size. For small string values the time complexity is thus O(1)+O(1*M) where M is small, so simply O(1). See <redis.io/commands/dump> for more details.
26 27 28 |
# File 'lib/protocol/redis/methods/generic.rb', line 26 def dump(key) call("DUMP", key) end |
#exists(key, *keys) ⇒ Object
Determine if a key exists. O(1). See <redis.io/commands/exists> for more details.
34 35 36 |
# File 'lib/protocol/redis/methods/generic.rb', line 34 def exists(key, *keys) call("EXISTS", key, *keys) end |
#exists?(key, *keys) ⇒ Boolean
Boolean oversion of ‘exists`
41 42 43 |
# File 'lib/protocol/redis/methods/generic.rb', line 41 def exists?(key, *keys) exists(key, *keys) > 0 end |
#expire(key, seconds) ⇒ Object
Set a key’s time to live in seconds. O(1). See <redis.io/commands/expire> for more details.
49 50 51 |
# File 'lib/protocol/redis/methods/generic.rb', line 49 def expire(key, seconds) call("EXPIRE", key, seconds) end |
#expireat(key, time) ⇒ Object
Set the expiration for a key as a UNIX timestamp. O(1). See <redis.io/commands/expireat> for more details.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/protocol/redis/methods/generic.rb', line 57 def expireat(key, time) case time when DateTime, Time, Date = time.strftime("%s").to_i else = time end call("EXPIREAT", key, ) end |
#keys(pattern) ⇒ Object
Find all keys matching the given pattern. O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length. See <redis.io/commands/keys> for more details.
71 72 73 |
# File 'lib/protocol/redis/methods/generic.rb', line 71 def keys(pattern) call("KEYS", pattern) end |
#migrate(host, port, destination = 0, keys:, timeout: 0, copy: false, replace: false, auth: nil) ⇒ Object
Atomically transfer a key from a Redis instance to another one. This command actually executes a DUMP+DEL in the source instance, and a RESTORE in the target instance. See the pages of these commands for time complexity. Also an O(N) data transfer between the two instances is performed. See <redis.io/commands/migrate> for more details.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/protocol/redis/methods/generic.rb', line 84 def migrate(host, port, destination = 0, keys:, timeout: 0, copy: false, replace: false, auth: nil) raise ArgumentError, "Must provide keys" if keys.empty? arguments = [host, port] if keys.size == 1 arguments.append(*keys) else arguments.append("") end arguments.append(destination, timeout) if copy arguments.append("COPY") end if replace arguments.append("REPLACE") end if auth arguments.append("AUTH", auth) end if keys.size > 1 arguments.append("KEYS", *keys) end call("MIGRATE", *arguments) end |
#move(key, db) ⇒ Object
Move a key to another database. O(1). See <redis.io/commands/move> for more details.
120 121 122 |
# File 'lib/protocol/redis/methods/generic.rb', line 120 def move(key, db) call("MOVE", key, db) end |
#object(subcommand, *arguments) ⇒ Object
Inspect the internals of Redis objects. O(1) for all the currently implemented subcommands. See <redis.io/commands/object> for more details.
128 129 130 |
# File 'lib/protocol/redis/methods/generic.rb', line 128 def object(subcommand, *arguments) call("OBJECT", subcommand, *arguments) end |
#persist(key) ⇒ Object
Remove the expiration from a key. O(1). See <redis.io/commands/persist> for more details.
135 136 137 |
# File 'lib/protocol/redis/methods/generic.rb', line 135 def persist(key) call("PERSIST", key) end |
#pexpire(key, milliseconds) ⇒ Object
Set a key’s time to live in milliseconds. O(1). See <redis.io/commands/pexpire> for more details.
143 144 145 |
# File 'lib/protocol/redis/methods/generic.rb', line 143 def pexpire(key, milliseconds) call("PEXPIRE", key, milliseconds) end |
#pexpireat(key, time) ⇒ Object
Set the expiration for a key as a UNIX timestamp specified in milliseconds. O(1). See <redis.io/commands/pexpireat> for more details.
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/protocol/redis/methods/generic.rb', line 151 def pexpireat(key, time) case time when DateTime, Time, Date = (time.to_f * 1000).to_i else = time end call("PEXPIREAT", key, ) end |
#pttl(key) ⇒ Object
Get the time to live for a key in milliseconds. O(1). See <redis.io/commands/pttl> for more details.
165 166 167 |
# File 'lib/protocol/redis/methods/generic.rb', line 165 def pttl(key) call("PTTL", key) end |
#randomkey ⇒ Object
Return a random key from the keyspace. O(1). See <redis.io/commands/randomkey> for more details.
171 172 173 |
# File 'lib/protocol/redis/methods/generic.rb', line 171 def randomkey call("RANDOMKEY") end |
#rename(key, new_key) ⇒ Object
Rename a key. O(1). See <redis.io/commands/rename> for more details.
179 180 181 |
# File 'lib/protocol/redis/methods/generic.rb', line 179 def rename(key, new_key) call("RENAME", key, new_key) end |
#renamenx(key, new_key) ⇒ Object
Rename a key, only if the new key does not exist. O(1). See <redis.io/commands/renamenx> for more details.
187 188 189 |
# File 'lib/protocol/redis/methods/generic.rb', line 187 def renamenx(key, new_key) call("RENAMENX", key, new_key) end |
#restore(key, serialized_value, ttl = 0) ⇒ Object
Create a key using the provided serialized value, previously obtained using DUMP. O(1) to create the new key and additional O(N*M) to reconstruct the serialized value, where N is the number of Redis objects composing the value and M their average size. For small string values the time complexity is thus O(1)+O(1*M) where M is small, so simply O(1). However for sorted set values the complexity is O(N*M*log(N)) because inserting values into sorted sets is O(log(N)). See <redis.io/commands/restore> for more details.
198 199 200 |
# File 'lib/protocol/redis/methods/generic.rb', line 198 def restore(key, serialized_value, ttl=0) call("RESTORE", key, ttl, serialized_value) end |
#scan(cursor, match: nil, count: nil, type: nil) ⇒ Object
Incrementally iterate the keys space. O(1) for every call. O(N) for a complete iteration, including enough command calls for the cursor to return back to 0. N is the number of elements inside the collection. See <redis.io/commands/scan> for more details.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/protocol/redis/methods/generic.rb', line 205 def scan(cursor, match: nil, count: nil, type: nil) arguments = [cursor] if match arguments.append("MATCH", match) end if count arguments.append("COUNT", count) end if type arguments.append("TYPE", type) end call("SCAN", *arguments) end |
#sort(key, by: nil, offset: nil, count: nil, get: nil, order: "ASC", alpha: false, store: nil) ⇒ Object
Sort the elements in a list, set or sorted set. O(N+M*log(M)) where N is the number of elements in the list or set to sort, and M the number of returned elements. When the elements are not sorted, complexity is currently O(N) as there is a copy step that will be avoided in next releases. See <redis.io/commands/sort> for more details.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/protocol/redis/methods/generic.rb', line 228 def sort(key, by: nil, offset: nil, count: nil, get: nil, order: "ASC", alpha: false, store: nil) arguments = [] if by arguments.append("BY", by) end if offset and count arguments.append("LIMIT", offset, count) end get&.each do |pattern| arguments.append("GET", pattern) end if order arguments.append(order) end if alpha arguments.append("ALPHA") end if store arguments.append("STORE", store) end call("SORT", key, *arguments) end |
#touch(key, *keys) ⇒ Object
Alters the last access time of a key(s). Returns the number of existing keys specified. O(N) where N is the number of keys that will be touched. See <redis.io/commands/touch> for more details.
261 262 263 |
# File 'lib/protocol/redis/methods/generic.rb', line 261 def touch(key, *keys) call("TOUCH", key, *keys) end |
#ttl(key) ⇒ Object
Get the time to live for a key. O(1). See <redis.io/commands/ttl> for more details.
268 269 270 |
# File 'lib/protocol/redis/methods/generic.rb', line 268 def ttl(key) call("TTL", key) end |
#type(key) ⇒ Object
Determine the type stored at key. O(1). See <redis.io/commands/type> for more details.
275 276 277 |
# File 'lib/protocol/redis/methods/generic.rb', line 275 def type(key) call("TYPE", key) end |
#unlink(key) ⇒ Object
Delete a key asynchronously in another thread. Otherwise it is just as DEL, but non blocking. O(1) for each key removed regardless of its size. Then the command does O(N) work in a different thread in order to reclaim memory, where N is the number of allocations the deleted objects where composed of. See <redis.io/commands/unlink> for more details.
282 283 284 |
# File 'lib/protocol/redis/methods/generic.rb', line 282 def unlink(key) call("UNLINK", key) end |
#wait(numreplicas, timeout = 0) ⇒ Object
Wait for the synchronous replication of all the write commands sent in the context of the current connection. O(1). See <redis.io/commands/wait> for more details.
290 291 292 |
# File 'lib/protocol/redis/methods/generic.rb', line 290 def wait(numreplicas, timeout = 0) call("WAIT", numreplicas, timeout) end |