当前位置:网站首页>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
边栏推荐
- Timescaledb 2.5.2 release, time series database based on PostgreSQL
- Rabbit MQ message sending of vertx
- Runc hang causes the kubernetes node notready
- 【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
- Unified blog writing environment
- Collection of gmat750 wrong questions
- PowerShell: use PowerShell behind the proxy server
- [机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
- Can financial products be redeemed in advance?
- Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
猜你喜欢
Codeforces Global Round 19 ABC
Application and Optimization Practice of redis in vivo push platform
Binary tree traversal - middle order traversal (golang)
Interesting practice of robot programming 15- autoavoidobstacles
流批一體在京東的探索與實踐
Restful fast request 2022.2.1 release, support curl import
Do you know the eight signs of a team becoming agile?
线上故障突突突?如何紧急诊断、排查与恢复
力扣剑指offer——二叉树篇
Variables in postman
随机推荐
Application and Optimization Practice of redis in vivo push platform
How to make a cool ink screen electronic clock?
Richview trvunits image display units
Change the background color of a pop-up dialog
PHP Basics - detailed explanation of DES encryption and decryption in PHP
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
Practical case of SQL optimization: speed up your database
Comment mettre en place une équipe technique pour détruire l'entreprise?
Visual explanation of Newton iteration method
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Codeforces Global Round 19 ABC
Tla+ through examples (XI) -- propositional logic and examples
Summary of regularization methods
CAM Pytorch
Introduce reflow & repaint, and how to optimize it?
A label colorful navigation bar
. Net starts again happy 20th birthday
Using druid to connect to MySQL database reports the wrong type