当前位置:网站首页>Redis: redis transactions
Redis: redis transactions
2022-07-04 23:06:00 【dengfengling999】
Redis The transaction allows a set of commands to be executed in a single step , And it can ensure that all commands in a transaction are serialized , Then execute in order ; In a Redis Transaction ,Redis Or execute all the commands in it , Or nothing . namely Redis Transactions should be able to ensure serialization and atomicity .
Redis Transactions of can only maintain partial atomicity , Cannot remain completely atomic , Data may be inconsistent
Redis Simplify things , Why simplify ? It should improve its access efficiency , It does not consider the portability of so many transactions
- Redis Common commands for transactions :
- multi
grammar :multi
function : Used to mark the beginning of a transaction block .Redis The subsequent commands will be put into the queue one by one , And then you can use it EXEC The command executes the command sequence atomically .
Return value : Open successfully and return OK
for example :
2.exec
grammar :exec
function : Execute all previously queued commands in a transaction , Then restore the normal connection state .
If an error is reported in the process of pushing the command into the queue , Then the commands in the whole queue will not be executed , Error in execution result ;
If it's normal in the process of pressing the queue , An error is reported for a command in the execution queue , It will only affect the execution result of this command , Other commands operate normally ;
When using WATCH On command , Only if the monitored key has not been modified ,EXEC Command will execute the command in the transaction ; And once implemented exec command , All the previously added watch All monitoring is cancelled .
Return value : The return value of this command is an array , Each of these elements is the return value of each command in the atomized transaction . When using WATCH On command , If the transaction execution aborts , that EXEC The command will return a Null value .
inc k1 : For numerical type k1 Add one , If it is not numerical, report an error , It will not really execute when pressing the opposite column , So I don't know k1 Is it numerical
for example : Press in the right column to report errors , Do not carry out , Keep atomicity
Press the right column without reporting an error :
3.discard
grammar :discard
function : Clear all previously queued commands in a transaction , And end the transaction .
If used WATCH command , that DISCARD The command will cancel the monitoring of all keys currently connected to the monitoring .
Return value : Clear successfully , return OK.
for example :
4.watch
grammar :watch key [key …]
function : When a transaction needs to be executed conditionally , Use this command to set the given key to be monitored . If monitored key When the value is modified outside this transaction , Then the instructions of the firm will not be executed .Watch The command is equivalent to an optimistic lock in a relational database .
Return value : Monitoring successful , return OK.
decrby: Minus the specified value incrby: Add the specified value
for example :
Establish another connection to make changes vcersion
5.unwatch
grammar :unwatch
function : Clear all previously monitored keys for a transaction .
If in watch After the command, you call EXEC or DISCARD command , So you don't have to call it manually UNWATCH command .
Return value : Clear successfully , return OK.
for example : Clear all monitored
change version:
Redis Business summary :
1、 Separate isolation operation : All commands in the transaction are serialized 、 Execute in sequence . The transaction is in progress , Will not be interrupted by command requests from other clients , Unless used watch Commands monitor certain keys .
2、 The atomicity of transactions is not guaranteed :redis If a command fails in the same transaction , Subsequent orders may still be executed ,redis The transaction was not rolled back .Redis Function simplification has been carried out inside the system , This ensures faster running speed , because Redis The ability to roll back transactions is not required .
边栏推荐
猜你喜欢
EditPlus--用法--快捷键/配置/背景色/字体大小
cout/cerr/clog的区别
智力考验看成语猜古诗句微信小程序源码
VIM editor knowledge summary
JS card style countdown days
Redis getting started complete tutorial: hash description
Unity vscode emmylua configuration error resolution
Serial port data frame
colResizable. JS auto adjust table width plug-in
Redis introduction complete tutorial: slow query analysis
随机推荐
SPH中的粒子初始排列问题(两张图解决)
ffmpeg快速剪辑
9 - class
Qt加法计算器(简单案例)
剑指Offer 68 - II. 二叉树的最近公共祖先
Redis:Redis的事务
微信公众号解决从自定义菜单进入的缓存问题
Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?
Excel 快捷键-随时补充
Redis introduction complete tutorial: detailed explanation of ordered collection
Principle of lazy loading of pictures
vim编辑器知识总结
Redis入门完整教程:客户端通信协议
Sobel filter
How to send a reliable request before closing the page
Sword finger offer 67 Convert a string to an integer
【剑指offer】1-5题
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
SHP data making 3dfiles white film