当前位置:网站首页>Redis distributed lock, lock code logic
Redis distributed lock, lock code logic
2022-07-05 02:11:00 【I use mosquito repellent incense】
public boolean lock(String key, String value) {
if(redisTemplate.opsForValue().setIfAbsent(key, value)) {
return true;
}
String currentValue = redisTemplate.opsForValue().get(key);
if (!StringUtils.isEmpty(currentValue)
&& Long.parseLong(currentValue) < System.currentTimeMillis()) {
String oldValue = redisTemplate.opsForValue().getAndSet(key, value);
if (!StringUtils.isEmpty(oldValue) && oldValue.equals(currentValue)) {
return true;
}
}
return false;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
Suppose there are two threads now A and B At the same time lock() Method , That is, the of these two threads value It's exactly the same , All for value=2020-12-19, And they all carry out String oldValue = redisTemplate.opsForValue().getAndSet(key, value);
, There will be one to execute first and then :
If a thread A Execute first , Back to oldValue=2020-12-18
, Simultaneous setting value = 2020-12-19
, because oldvalue=currentValue
return true, namely A The thread is locked ;
here B The thread continues to execute , Back to oldValue=2020-12-19,oldvalue!=currentValue
, return false, Locking failed
So the logic of this code is to lock only one thread
边栏推荐
- Android advanced interview question record in 2022
- How to build a technical team that will bring down the company?
- Open source SPL optimized report application coping endlessly
- RichView TRVUnits 图像显示单位
- Outlook:总是提示输入用户密码
- Codeforces Round #770 (Div. 2) ABC
- A label making navigation bar
- Official announcement! The third cloud native programming challenge is officially launched!
- Li Kou Jianzhi offer -- binary tree chapter
- A label colorful navigation bar
猜你喜欢
What sparks can applet container technology collide with IOT
Li Kou Jianzhi offer -- binary tree chapter
Win:使用 PowerShell 检查无线信号的强弱
[技术发展-26]:新型信息与通信网络的数据安全
Win: use shadow mode to view the Desktop Session of a remote user
Application and Optimization Practice of redis in vivo push platform
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
Tucson will lose more than $400million in the next year
MySQL backup and recovery + experiment
Security level
随机推荐
batchnorm. Py this file single GPU operation error solution
MATLB|多微电网及分布式能源交易
pytorch fine-tuning (funtune) : 镂空设计or 偷梁换柱
172. Zero after factorial
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
Comment mettre en place une équipe technique pour détruire l'entreprise?
MySQL regexp: Regular Expression Query
Go RPC call
Android advanced interview question record in 2022
Data guard -- theoretical explanation (III)
[technology development-26]: data security of new information and communication networks
流批一體在京東的探索與實踐
Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
Luo Gu Pardon prisoners of war
Introduce reflow & repaint, and how to optimize it?
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
Yolov5 model training and detection
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Naacl 2021 | contrastive learning sweeping text clustering task