1. Generic
1.1 copy
1.1.1 基本信息
COPY source destination [DB destination-db] [REPLACE]
summary: Copy a key
since: 6.2.0
This command copies the value stored at the source key to the destination key. 此命令将存储在源键上的值复制到目标键。
By default, the destination key is created in the logical database used by the connection. The DB option allows specifying an alternative logical database index for the destination key. 默认情况下,目标键是在连接使用的逻辑数据库中创建的。DB 选项允许为目标键指定可选的逻辑数据库索引。
The command returns an error when the destination key already exists. The REPLACE option removes the destination key before copying the value to it. 当目标键已经存在时,该命令返回一个错误。REPLACE 选项在将值复制到目标键之前删除目标键。
1.1.2 返回
- 如果复制成功,则为1,否则返回0
1.1.3 练习
127.0.0.1:6379> set key1 "test1004"
OK
127.0.0.1:6379> copy key1 key2
(integer) 1
127.0.0.1:6379> get key2
"test1004"
1.2 del
1.2.1 基本信息
DEL key [key ...]
summary: Delete a key
since: 1.0.0
Removes the specified keys. A key is ignored if it does not exist.
移除指定的键。如果某个键不存在,则忽略该键。
1.2.2 返回
- 删除的键的个数
1.2.3 练习
127.0.0.1:6379> del key1 key2
(integer) 2
1.3 dump
1.3.1 基本信息
DUMP key
summary: Return a serialized version of the value stored at the specified key.
since: 2.6.0
Serialize the value stored at key in a Redis-specific format and return it to the user. The returned value can be synthesized back into a Redis key using the RESTORE command.
以特定于 Redis 的格式序列化键上存储的值,并将其返回给用户。可以使用 RESTORE 命令将返回的值合成回 Redis 键。
The serialization format is opaque and non-standard, however it has a few semantic characteristics:
序列化格式是不透明和非标准的,但是它有一些语义特征:
-
It contains a 64-bit checksum that is used to make sure errors will be detected. The RESTORE command makes sure to check the checksum before synthesizing a key using the serialized value.
-
- 它包含一个64位校验和,用于确保检测到错误。RESTORE 命令确保在使用序列化值合成键之前检查校验和。
-
Values are encoded in the same format used by RDB.
-
- 值的编码格式与 RDB 使用的格式相同。
-
An RDB version is encoded inside the serialized value, so that different Redis versions with incompatible RDB formats will refuse to process the serialized value.
-
- RDB 版本被编码在序列化值中,因此不同的 Redis 版本和不兼容的 RDB 格式将拒绝处理序列化值。
The serialized value does NOT contain expire information. In order to capture the time to live of the current value the PTTL command should be used.
序列化值不包含过期信息。为了捕获当前值的生存时间,应该使用 PTTL 命令。
If key does not exist a nil bulk reply is returned.
如果键不存在,则返回空。
1.3.2 返回
- 序列化的值
1.3.3 练习
127.0.0.1:6379> set key1 "test1004"
OK
127.0.0.1:6379> dump key1
"\x00\btest1004\t\x00*\x8f\xd9]\x94\xecb\x1e"
1.4 exists
1.4.1 基本信息
EXISTS key [key ...]
summary: Determine if a key exists
since: 1.0.0
Returns if key exists.
返回键是否存在。
The user should be aware that if the same existing key is mentioned in the arguments multiple times, it will be counted multiple times. So if somekey exists, EXISTS somekey somekey will return 2.
用户应该意识到,如果相同的现有键在参数中被多次提及,它将被多次计数。
1.4.2 返回
- 键存在的个数
1.4.3 练习
127.0.0.1:6379> exists key1
(integer) 1
127.0.0.1:6379> exists key1 key1
(integer) 2
127.0.0.1:6379> exists key1 key1 key1
(integer) 3
1.5 expire
1.5.1 基本信息
EXPIRE key seconds
summary: Set a key's time to live in seconds
since: 1.0.0
Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology. 在键上设置超时。超时过期后,key 将自动删除,带有超时的键通常被认为是不稳定的。
The timeout will only be cleared by commands that delete or overwrite the contents of the key, including DEL, SET, GETSET and all the *STORE commands. This means that all the operations that conceptually alter the value stored at the key without replacing it with a new one will leave the timeout untouched. For instance, incrementing the value of a key with INCR, pushing a new value into a list with LPUSH, or altering the field value of a hash with HSET are all operations that will leave the timeout untouched.
只有删除或覆盖密钥内容(包括 DEL、 SET、 GETSET 和所有 * STORE 命令)的命令才能清除超时。这意味着所有在概念上改变存储在键上的值而不用新键替换它的操作都将保持超时不变。例如,使用 INCR 增加一个键的值,使用 LPUSH 将一个新值推入一个列表,或者使用 HSET 改变一个 hash 的字段值,这些操作都不会触发超时。
The timeout can also be cleared, turning the key back into a persistent key, using the PERSIST command.
也可以清除超时,使用 PERSIST 命令将 key 设置为持久 key。
If a key is renamed with RENAME, the associated time to live is transferred to the new key name.
如果使用 RENAME 重命名 key ,则相关的生存时间将转移到新的 key。
If a key is overwritten by RENAME, like in the case of an existing key Key_A that is overwritten by a call like RENAME Key_B Key_A, it does not matter if the original Key_A had a timeout associated or not, the new key Key_A will inherit all the characteristics of Key_B.
如果一个键被 RENAME 覆盖,就像现有的键 Key _ A 被 RENAME Key _ B Key _ A 这样的调用覆盖一样,那么无论原始的 Key _ A 是否有超时关联,新的 Key _ A 将继承 Key _ B 的所有特征。
Note
calling EXPIRE/PEXPIRE with a non-positive timeout or EXPIREAT/PEXPIREAT with a time in the past will result in the key being deleted rather than expired (accordingly, the emitted key event will be del, not expired).
非正常超时调用 EXPIRE/PEXPIRE 或使用过去的时间调用 EXPIREAT/PEXPIREAT 将导致 key 被删除而不是过期(发出的事件将为 del,而不是过期)。
1.5.2 选项
-
NX -- Set expiry only when the key has no expiry
-
- NX ——仅当 key 没有有效期时才设置
-
XX -- Set expiry only when the key has an existing expiry
-
- XX ——仅当 key 有一个现有的有效期限时才设置有效期限
-
GT -- Set expiry only when the new expiry is greater than current one
-
- GT ——仅当新到期时间大于当前到期时间时才设置
-
LT -- Set expiry only when the new expiry is less than current one
-
- LT ——仅当新的到期时间小于当前到期时间时才设置
-
GT、 LT 和 NX 是相互排斥的
1.5.3 返回
- 如果设置了超时,则返回1
- 如果没有设置超时,则返回0
1.5.4 练习
127.0.0.1:6379> set key2 "test"
OK
127.0.0.1:6379> TTL key2
(integer) -1
127.0.0.1:6379> expire key2 10
(integer) 1
127.0.0.1:6379> TTL key2
(integer) 8
1.5.5 经典使用-Navigation session
Imagine you have a web service and you are interested in the latest N pages recently visited by your users, such that each adjacent page view was not performed more than 60 seconds after the previous. Conceptually you may consider this set of page views as a Navigation session of your user, that may contain interesting information about what kind of products he or she is looking for currently, so that you can recommend related products.
假设有一个 Web 服务,需要了解用户最近访问的最新 N 个页面,因此每个相邻的页面查看不会在前一个页面之后超过60秒执行。从概念上讲,可以将这组页面视图视为用户的导航会话,其中可能包含有关用户当前正在寻找的产品类型的有趣信息,以便推荐相关产品。
If the user will be idle more than 60 seconds, the key will be deleted and only subsequent page views that have less than 60 seconds of difference will be recorded.
如果用户空闲时间超过60秒,则将删除该键,并且只记录差异小于60秒的后续页面视图。
This pattern is easily modified to use counters using INCR instead of lists using RPUSH.
这种模式很容易修改为使用 INCR 计数器而不是使用 RPUSH 列表。
MULTI
RPUSH pagewviews.user:<userid> http://.....
EXPIRE pagewviews.user:<userid> 60
EXEC
1.5.6 Key 是如何过期的
Redis keys are expired in two ways: a passive way, and an active way.
Redis key 有两种过期方式: 被动方式和主动方式。
A key is passively expired simply when some client tries to access it, and the key is found to be timed out.
当一些客户端试图访问一个 key 时,该 key 就会被动地过期,并且发现该 key 已超时。
Of course this is not enough as there are expired keys that will never be accessed again. These keys should be expired anyway, so periodically Redis tests a few keys at random among keys with an expire set. All the keys that are already expired are deleted from the keyspace.
当然,这是不够的,因为有过期的 key,将永远不会再次访问。这些 key 无论如何都应该过期,因此 Redis 定期在设置了过期集的 key 中随机测试几个。所有已过期的 key 都将从 key 空间中删除。
Specifically this is what Redis does 10 times per second:
具体来说,Redis 每秒做10次的事情:
- Test 20 random keys from the set of keys with an associated expire. 从一组 key 中测试20个随机 key,并使用相关的过期时间。
- Delete all the keys found expired. 删除所有过期的 key。
- If more than 25% of keys were expired, start again from step 1. 如果超过25% 的 key 过期,则从步骤1重新开始。
This is a trivial probabilistic algorithm, basically the assumption is that our sample is representative of the whole key space, and we continue to expire until the percentage of keys that are likely to be expired is under 25%
这是一个简单的概率算法,基本上假设我们的样本代表整个空间,并且我们继续,直到可能过期的密钥的百分比低于25%
This means that at any given moment the maximum amount of keys already expired that are using memory is at max equal to max amount of write operations per second divided by 4.
这意味着在任何给定的时刻,使用内存的已过期 key 的最大数量最大等于每秒写操作的最大数量除以4。
1.5.7 AOF 文件对过期 Key 的处理
In order to obtain a correct behavior without sacrificing consistency, when a key expires, a DEL operation is synthesized in both the AOF file and gains all the attached replicas nodes. This way the expiration process is centralized in the master instance, and there is no chance of consistency errors.
为了在不牺牲一致性的情况下获得正确的行为,当 key 过期时,会在两个 AOF 文件中合成一个 DEL 操作,并获得所有附加的副本节点。这样,过期过程集中在主实例中,不会出现一致性错误。
However while the replicas connected to a master will not expire keys independently (but will wait for the DEL coming from the master), they'll still take the full state of the expires existing in the dataset, so when a replica is elected to master it will be able to expire the keys independently, fully acting as a master.
虽然连接到主实例的副本不会独立地使 key 过期(但会等待来自主的 DEL) ,但它们仍然会采用数据集中现有过期的完整状态,所以当选择一个副本切换为主时,它将能够独立地使 key 过期,充当主实例的角色。
1.6 expireat
1.6.1 基本信息
EXPIREAT key timestamp
summary: Set the expiration for a key as a UNIX timestamp
since: 1.2.0
EXPIREAT has the same effect and semantic as EXPIRE, but instead of specifying the number of seconds representing the TTL (time to live), it takes an absolute Unix timestamp (seconds since January 1, 1970). A timestamp in the past will delete the key immediately.
EXPIREAT 与 EXPIRE 具有相同的效果和语义,但是它没有指定表示 TTL (生存时间)的秒数,而是采用了一个绝对的 Unix 时间戳(自1970年1月1日以来的秒数)。时间戳结束后立马删除 key
1.6.2 选项
-
NX -- Set expiry only when the key has no expiry
-
- NX ——仅当 key 没有有效期时才设置
-
XX -- Set expiry only when the key has an existing expiry
-
- XX ——仅当 key 有一个现有的有效期限时才设置有效期限
-
GT -- Set expiry only when the new expiry is greater than current one
-
- GT ——仅当新到期时间大于当前到期时间时才设置
-
LT -- Set expiry only when the new expiry is less than current one
-
- LT ——仅当新的到期时间小于当前到期时间时才设置
-
GT、 LT 和 NX 是相互排斥的
1.6.3 返回
- 如果设置了超时,则返回1
- 如果没有设置超时,则返回0
1.6.4 练习
127.0.0.1:6379> set key2 "test"
OK
127.0.0.1:6379> exists key2
(integer) 1
127.0.0.1:6379> expireat key2 1293840000
(integer) 1
127.0.0.1:6379> exists key2
(integer) 0
1.7 keys
1.7.1 基本信息
KEYS pattern
summary: Find all keys matching the given pattern
since: 1.0.0
Returns all keys matching pattern. 返回所有符合正则匹配的键。
While the time complexity for this operation is O(N), the constant times are fairly low. For example, Redis running on an entry level laptop can scan a 1 million key database in 40 milliseconds.
虽然该操作的时间复杂度为 O (N) ,但是常数次相当低。
Supported glob-style patterns: 支持的模式:
- h?llo matches hello, hallo and hxllo
- h*llo matches hllo and heeeello
- h[ae]llo matches hello and hallo, but not hillo
- h[^e]llo matches hallo, hbllo, ... but not hello
- h[a-b]llo matches hallo and hbllo
Use ** to escape special characters if you want to match them verbatim.
如果希望逐字匹配特殊字符,则需要转义。
Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.
警告: 将 KEYS 作为一个命令在生产环境中使用时,要特别小心。在对大型数据库执行时,它可能会破坏性能。此命令用于调试和特殊操作。不要在常规应用程序代码中使用 KEYS。如果想在 key 空间中找到 key,可以考虑使用 SCAN 或 set。
1.7.2 返回
- 符合模式匹配的键
1.7.3 练习
127.0.0.1:6379> mset firstname "test" lastname "lastname"
OK
127.0.0.1:6379> keys *name*
1) "lastname"
2) "firstname"
1.8 move
1.8.1 基本信息
MOVE key db
summary: Move a key to another database
since: 1.0.0
Move key from the currently selected database (see SELECT) to the specified destination database. When key already exists in the destination database, or it does not exist in the source database, it does nothing. It is possible to use MOVE as a locking primitive because of this.
将键从当前选定的数据库(请参见 SELECT)移动到指定的目标数据库。如果目标数据库中已经存在 key,或者源数据库中不存在 key,则它不执行任何操作。由于这个原因,可以使用 MOVE 作为锁定原语。
1.8.2 返回
- key 移动成功返回1,否则返回0
1.8.3 练习
127.0.0.1:6379> move firstname 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get firstname
"test"
1.9 persist
1.9.1 基本信息
PERSIST key
summary: Remove the expiration from a key
since: 2.2.0
Remove the existing timeout on key, turning the key from volatile (a key with an expire set) to persistent (a key that will never expire as no timeout is associated).
删除键上现有的超时,将键从可变(设置过期的键)转换为持久(由于没有超时关联而永远不会过期的键)。
1.9.2 返回
- 如果删除超时,则为1
- 如果键不存在或没有关联的超时,则返回0
1.9.3 练习
127.0.0.1:6379[1]> set key1 "testkey1" ex 10
OK
127.0.0.1:6379[1]> persist key1
(integer) 1
127.0.0.1:6379[1]> TTL key1
(integer) -1
1.10 pexpire
1.10.1 基本信息
PEXPIRE key milliseconds
summary: Set a key's time to live in milliseconds
since: 2.6.0
This command works exactly like EXPIRE but the time to live of the key is specified in milliseconds instead of seconds.
此命令的工作方式与 EXPIRE 完全相同,但是生存时间是以毫秒而不是以秒为单位指定的。
1.10.2 返回
- 设置成功返回1,否则返回0
1.10.3 练习
127.0.0.1:6379[1]> get key1
"testkey1"
127.0.0.1:6379[1]> TTL key1
(integer) -1
127.0.0.1:6379[1]> pexpire key1 100000
(integer) 1
127.0.0.1:6379[1]> TTL key1
(integer) 97
1.11 pexpireat
1.11.1 基本信息
PEXPIREAT key milliseconds-timestamp
summary: Set the expiration for a key as a UNIX timestamp specified in milliseconds
since: 2.6.0
PEXPIREAT has the same effect and semantic as EXPIREAT, but the Unix time at which the key will expire is specified in milliseconds instead of seconds.
PEXPIREAT 与 EXPIREAT 具有相同的效果和语义,但是指定键过期的 Unix 时间是以毫秒而不是以秒为单位。
1.11.2 返回
- 设置成功返回1,否则返回0
1.11.3 练习
127.0.0.1:6379[1]> pexpireat key2 1000
(integer) 0
1.12 pttl
1.12.1 基本信息
PTTL key
summary: Get the time to live for a key in milliseconds
since: 2.6.0
Like TTL this command returns the remaining time to live of a key that has an expire set, with the sole difference that TTL returns the amount of remaining time in seconds while PTTL returns it in milliseconds.
与 TTL 类似,这个命令返回设置了 key 的过期时间,唯一的区别是 TTL 返回的剩余时间量以秒为单位,而 PTTL 返回的剩余时间量以毫秒为单位。
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.
在 Redis 2.6或更高版本中,如果键不存在,或者键存在但没有相关的过期,则命令返回 -1。
Starting with Redis 2.8 the return value in case of error changed:
从 Redis 2.8开始,如果发生错误,返回值会发生变化:
- The command returns -2 if the key does not exist.
- 如果键不存在,则命令返回 -2。
- The command returns -1 if the key exists but has no associated expire.
- 如果键存在但没有关联过期,则命令返回 -1。
1.12.2 返回
- 以毫秒为单位的 TTL,或者为了发出错误信号的负值
1.12.3 练习
127.0.0.1:6379[1]> pttl key1
(integer) -2
1.13 randomkey
1.13.1 基本信息
RANDOMKEY -
summary: Return a random key from the keyspace
since: 1.0.0
Return a random key from the currently selected database.
从当前选定的数据库返回一个随机 key。
1.13.2 返回
- 随机键,或数据库为空时为 nil
1.13.3 练习
127.0.0.1:6379[1]> randomkey
"firstname"
1.14 rename
1.14.1 基本信息
RENAME key newkey
summary: Rename a key
since: 1.0.0
Renames key to newkey. It returns an error when key does not exist. If newkey already exists it is overwritten, when this happens RENAME executes an implicit DEL operation, so if the deleted key contains a very big value it may cause high latency even if RENAME itself is usually a constant-time operation.
将 key 重命名为 newkey。当键不存在时,它返回一个错误。如果 newkey 已经存在,它将被覆盖,当这种情况发生时 RENAME 执行一个隐式的 DEL 操作,所以如果被删除的键包含一个非常大的值,它可能会导致高延迟,即使 RENAME 本身通常是一个常量时间操作。
In Cluster mode, both key and newkey must be in the same hash slot, meaning that in practice only keys that have the same hash tag can be reliably renamed in cluster.
在集群模式下,key 和 newkey 必须位于相同的散列槽中,这意味着实际上只有具有相同散列标记的键才能在集群中可靠地重命名。
1.14.2 返回
- OK
1.14.3 练习
127.0.0.1:6379[1]> rename firstname lastname
OK
127.0.0.1:6379[1]> get lastname
"test"
1.15 renamenx
1.15.1 基本信息
RENAMENX key newkey
summary: Rename a key, only if the new key does not exist
since: 1.0.0
Renames key to newkey if newkey does not yet exist. It returns an error when key does not exist.
如果 newkey 不存在,则将 key 重命名为 newkey。当 key 不存在时返回一个错误。
In Cluster mode, both key and newkey must be in the same hash slot, meaning that in practice only keys that have the same hash tag can be reliably renamed in cluster.
在集群模式下,key 和 newkey 必须位于相同的散列槽中,这意味着实际上只有具有相同散列标记的键才能在集群中可靠地重命名。
1.15.2 返回
- 如果键被重命名为 newkey,则为1
- 如果 newkey 已经存在,则返回0
1.15.3 练习
127.0.0.1:6379[1]> renamenx lastname firstname
(integer) 1
127.0.0.1:6379[1]> get firstname
"test"
1.16 restore
1.16.1 基本信息
RESTORE key ttl serialized-value [REPLACE] [ABSTTL] [IDLETIME seconds] [FREQ frequency]
summary: Create a key using the provided serialized value, previously obtained using DUMP.
since: 2.6.0
Create a key associated with a value that is obtained by deserializing the provided serialized value (obtained via DUMP).
创建一个通过反序列化提供的序列化值(通过 DUMP 获得)的键。
If ttl is 0 the key is created without any expire, otherwise the specified expire time (in milliseconds) is set.
如果 ttl 为0,则创建 key 时没有任何过期时间,否则设置指定的过期时间(以毫秒为单位)。
If the ABSTTL modifier was used, ttl should represent an absolute Unix timestamp (in milliseconds) in which the key will expire.
如果使用了 ABSTTL 修饰符,ttl 应该表示 key 过期的绝对 Unix 时间戳(毫秒)。
RESTORE will return a "Target key name is busy" error when key already exists unless you use the REPLACE modifier.
如果键已经存在,除非使用 REPLACE 修饰符,否则 RESTORE 将返回“ Target key name is busy”错误。
RESTORE checks the RDB version and data checksum. If they don't match an error is returned.
RESTORE 检查 RDB 版本和数据校验和。如果它们不匹配,则返回错误。
1.16.2 返回
- 命令执行成功后返回 OK
1.17 scan
1.17.1 基本信息
SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
summary: Incrementally iterate the keys space
since: 2.8.0
The SCAN command and the closely related commands SSCAN, HSCAN and ZSCAN are used in order to incrementally iterate over a collection of elements.
SCAN 命令以及与之密切相关的命令 SSCAN、 HSCAN 和 ZSCAN 用于对元素集合进行增量遍历。
-
SCAN iterates the set of keys in the currently selected Redis database.
-
- SCAN 遍历当前选定的 Redis 数据库中的键集。
-
SSCAN iterates elements of Sets types.
-
- SSCAN 遍历 Set 类型的元素。
-
HSCAN iterates fields of Hash types and their associated values.
-
- HSCAN 遍历 Hash 类型的字段及其相关值。
-
ZSCAN iterates elements of Sorted Set types and their associated scores.
-
- ZSCAN 遍历排序集类型的元素及其相关分数。
Since these commands allow for incremental iteration, returning only a small number of elements per call, they can be used in production without the downside of commands like KEYS or SMEMBERS that may block the server for a long time (even several seconds) when called against big collections of keys or elements.
由于这些命令允许增量遍历,每次调用只返回少量的元素,因此它们可以在生产环境中使用,而不会出现 KEYS 或 SMMBERS 等命令的缺点,这些命令在对大量键或元素进行调用时可能会长时间(甚至几秒钟)阻塞服务器。
However while blocking commands like SMEMBERS are able to provide all the elements that are part of a Set in a given moment, The SCAN family of commands only offer limited guarantees about the returned elements since the collection that we incrementally iterate can change during the iteration process.
然而,虽然像 SMMBERS 这样的阻塞命令能够在给定的时刻提供 Set 的所有元素,但是 SCAN 命令系列只能提供有限的返回元素的保证,因为我们增量遍历的集合在遍历过程中可能会发生变化。
Note that SCAN, SSCAN, HSCAN and ZSCAN all work very similarly, so this documentation covers all the four commands. However an obvious difference is that in the case of SSCAN, HSCAN and ZSCAN the first argument is the name of the key holding the Set, Hash or Sorted Set value. The SCAN command does not need any key name argument as it iterates keys in the current database, so the iterated object is the database itself.
请注意,在 SSCAN、 HSCAN 和 ZSCAN 的情况下,第一个参数是保存 Set、 Hash 或 Sorted Set 值的键的名称。SCAN 命令不需要任何键名参数,因为它在当前数据库中迭代键,所以迭代的对象就是数据库本身。
1.17.2 基础使用
SCAN is a cursor based iterator. This means that at every call of the command, the server returns an updated cursor that the user needs to use as the cursor argument in the next call.
SCAN 是一个基于游标的迭代器。这意味着在每次调用该命令时,服务器都会返回一个更新后的游标,用户需要在下次调用中将其用作游标参数。
127.0.0.1:6379[1]> scan 0
1) "7"
2) 1) "key9"
2) "key2"
3) "key5"
4) "key6"
5) "key1"
6) "key4"
7) "key3"
8) "key7"
9) "key8"
10) "firstname"
127.0.0.1:6379[1]> scan 7
1) "0"
2) (empty array)
1.18 sort
1.18.1 基本信息
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
summary: Sort the elements in a list, set or sorted set
since: 1.0.0
Returns or stores the elements contained in the list, set or sorted set at key.
返回或存储列表中包含的元素,按键设置或排序设置。
There is also the SORT_RO read-only variant of this command.
此命令还有 SORT _ RO 只读变体。
By default, sorting is numeric and elements are compared by their value interpreted as double precision floating point number. This is SORT in its simplest form:
默认情况下,排序为数值排序,元素通过其被解释为双精度浮点数的值进行比较。
When mylist contains string values and you want to sort them lexicographically, use the ALPHA modifier:
当 mylist 包含字符串值并希望按照字典顺序对它们进行排序时,请使用 ALPHA 修饰符
The number of returned elements can be limited using the LIMIT modifier. This modifier takes the offset argument, specifying the number of elements to skip and the count argument, specifying the number of elements to return from starting at offset. The following example will return 10 elements of the sorted version of mylist, starting at element 0 (offset is zero-based):
可以使用 LIMIT 修饰符限制返回元素的数量。此修饰符接受偏移量参数,指定要跳过的元素数和 count 参数,指定从偏移量开始返回的元素数。
1.18.2 练习
SORT mylist LIMIT 0 5 ALPHA DESC
SORT mylist BY weight_*
SORT mylist BY nosort
SORT mylist BY weight_* GET object_*
1.19 touch
1.19.1 基本信息
TOUCH key [key ...]
summary: Alters the last access time of a key(s). Returns the number of existing keys specified.
since: 3.2.1
Alters the last access time of a key(s). A key is ignored if it does not exist.
更改键的最后访问时间。如果键不存在,则忽略。
1.19.2 返回
- 被更改的键数
1.19.3 练习
127.0.0.1:6379[1]> touch key1 key2
(integer) 2
1.20 ttl
1.20.1 基本信息
TTL key
summary: Get the time to live for a key
since: 1.0.0
Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.
返回设定了超时的键的剩余生存时间。这种内省功能允许 Redis 客户端检查给定的 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.
在 Redis 2.6或更高版本中,如果键不存在,或者键存在但没有相关的过期,则命令返回 -1。
Starting with Redis 2.8 the return value in case of error changed:
从 Redis 2.8开始,如果发生错误,返回值会发生变化:
- The command returns -2 if the key does not exist.
- 如果键不存在,则命令返回 -2。
- The command returns -1 if the key exists but has no associated expire.
- 如果键存在但没有关联过期,则命令返回 -1。
1.20.2 返回
- 以秒为单位的 TTL,或者为了发出错误信号的负值
1.20.3 练习
127.0.0.1:6379[1]> ttl key1
(integer) -1
1.21 type
1.21.1 基本信息
TYPE key
summary: Determine the type stored at key
since: 1.0.0
Returns the string representation of the type of the value stored at key. The different types that can be returned are: string, list, set, zset, hash and stream.
返回键上存储的值的类型。可以返回的不同类型有: string、 list、 set、 zset、 hash 和 stream。
1.21.2 返回
- 键的值的类型
1.21.3 练习
127.0.0.1:6379[1]> type key1
string
1.22 unlink
1.22.1 基本信息
UNLINK key [key ...]
summary: Delete a key asynchronously in another thread. Otherwise it is just as DEL, but non blocking.
since: 4.0.0
This command is very similar to DEL: it removes the specified keys. Just like DEL a key is ignored if it does not exist. However the command performs the actual memory reclaiming in a different thread, so it is not blocking, while DEL is. This is where the command name comes from: the command just unlinks the keys from the keyspace. The actual removal will happen later asynchronously.
这个命令与 DEL 非常相似: 它删除指定的键。就像 DEL 一样,如果键不存在,则忽略它。但是,该命令在不同的线程中执行实际的内存回收,因此它不会阻塞,而 DEL 会阻塞。这就是命令名的来源: 命令只是从密钥空间断开密钥的链接。实际的删除将在稍后异步地进行。
1.22.2 返回
- 未链接的键数
1.22.3 练习
127.0.0.1:6379[1]> unlink key1 key2 key3
(integer) 3
127.0.0.1:6379[1]> get key1
(nil)
1.23 wait
1.23.1 基本信息
WAIT numreplicas timeout
summary: Wait for the synchronous replication of all the write commands sent in the context of the current connection
since: 3.0.0
This command blocks the current client until all the previous write commands are successfully transferred and acknowledged by at least the specified number of replicas. If the timeout, specified in milliseconds, is reached, the command returns even if the specified number of replicas were not yet reached.
此命令将阻塞当前客户端,直到所有以前的写命令都成功传输,并且至少得到指定数量的副本的确认。如果达到以毫秒为单位指定的超时,即使尚未达到指定数量的副本,该命令也将返回。
The command will always return the number of replicas that acknowledged the write commands sent before the WAIT command, both in the case where the specified number of replicas are reached, or when the timeout is reached.
无论是在达到指定数量的副本时,还是在达到超时时,命令总是返回确认在 WAIT 命令之前发送的写命令的副本数。
- When WAIT returns, all the previous write commands sent in the context of the current connection are guaranteed to be received by the number of replicas returned by WAIT. 当 WAIT 返回时,通过 WAIT 返回的副本数量可以保证接收到在当前连接上下文中发送的所有以前的写命令。
- If the command is sent as part of a MULTI transaction, the command does not block but instead just return ASAP the number of replicas that acknowledged the previous write commands. 如果命令是作为 MULTI 事务的一部分发送的,则该命令不会阻塞,而只是尽快返回确认以前写入命令的副本数。
- A timeout of 0 means to block forever. 0的超时意味着永远阻塞。
- Since WAIT returns the number of replicas reached both in case of failure and success, the client should check that the returned value is equal or greater to the replication level it demanded. 因为 WAIT 在失败和成功的情况下都返回到达的副本数,所以客户端应该检查返回的值是否等于或大于它所需要的复制级别。
1.23.2 一致性问题
Note that WAIT does not make Redis a strongly consistent store: while synchronous replication is part of a replicated state machine, it is not the only thing needed. However in the context of Sentinel or Redis Cluster failover, WAIT improves the real world data safety.
请注意,WAIT 并没有使 Redis 成为一个强一致性存储: 虽然同步复制是复制状态机的一部分,但它并不是唯一需要的东西。然而,在哨兵或 Redis 集群故障转移的上下文中,WAIT 提高了真实世界的数据安全性。
Specifically if a given write is transferred to one or more replicas, it is more likely (but not guaranteed) that if the master fails, we'll be able to promote, during a failover, a replica that received the write: both Sentinel and Redis Cluster will do a best-effort attempt to promote the best replica among the set of available replicas.
具体来说,如果一个给定的写操作被转移到一个或多个副本,那么更有可能(但不能保证)的是,如果主实例失败,我们将能够在故障转移期间提升一个接收到写操作的副本: Sentinel 和 Redis 集群将尽最大努力在一组可用副本中提升最好的副本。
However this is just a best-effort attempt so it is possible to still lose a write synchronously replicated to multiple replicas.
然而,这只是一种尽力而为的尝试,因此仍有可能丢失同步复制到多个副本的写操作。
1.23.3 实现细节
Since the introduction of partial resynchronization with replicas (PSYNC feature) Redis replicas asynchronously ping their master with the offset they already processed in the replication stream. This is used in multiple ways:
由于引入了与副本的部分重同步(PSYNC 特性) ,Redis 副本异步 ping 它们的主副本和它们已经在复制流中处理的偏移量。这种方法有多种用途:
- Detect timed out replicas. 检测超时副本
- Perform a partial resynchronization after a disconnection. 断开连接后执行部分重新同步。
- Implement WAIT. 实施等待。
In the specific case of the implementation of WAIT, Redis remembers, for each client, the replication offset of the produced replication stream when a given write command was executed in the context of a given client. When WAIT is called Redis checks if the specified number of replicas already acknowledged this offset or a greater one.
在实现 WAIT 的特定情况下,Redis 会为每个客户端记住在给定客户端的上下文中执行给定写命令时生成的复制流的复制偏移量。当 WAIT 被称为 Redis 时,检查指定数量的副本是否已经确认该偏移量或更大的副本。
标签:返回,0.1,127.0,Generic,Redis,6379,key,004 From: https://www.cnblogs.com/autumncat/p/16757600.html