当前位置:网站首页>13_Redis_事务
13_Redis_事务
2022-07-02 12:00:00 【听*雨声】
事务
Mysql:ACID
原子性:要么同时成功,要么同时失败
Redis事务本质︰一组命令的集合!一个事务中的所有命令都会被序列化,在事务执行过程的中,会按照顺序执行!
特性:
- 一次性:一组命令会在一个队列中一次性执行
- 顺序性:一组命令按照顺序执行
- 排他性:事务在执行的过程中是不允许被别人打断的
Redis事务没有没有隔离级别的概念!
所有的命令在事务中,并没有直接被执行!只有发起执行命令的时候才会执行!Exec
Redis单条命令是保证原子性的,但是事务不保证原子性!
redis的事务∶
- 开启事务(muti)
- 命令入队(…)
- 执行事务 (exec)
事务操作
正常执行事务
127.0.0.1:6379> multi // 开启事务
OK
127.0.0.1:6379(TX)> set k1 v1 // 命令入队
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> get k1
QUEUED
127.0.0.1:6379(TX)> set k3 v3
QUEUED
127.0.0.1:6379(TX)> exec //执行事务
1 ) OK
2 ) OK
3 ) “v1”
4 ) OK
放弃事务
127.0.0.1:6379(TX)> set k1 v11
QUEUED
127.0.0.1:6379(TX)> set k2 v22
QUEUED
127.0.0.1:6379(TX)> set k4 v4
QUEUED
127.0.0.1:6379(TX)> DISCARD // 取消事务
OK
127.0.0.1:6379> get k4 // 事务队列中命令都不会被执行!
(nil)
事务异常
编译型异常(代码有问题!命令有错!),事务中所有的命令都不会被执行!
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> set k1 v1
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> set k3 v3
QUEUED
127.0.0.1:6379(TX)> getset k2 // 错误的命令
(error) ERR wrong number of arguments for ‘getset’ command
127.0.0.1:6379(TX)> set k4 v4
QUEUED
127.0.0.1:6379(TX)> exec // 执行事务报错! 所有的命令都不会被执行!
(error) EXECABORT Transaction discarded because of previous errors.
运行时异常( 1/0),如果事务队列中存在语法性,那么执行命令的时候,其他命令是可以正常执行的,错误命令抛出异常!(也就是说事务没有原子性)
127.0.0.1:6379> set k1 “v1”
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> INCR k1 // 字符串是不能加一操作的
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> set k3 v3
QUEUED
127.0.0.1:6379(TX)> get k3
QUEUED
127.0.0.1:6379(TX)> exec
1 ) (error) ERR value is not an integer or out of range // 虽然第一条命令报错了,但是依旧正常执行成功了!
2 ) OK
3 ) OK
4 ) “v3”
127.0.0.1:6379> get k2
“v2”
边栏推荐
- 广州市应急管理局发布7月高温高湿化工安全提醒
- forEach的错误用法,你都学废了吗
- Sharp tool SPL for post SQL calculation
- How to write sensor data into computer database
- [noi Simulation Competition] scraping (dynamic planning)
- Application and practice of Jenkins pipeline
- Printf function and scanf function in C language
- MFC 控制台打印,弹出对话框
- Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
- Introduction to C language -- array
猜你喜欢

The past and present lives of visual page building tools

19_Redis_宕机后手动配置主机

【无标题】LeetCode 2321. 拼接数组的最大分数

C#代码审计实战+前置知识

TiDB数据迁移工具概览
![[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)](/img/7b/2c471c070a3faa981f70136603495a.jpg)
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)

GeoServer offline map service construction and layer Publishing

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

LeetCode 2310. The number of digits is the sum of integers of K

C code audit practice + pre knowledge
随机推荐
可视化搭建页面工具的前世今生
IE 浏览器正式退休
C语言中的算术运算及相关练习题
蜻蜓低代码安全工具平台开发之路
Table responsive layout tips
PHP method to get the index value of the array item with the largest key value in the array
Application of CDN in game field
Tidb data migration scenario overview
广州市应急管理局发布7月高温高湿化工安全提醒
Practical debugging skills
为什么只会编程的程序员无法成为优秀的开发者?
[noi simulation] Elis (greedy, simulation)
04_ 栈
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
[untitled] leetcode 2321 Maximum score of concatenated array
Introduction to mathjax (web display of mathematical formulas, vector)
MFC CString to char*
MFC console printing, pop-up dialog box
原则、语言、编译、解释
LeetCode 209. Minimum length subarray