当前位置:网站首页>[lock] redis lock handles concurrency atomicity
[lock] redis lock handles concurrency atomicity
2022-07-01 14:54:00 【I'm Superman】
【 lock 】Redis lock Concurrent processing Atomicity
If it is empty, then set value , And back to 1
If there is ( Not empty ) No operation , And back to 0
Obviously , Than get and set It is better to . Because judge first get, Again set Usage of , It's possible to repeat set value .
setIfAbsent and setnx
setIfAbsent yes java The method in
setnx yes redis Methods in the command
explain : When key non-existent , take key The value of the set value, And set expiration time , return true; If a given key Already exist , Then do nothing , And back to false. This method is atomic
@Autowired
private RedisTemplate redisTemplate;
/**
* Lock
* @param key key
* @param value value
* @param timeout Expiration time in seconds
* @param false Indicates a lock / true Express key non-existent unlocked
*/
public boolean getLock(String key, String value, long timeout) {
try {
// Atomic operation
boolean flag = redisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS);
// If there is false It means repetition
return flag;
} catch (Exception e) {
logger.error("redis Abnormal locking ", e);
// An exception occurred to delete the lock
redisTemplate.delete(key);
return true;
}
}redis command
redis> SETNX mykey "Hello"
(integer) 1
redis> SETNX mykey "World"
(integer) 0
redis> GET mykey
"Hello"
边栏推荐
- In hot summer, please put away this safe gas use guide!
- 数据产品经理需要掌握哪些数据能力?
- [leetcode 324] swing sorting II thinking + sorting
- Research Report on the development trend and competitive strategy of the global display filter industry
- Cannot link redis when redis is enabled
- Blog recommendation | in depth study of message segmentation in pulsar
- [zero basic IOT pwn] reproduce Netgear wnap320 rce
- Research Report on development trend and competitive strategy of global vibration polishing machine industry
- Build your own website (14)
- Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
猜你喜欢

【LeetCode】16、最接近的三数之和

【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较

The State Administration of Chia Tai market supervision, the national development and Reform Commission and the China Securities Regulatory Commission jointly reminded and warned some iron ores

cmake 基本使用过程

Sqlachemy common operations

In hot summer, please put away this safe gas use guide!

Filter &(登录拦截)

【15. 区间合并】
![After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]](/img/ac/ab6053e6ea449beedf434d4cf07dbb.png)
After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]

The markdown editor uses basic syntax
随机推荐
职场太老实,总被欺负怎么办?
tensorflow2-savedmodel convert to pb(frozen_graph)
Opencv mat class
基于价值量化的需求优先级排序方法
数字化转型:数据可视化赋能销售管理
Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
Music player development example (can be set up)
微服务开发步骤(nacos)
NPDP产品经理国际认证报名有什么要求?
[leetcode 324] 摆动排序 II 思维+排序
Details of appium key knowledge
Sqlachemy common operations
Solid basic basic grammar and definition function
About the use of HTTP cache validation last modified and Etag
【LeetCode】16、最接近的三数之和
The first word of JVM -- detailed introduction to JVM and analysis of runtime data area
Vnctf2022 open web gocalc0
关于重载运算符的再整理
Error-tf. function-decorated function tried to create variables on non-first call
En utilisant le paquet npoi de net Core 6 c #, lisez Excel.. Image dans la cellule xlsx et stockée sur le serveur spécifié