当前位置:网站首页>Redis学习笔记—事务
Redis学习笔记—事务
2022-06-23 09:00:00 【爱锅巴】
事务简单理解就是一组命令要么执行,要么全部不执行,否则执行一部分数据会造成数据不一致的问题
Redis提供了简单的事务功能,不支持回滚操作,将一组需要批量执行的命令放到multi和exec两个命令之间就可以实现简单地事务功能。
- multi命令代表事务开始
- exec命令代表事务结束
- discard命令代表中途中断
下面添加一个名字为Charlie的成绩信息,要求一气呵成的添加,如果没有输入exec之前,数据的返回值是“QUEUED”排队的状态,输入exec之后数据就被写入到Redis里面了(如果中途想要中断就输入discard)
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set Charlie:english:score 88
QUEUED
127.0.0.1:6379> set Charlie:math:score 75
QUEUED
127.0.0.1:6379> set Charlie:totalscore 163
QUEUED
127.0.0.1:6379> exec
1) OK
2) OK
3) OK
- 如果整个执行过程中命令输错,比如set写成了sett写错了,这样整个事务就无法执行了
- 如果整个执行过程中输错命令,把set写成功了zset,因为zset原来就存在这个命令所以不会报错,需要开发人员自行处理
watch
有些应用场景需要在事务之前,确保事务中的key没有被其他客户端修改过,才执行事务,否则不执行(类似乐观锁)。Redis提供了watch命令来解决这类问题
客户端1中使用watch命令标记key为“java”的键
127.0.0.1:6379> watch java
OK
客户端2操作设置了key为“java”的值
127.0.0.1:6379> set java jedis
OK
客户端1进行事务操作,会发现exec之后返回(nil)没有执行成功,watch起到了效果
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set java Jedis
QUEUED
127.0.0.1:6379> exec
(nil)
边栏推荐
- MySQL fault case | error 1071 (42000): specified key was too long
- Cookie和Session入门
- [qnx hypervisor 2.2 user manual]6.2 network
- Community article | mosn building subset optimization ideas sharing
- Mysql 数据库入门总结
- 438. Find All Anagrams in a String
- Balls and cows of leetcode topic analysis
- Best time to buy and sell stock
- Leetcode topic analysis spiral matrix II
- Summary of Arthas vmtool command
猜你喜欢

Servlet-02 生命周期

自定义标签——jsp标签基础

Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“

点击添加下拉框

JSP入门总结

Custom tags - JSP tag enhancements

GeoServer adding mongodb data source

Simple student management

The sliding window of the force button "step by step" (209. sub array with the smallest length, 904. fruit basket)

简易学生管理
随机推荐
Click Add drop-down box
进入小公司的初级程序员要如何自我提高?
[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24
3. caller service call - dapr
Happy number of leetcode topic analysis
Redis学习笔记—主从复制
js 用**遮罩身份证以及手机号的重要数据
Redis学习笔记—慢查询分析
Simple student management
线性表(LinkList)的链式表示和实现----线性结构
Quartz Crystal Drive Level Calculation
986. Interval List Intersections
Subsets of leetcode topic resolution
670. Maximum Swap
How to use matrix analysis to build your thinking scaffold in flowus, notation and other note taking software
Leetcode topic analysis set matrix zeroes
Summary ranges of leetcode topic resolution
65. Valid Number
Intelligent operation and maintenance exploration | anomaly detection method in cloud system
node request模塊cookie使用