当前位置:网站首页>14_Redis_乐观锁
14_Redis_乐观锁
2022-07-02 12:00:00 【听*雨声】
乐观锁
监控:Watch
悲观锁:
- 很悲观,认为什么时候都会出问题,无论做什么都会加锁!
乐观锁:
- 很乐观,认为什么时候都不会出问题,所以不会上锁!更新数据的时候去判断一下,在此期间是否有人修改过这个数据,version !
- 获取version
- 更新的时候比较version
Redis的监视测试
正常执行成功
127.0.0.1:6379> set money 100
OK
127.0.0.1:6379> set out 0
OK
127.0.0.1:6379> watch money // 监视 money对象 ,一旦事务执行成功监控就会取消
OK
127.0.0.1:6379> multi // 事务正常结束,数据期间没有发生变动,这个时候就正常执行成功!
OK
127.0.0.1:6379(TX)> decrby money 20
QUEUED
127.0.0.1:6379(TX)> INCRby out 20
QUEUED
127.0.0.1:6379(TX)> exec
1 ) (integer) 80
2 ) (integer) 20
测试多线程修改值,使用watch可以当做redis的乐观锁操作!

修改方式:释放乐观锁后重新获取乐观锁
1.事务执行失败,就先解锁
获取最新的值,再次监视,select version


边栏推荐
猜你喜欢

04_ 栈

你不知道的Set集合

LeetCode 2310. 个位数字为 K 的整数之和

Advanced C language (realize simple address book)

Introduction to mathjax (web display of mathematical formulas, vector)

Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly

XML配置文件

Learn the method code example of converting timestamp to uppercase date using PHP

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
随机推荐
C语言中的printf函数和scanf函数
Solve the problem that El radio group cannot be edited after echo
MFC 定时器使用
About text selection in web pages and counting the length of selected text
Tidb data migration tool overview
Dragonfly low code security tool platform development path
使用 TiUP 部署 TiDB 集群
02_线性表_顺序表
Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
Fundamentals of software testing
C# 线程传参
解决el-radio-group 回显后不能编辑问题
18_Redis_Redis主从复制&&集群搭建
TiDB跨数据中心部署拓扑
AtCoder Beginner Contest 254
Kibana basic operation
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
C#延时、在线程中开启定时器、获取系统时间
数据库内容输出有问题怎么解决