当前位置:网站首页>redis分布式锁
redis分布式锁
2022-07-17 05:57:00 【程序三两行】
一、常见分布式锁
基于数据库实现
基于redis实现(性能最高)
基于zookeeper实现(可靠性最高)
二、基于redis实现实现分布式锁
1、命令
1、设置锁 命令:setnx key_name value
setnx users 10
接着执行 setnx user 20 返回0 操作失败 因为上锁了
2、释放锁 命令:del key_name
del users
3、过期设置 命令:expire key_name value
expire users 20
即上锁又设置过期时间使操作称为原子操作
set users 10 nx ex 122、java代码


三、uuid防止误删锁操作
问题分析

解决
设置不同的uuid,释放锁定的时候判断当前的uuid和要释放的锁uuid是否一样
上面代码修改
设置uuid

比较释放

四、lua保证删除操作原子性



![]()


边栏推荐
- 机器学习面试题(转载)
- 1669. Merge two linked lists (merge of two linked lists)
- 理解LSTM和GRU
- Spark3.x entry to mastery - stage 7 (spark dynamic resource allocation)
- 正则表达示提取匹配内容
- Introduction to arm development environment
- VMware cloud director 10.4 release (including download) - cloud computing provisioning and management platform
- Titanic passenger rescue prediction (Advanced)
- 2022 review questions and mock exams for main principals of hazardous chemical business units
- Practical tutorial: application of canoe in CAN bus test
猜你喜欢

Spark3.x-practical double flow join (window and redis implementation method and template code)

Visit Beijing Zoo in dog days

Flink introduction to practice - phase II (illustrated runtime architecture)

Pytorch随记(3)

Canel Introduction & use

预测销售XGBoost

Pytoch notes (1)

【day01】前言、入门程序、常量变量

修改滚动条样式

力扣114题:二叉树展开链表
随机推荐
Random forest of machine learning
[operation rules] how to realize TSN system level test?
[day01] preface, introductory program, constant variables
js数组交集、差集和并集
Practical tutorial: application of canoe in CAN bus test
Wechat oauth2.0 login process and security analysis
Pytorch notes (5)
MongoDB的使用
Flink introduction to practice - phase II (illustrated runtime architecture)
如何选择合适的模型
机器学习之随机森林
养老年金保险有必要买吗?适合老人的养老年金产品有哪些?
Facial key point detection CNN
Pytoch notes (1)
FMC sub card: 8-channel 125msps sampling rate 16 bit AD acquisition sub card
Flutter3.0 (framework) - UI rendering
standard-version(发版与 Changelog 自动化)
INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
Ku115 FPGA high performance 10G Optical fiber network hardware accelerator card / 2-way 10G Optical fiber data accelerator card
Modify scroll bar style