当前位置:网站首页>Redis事务与watch指令
Redis事务与watch指令
2022-06-26 14:38:00 【华为云】
Redis事务与watch指令
Redis事务
redis的事务是MULTI指令表明敞开事物,敞开业务后客户端发送的EXEC DISCARD WATCH MULTI指令立即履行外,其他指令后会放入业务队列中,然后回来给客户端消息:QUEUE
当客户端向服务器发送EXEC指令的时分,服务器遍历客户端的业务队列,履行保存的一切指令,并把一切的履行结果回来给客户端。
watch指令
watch指令是可以监督多个数据库键,当excc指令履行的时分,假如被监督的数据库键被修正过了,业务履行失利,原理就是redis数据库中有个watched_keys字典,字典键是被watch指令监督的数据库键,值是链表,记录一切监督这个键的客户端信息。
当履行数据库修正的指令的时分会调用touchWatchedKey函数,touchWatchedKey函数中会对修正键的客户端设置CLIENT_DIRTY_CAS标识
// 当key被修正,调用该函数void signalModifiedKey(redisDb *db, robj *key) { touchWatchedKey(db,key);}// Touch 一个 key,假如该key正在被监督,那么客户端会履行EXEC失利void touchWatchedKey(redisDb *db, robj *key) { list *clients; listIter li; listNode *ln; // 假如数据库中没有被监督的key,直接回来 if (dictSize(db->watched_keys) == 0) return; // 找出监督该key的client链表 clients = dictFetchValue(db->watched_keys, key); // 没找到回来 if (!clients) return; /* Mark all the clients watching this key as CLIENT_DIRTY_CAS */ /* Check if we are already watching for this key */ listRewind(clients,&li); // 遍历一切监督该key的client while((ln = listNext(&li))) { client *c = listNodeValue(ln); // 设置CLIENT_DIRTY_CAS标识 c->flags |= CLIENT_DIRTY_CAS; }}然后服务器收到客户端的exec指令时,服务器依据客户端的CLIENT_DIRTY_CAS标识判断,假如有这个标识,说明被修正过了,服务器就会拒绝客户端提交的业务。
总结
这就是redis事务的相关内容,事务开启通过MULTI命令,所有的命令都会进入事务队列中,当执行exec命令的时候,在事务队列中的命令再一起提交,watch命令用来监视键,当事务过程中有键被修改的话,这个键对应的监视器就能检测到,然后事务就会提交失败,保证事务一致性。
️ 感谢大家
如果你觉得这篇内容对你挺有有帮助的话:
- 欢迎关注我️,点赞,评论,转发
- 关注
盼盼小课堂,定期为你推送好文,还有群聊不定期抽奖活动,可以畅所欲言,与大神们一起交流,一起学习。 - 有不当之处欢迎批评指正。
边栏推荐
- Excel-vba quick start (II. Condition judgment and circulation)
- 在云服务器中云磁盘如何挂载
- 《三体》摘录
- How to mount cloud disks in ECS
- Electron
- 券商经理给的开户链接办理股票开户安全吗?我想开个户
- Use of subqueries
- 登录认证服务
- Equation derivation: second order active bandpass filter design! (download: Tutorial + schematic + Video + code)
- Mark一下 Unity3d在Inspector中选中不了资源即Project锁定问题
猜你喜欢

How to personalize VIM editor format (DIY)

获取两个dataframe的交并差集

Keil4打开单片机工程一片空白,cpu100%程序卡死的问题解决

This is the graceful file system mounting method, which is effective through personal testing

Mark: unity3d cannot select resources in the inspector, that is, project locking

权威发布 | 延安大学2022年教师岗位招聘公告

Question bank and answers of the latest Guizhou construction eight (Mechanics) simulated examination in 2022

重磅白皮书发布,华为持续引领未来智慧园区建设新模式

Combat readiness mathematical modeling 32 correlation analysis 2

Deploy the flask environment using the pagoda panel
随机推荐
Common evaluation indexes of classification model -- confusion matrix and ROC curve
Electron
R语言epiDisplay包的tableStack函数制作统计汇总表格(分组的描述性统计、假设检验等)、不设置by参数计算基础描述性统计信息、指定对于大多数样本负相关的变量进行反序
datasets Dataset类(2)
通俗语言说BM3D
Sharing ideas for a quick switch to an underlying implementation
C语言基础知识入门(大全)「建议收藏」
D - Face Produces Unhappiness
Mark一下 Unity3d在Inspector中选中不了资源即Project锁定问题
GDAL and opencv smooth and blur TIF images
Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
Datasets dataset class (2)
Detailed explanation of C language programming problem: can any three sides form a triangle, output the area of the triangle and judge its type
C语言刷题随记 —— 乒乓球比赛
fileinput. js php,fileinput
Go变量的声明与赋值
Authoritative announcement on the recruitment of teachers in Yan'an University in 2022
feil_uVission4左侧工目录消失
聊聊 RPA 方向的规划:简单有价值的事情长期坚持做
'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册