当前位置:网站首页>Redis:Redis的事务
Redis:Redis的事务
2022-07-04 22:43:00 【dengfengling999】


Redis的事务允许在一次单独的步骤中执行一组命令,并且能够保证将一个事务中的所有命令序列化,然后按顺序执行;在一个Redis事务中,Redis要么执行其中的所有命令,要么什么都不执行。即Redis的事务要能够保证序列化和原子性。
Redis的事务只能保持部分原子性,不能保持完全原子性,数据可能存在不一致
Redis对事物进行了简化,为什么简化?它要提高它的访问效率,它不考虑那么多事务的移植性
- Redis事务的常用命令:
- multi
语法:multi
功能:用于标记事务块的开始。Redis会将后续的命令逐个放入队列中,然后才能使用EXEC命令原子化地执行这个命令序列。
返回值:开启成功返回OK

例如:

2.exec
语法:exec
功能:在一个事务中执行所有先前放入队列的命令,然后恢复正常的连接状态。
如果在把命令压入队列的过程中报错,则整个队列中的命令都不会执行,执行结果报错;
如果在压队列的过程中正常,在执行队列中某一个命令报错,则只会影响本条命令的执行结果,其它命令正常运行;
当使用WATCH命令时,只有当受监控的键没有被修改时,EXEC命令才会执行事务中的命令;而一旦执行了exec命令,之前加的所有watch监控全部取消。
返回值:这个命令的返回值是一个数组,其中的每个元素分别是原子化事务中的每个命令的返回值。 当使用WATCH命令时,如果事务执行中止,那么EXEC命令就会返回一个Null值。
inc k1 :对数值型k1 进行加一,如果不是数值型的话汇报错,在压入对列的时候不会真正执行,所以不知道k1是不是数值型
例如:压入对列报错,都不执行,保持原子性

压入对列不报错:

3.discard
语法:discard
功能:清除所有先前在一个事务中放入队列的命令,并且结束事务。
如果使用了WATCH命令,那么DISCARD命令就会将当前连接监控的所有键取消监控。
返回值:清除成功,返回OK。

例如:
4.watch

语法:watch key [key …]
功能:当某个事务需要按条件执行时,就要使用这个命令将给定的键设置为受监控的。如果被监控的key值在本事务外有修改时,则本事务所有指令都不会被执行。Watch命令相当于关系型数据库中的乐观锁。
返回值:监控成功,返回OK。
decrby:减指定值 incrby:加指定值
例如:

建立另外一个连接进行更改vcersion



5.unwatch
语法:unwatch
功能:清除所有先前为一个事务监控的键。
如果在watch命令之后你调用了EXEC或DISCARD命令,那么就不需要手动调用UNWATCH命令。
返回值:清除成功,返回OK。

例如:清除所有监控的

改变version:

Redis事务小结:
1、单独的隔离操作:事务中的所有命令都会序列化、顺序地执行。事务在执行过程中,不会被其它客户端发来的命令请求所打断,除非使用watch命令监控某些键。
2、不保证事务的原子性:redis同一个事务中如果一条命令执行失败,其后的命令仍然可能会被执行,redis的事务没有回滚。Redis已经在系统内部进行功能简化,这样可以确保更快的运行速度,因为Redis不需要事务回滚的能力。
边栏推荐
- 【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
- JS card style countdown days
- The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
- D3.js+Three. JS data visualization 3D Earth JS special effect
- Serial port data frame
- 企业如何跨越数字化鸿沟?尽在云原生2.0
- Redis introduction complete tutorial: detailed explanation of ordered collection
- Advanced area of attack and defense world misc 3-11
- ETCD数据库源码分析——处理Entry记录简要流程
- 云服务器设置ssh密钥登录
猜你喜欢

Redis入门完整教程:列表讲解

The overview and definition of clusters can be seen at a glance

Redis getting started complete tutorial: publish and subscribe

Analysis of the self increasing and self decreasing of C language function parameters

攻防世界 misc 进阶区 2017_Dating_in_Singapore

Redis入门完整教程:慢查询分析

Persistence mechanism of redis

浅聊一下中间件

Attack and defense world misc advanced area ditf

Redis introduction complete tutorial: Collection details
随机推荐
Breakpoint debugging under vs2019 c release
VIM editor knowledge summary
Attack and defense world misc advanced area ditf
Analysis of the self increasing and self decreasing of C language function parameters
Duplicate ADMAS part name
PS style JS webpage graffiti board plug-in
Redis入门完整教程:客户端通信协议
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
LabVIEW中比较两个VI
堆排序代码详解
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
Sword finger offer 68 - ii The nearest common ancestor of binary tree
【剑指Offer】6-10题
A complete tutorial for getting started with redis: transactions and Lua
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
企业如何跨越数字化鸿沟?尽在云原生2.0
How can enterprises cross the digital divide? In cloud native 2.0
攻防世界 MISC 进阶 glance-50
Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
Redis入门完整教程:列表讲解