Redis SETNX 命令
Created|Updated|Redis教程
|Post Views:
从 Redis 2.6.12 版本开始,此命令被标记为已废弃。
在迁移或编写新的代码时,可以使用带有NX参数的SET命令替代。
语法
1 | SETNX key value |
可用版本
≥ 1.0.0
时间复杂度
$O(1)$
ACL类别
@write, @string, @fast
如果 key 不存在,则将 key 设置为保存字符串值。在这种情况下,它等于 SET。当 key 已经存在时,则不执行任何操作。
SETNX 是 “SET if Not eXists” 的缩写。
返回值
返回一个整数:
- 如果
key被成功设置,则返回 1; - 如果
key没被设置,则返回 0
示例
1 | redis> SETNX john "JOHNSON" |
(END)
Author: Johnson Lin
Copyright Notice: All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
Related Articles
2024-10-01
Redis Set
In Redis, a Set is an unordered collection of unique String elements. This means that duplicate values are not allowed within a Set. The Set’s underlying data structure can either be an intset or a hashtable. Since Redis Sets are implemented using hash tables, the time complexity for adding, removing, or checking for members is O(1). A Set can have a maximum of 2^32 - 1 (4,294,967,295) members, which means each Set can store over 4 billion elements. Example12345678910111213127.0.0.1:6379> ...
2023-06-08
Redis PFCOUNT 命令
语法1PFCOUNT key [key ...] 可用版本 ≥ 2.8.9 时间复杂度 当命令作用于单个 HyperLogLog 时,复杂度为 $O(1)$,并且具有非常低的平均常数时间。当命令作用于 N 个 HyperLogLog 时,复杂度为 $O(N)$ ,常数时间也比处理单个 HyperLogLog 时要大得多。 ACL类别 @read, @hyperloglog, @slow 调用 PFCOUNT 命令时,如果只传入一个 key,则返回存储在指定 key 中的 HyperLogLog 数据结构计算的近似基数,如果 key 不存在则返回为 0。 如果传入多个 key 时,则返回所有给定 key 中的 HyperLogLog 的合集的近似基数,这个近似基数是通过合并所有给定的 HyperLogLog 到一个临时 HyperLogLog 中计算得出的。 HyperLogLog 数据结构用于计算集合中的唯一元素,只需要使用少量固定内存,特别是每个 HyperLogLog 占用 12KB(加上 key 本身的几个字节)。 返回的集合基数不是精确的,而是近似的,标准误差为 0.81...
2024-10-08
Redis Set SINTERCARD Command
The Redis SINTERCARD command returns the cardinality of the set which would result from the intersection of all the given sets. Keys that do not exist are considered to be empty sets. With one of the keys being an empty set, the resulting set is also empty (since set intersection with an empty set always results in an empty set). SyntaxThe basic syntax of the SINTERCARD command is as follows: 1SINTERCARD numkeys key [key ...] [LIMIT limit] This command is similar to SINTER, but instead of ret...
2023-06-03
Redis INCRBY 命令
语法1INCRBY key increment 可用版本 ≥ 1.0.0 时间复杂度 $O(1)$ ACL类别 @write, @string, @fast 将 key 中储存的数字加上指定的增量值。如果键不存在,则在执行操作前将其设置为 0。如果键包含一个错误类型的值或包含一个不能表示为整数的字符串,则返回错误。 该操作仅限于 64 位有符号的整数。 返回值返回一个整数,表示加上指定的增量值之后的 key 值。 示例 1以下例子演示了使用 INCRBY 命令对一个存储整数值的键进行加 2 操作: 12345redis> SET counter "1000"OKredis> INCRBY counter 2(integer) 1002redis> 示例 2对一个不存在的键进行 INCRBY 操作: 1234567redis> EXISTS total(integer) 0redis> INCRBY total 5(integer) 5redis> GET total"5"redis> 示例 3对一...
2024-07-29
A set of best practices and guidelines for using Redis effectively
In this article, I introduce you to 12 specifications in the areas of key-value pair usage, command usage, and data preservation, centered around the goals of high-performance access and saving memory space in Redis applications. Key-Value Pair Usage Specifications [Recommended] Use the business name as the KEY prefix, preferably using its abbreviation. This is done by prefixing the business name, then separating it with a colon(:), followed by the specific business data name. For example, if...
2023-06-02
查看 Redis 版本号
查看服务端版本1redis-server -v 或 1redis-server --version 例如,使用 redis-server -v 查看 Redis 的版本信息: 12$ redis-server -vRedis server v=7.0.4 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=59659b131c47ab91 输出信息如下: redis-server - 表示这是 Redis 服务器进程 v=7.0.4 - Redis 的版本号,这里是7.0.4版本 sha=00000000:0 - Git SHA 标识,用来标识 Git 中的某个提交 malloc=jemalloc-5.2.1 - Redis 使用的内存分配器,这里是 jemalloc 5.2.1 版本 bits=64 - Redis 编译时使用的位数,这里是 64 位 build=59659b131c47ab91 - Redis 的构建 ID 查看客户端版本1redis-cli -v 或 1redis-cli --version 例如,以下命...