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
2019-09-03
Redis根据通配符批量删除Key
Redis 目前还不支持根据通配符批量删除 Key 的命令,但我们可以借助 Redis 的 del 命令、Linux 管道和 xargs 指令来完成。 Redis的 del 命令支持删除给定的一个或多个 key,并返回被删除 key 的数量。如命令 del key1 key2 key3。 比如要删除所有以 WxMpSessionKey 开头的 Key,可以使用如下命令: 12[redis@iztz9ploifbvce ~]# redis-cli -h 192.168.182.227 -p 1379 keys "WxMpSessionKey*" |xargs redis-cli -h 192.168.182.227 -p 1379 del(integer) 2674 如果连接 Redis 的 HOST 为 127.0.0.1,端口号也为默认的 6379,则以上的命令可以简写为: 1redis-cli keys "WxMpSessionKey*" |xargs redis-cli del (END)
2019-12-19
Spring Boot 2.x 集成Redis示例
一、如何集成首先,在 pom 文件新增 redis 依赖: 1234<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency> 接着修改项目配置文件 application.properties,增加 redis 配置 1234# redis hostspring.redis.host=172.24.58.226# redis portspring.redis.port=6379 经过上面简单的两步,即可在项目中使用 StringRedisTemplate 和 RedisTemplate<Object,Object>,因为从 Spring Boot 2.0 开始,Spring...
2024-06-02
Using the PFADD Command in Redis
Redis, an open-source, in-memory data structure store, offers a wide range of commands to manipulate data structures like strings, lists, sets, hashes, bitmaps, hyperloglogs, and more. Among these, the HyperLogLog data type is particularly useful for approximating the number of unique elements in a set when the set is too large to fit into memory or when you don’t need absolute accuracy. The PFADD command is one of the key operations for working with HyperLogLog in Redis. This guide will...
2024-07-25
How to learn Redis effectively?
Redis, an open-source, high-performance in-memory data structure store, is renowned for its speed, versatility, and simplicity. It has gained immense popularity and is widely utilized for caching, distributed locks, session management, real-time analytics, and more. Whether you’re a developer looking to expand your skills or an IT professional aiming to optimize data handling, mastering Redis can significantly enhance your proficiency. Here’s a comprehensive guide on how to learn Redis...
2024-07-27
Flushing Cache Data in Redis: A Step-by-Step Guide
Caching mechanisms are vital for optimizing application performance by storing frequently accessed data in-memory. Redis, as a robust key-value store, offers powerful caching capabilities. Occasionally, you might need to clear or flush all cached data from Redis to ensure consistency or during maintenance tasks. Here’s how you can do it effectively: Understanding Redis FlushingFlushing in Redis refers to the action of removing all data stored in the current database. This operation is...
2024-07-30
Effective Ways to Delete Fuzzy Matching Keys in Redis
In Redis, managing keys efficiently is crucial for optimal performance. Sometimes, you might need to delete keys that match a pattern or have a fuzzy match. Here’s how you can achieve this: Using Redis CLI CommandsRedis CLI provides several commands to interact with keys. To delete keys that match a pattern, you can use the KEYS command in combination with the DEL command: 1redis-cli KEYS "pattern*" | xargs redis-cli DEL Replace "pattern*" with your specific pattern. This...