Redis command details and Usage Scenarios Example--transaction (transactional)

Source: Internet
Author: User
Tags redis versions
DISCARD

Cancels the transaction, discarding all commands within the transaction block.
If you are using the Watch command to monitor a (or some) key, then canceling all monitoring is equivalent to executing command unwatch.
Available Versions:
2.0.0+
complexity of Time:
O (1).
return Value:
Always return OK.

redis> MULTI
OK
redis> PING
QUEUED
redis> SET Greeting "Hello"
QUEUED
redis> DISCARD
OK
EXEC

Executes the commands within all the transaction blocks.
If a (or some) key is under the Watch command, and there are commands related to this (or these) key in the transaction block, then the EXEC command will be interrupted (abort) only if the (or these) key is not modified by another command.
Available Versions:
1.2.0+
complexity of Time:
The sum of the time complexity of all commands within a transaction block.
return Value:
The return value of all commands within a transaction block, sorted by the order in which they were executed.
When the operation is interrupted, a null value of nil is returned.
The transaction was successfully executed

redis> MULTI
OK
redis> INCR user_id
QUEUED
redis> INCR user_id
QUEUED
redis> INCR user_id
QUEUED
redis> PING
QUEUED
redis> EXEC
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) PONG

Monitor key, and the transaction executes successfully

Redis> WATCH lock Lock_times
OK
redis> MULTI
OK
redis> SET lock "Huangz"
QUEUED
redis> INCR lock_times
QUEUED
redis> EXEC
1) OK
2) (integer) 1

Monitor key, and the transaction is interrupted

Redis> WATCH lock Lock_times
OK
redis> MULTI
OK
redis> SET lock "Joe"        # just then, Another client modified the value of Lock_times, concurrentmodifyed
QUEUED
redis> INCR lock_times
QUEUED
redis> EXEC                  # Because Lock_times was modified, Joe's transaction execution failed
(nil)
MULTI

Marks the beginning of a transaction block.
Multiple commands within a transaction block are placed in a queue in order of precedence, and finally executed by the EXEC command atomicity (Atomic).
Available Versions:
1.2.0+
complexity of Time:
O (1).
return Value:
Always return OK.

redis> MULTI            # Mark transaction start
OK
redis> INCR user_id     # Multiple commands queued in order
QUEUED
redis> INCR user_id
QUEUED
redis> INCR user_id
QUEUED
redis> PING
QUEUED
redis> EXEC             # execution
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) PONG
unwatch

Cancels the watch command's monitoring of all keys.
If the EXEC command or the DISCARD command is executed first after the WATCH command is executed, then no more unwatch is required.
Because the EXEC command executes a transaction, the effect of the Watch command has already been generated, and the DISCARD command cancels all monitoring of the key while canceling the transaction, so after these two commands are executed, there is no need to execute unwatch.
Available Versions:
2.2.0+
complexity of Time:
O (1)
return Value:
Always OK.

Redis> WATCH lock Lock_times
OK
redis> unwatch
OK
WATCH key [key ...]

Monitor one (or more) key, and if this (or these) key is changed by another command before the transaction executes, the transaction will be interrupted.
Available Versions:
2.2.0+
complexity of Time:
O (1).
return Value:
Always return OK.
Redis> WATCH Lock Lock_times
Ok

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.