当前位置:网站首页>【锁】Redis锁 处理并发 原子性
【锁】Redis锁 处理并发 原子性
2022-07-01 14:53:00 【我是Superman丶】
【锁】Redis锁 处理并发 原子性
如果为空就set值,并返回1
如果存在(不为空)不进行操作,并返回0
很明显,比get和set要好。因为先判断get,再set的用法,有可能会重复set值。
setIfAbsent 和 setnx
setIfAbsent 是java中的方法
setnx 是 redis命令中的方法
说明:当key不存在,将key的值设为value,并设置过期时间,返回true;若给定的key已经存在,则不做任何动作,并返回false。此方法是原子性的
@Autowired
private RedisTemplate redisTemplate;
/**
* 加锁
* @param key key
* @param value value
* @param timeout 过期时间单位秒
* @param false表示有锁 / true表示key不存在 无锁
*/
public boolean getLock(String key, String value, long timeout) {
try {
//原子性操作
boolean flag = redisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS);
//如果存在false表示重复
return flag;
} catch (Exception e) {
logger.error("redis加锁异常", e);
//出现异常删除锁
redisTemplate.delete(key);
return true;
}
}redis命令
redis> SETNX mykey "Hello"
(integer) 1
redis> SETNX mykey "World"
(integer) 0
redis> GET mykey
"Hello"
边栏推荐
- Research Report on development trend and competitive strategy of global consumer glassware industry
- Markdown编辑器使用基本语法
- 购物商城6.27待完成
- Research Report on the development trend and competitive strategy of the global display filter industry
- cmake 基本使用过程
- 基于价值量化的需求优先级排序方法
- Develop small programs and official account from zero [phase III]
- Tensorflow 2. X realizes iris classification
- Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
- 643. Maximum average number of subarrays I
猜你喜欢
![[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion](/img/e1/d35e1d382e0e945849010941b219d3.png)
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
![[zero basic IOT pwn] reproduce Netgear wnap320 rce](/img/f7/d683df1d4b1b032164a529d3d94615.png)
[zero basic IOT pwn] reproduce Netgear wnap320 rce

sqlilabs less10
![[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize](/img/30/aea4ae24f418eb971bca77a1d46bef.png)
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize

cmake 基本使用过程

Problem note - Oracle 11g uninstall
![[leetcode 324] 摆动排序 II 思维+排序](/img/cb/26d89e1a1f548b75a5ef9f29eebeee.png)
[leetcode 324] 摆动排序 II 思维+排序

JVM第二话 -- JVM内存模型以及垃圾回收

Fundamentals of C language

The first technology podcast month will be broadcast soon
随机推荐
Redis安装及Ubuntu 14.04下搭建ssdb主从环境
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码
音乐播放器开发实例(可毕设)
关于软件测试的一些思考
炎炎夏日,这份安全用气指南请街坊们收好!
Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
JVM second conversation -- JVM memory model and garbage collection
Reorganize the trivial knowledge points at the end of the term
[leetcode 324] 摆动排序 II 思维+排序
保证生产安全!广州要求危化品企业“不安全不生产、不变通”
solidty-基础篇-结构体和数组,私有 / 公共函数,函数的返回值和修饰符,事件
JVM performance tuning and practical basic theory part II
对于编程思想和能力有重大提升的书有哪些?
Markdown编辑器使用基本语法
MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
Details of appium key knowledge
The first technology podcast month will be broadcast soon
2022-2-15 learning the imitation Niuke project - Section 3 post details
Today, with the popularity of micro services, how does service mesh exist?
C#学习笔记(5)类和继承