Redis transaction introduction and application

Source: Internet
Author: User

It has been several months since reids was studied and used. I have summarized a lot of documents and related materials. In the next time, I will share some relevant materials, this article also describes how to use it in our applications.

The following is an introduction to redis (refer to redis. io ):

I. Use of things

1. Redis transactions start with the MULTI command. This command always returns OK.

2. the user can then execute multiple commands. redis will not immediately execute these commands, but will only put them into the queue.

3. When the exec command is executed, all the commands are executed.

4. Calling the discard command will flush the transaction queue and exit the transaction.

As follows:

Redis 127.0.0.1: 6379> multi

OK

Redis 127.0.0.1: 6379> set foo 1

QUEUED

Redis 127.0.0.1: 6379> incr foo

QUEUED

Redis 127.0.0.1: 6379> incr foo

QUEUED

Redis 127.0.0.1: 6379> exec

1) OK

2) (integer) 2

3) (integer) 3

 

From the above session, we can see that the reply returned by the multi command is an array. Each element is the reply of every instruction in the transaction, and it is in the same order as the command release. When redis is connected to a multi request, all command responses are queued unless the statement of this command is incorrect. Some commands have correct syntax, but errors in the execution phase are also allowed.

See the following

Redis 127.0.0.1: 6379> multi

OK

Redis 127.0.0.1: 6379> set t 13

QUEUED

Redis 127.0.0.1: 6379> lpop t

QUEUED

Redis 127.0.0.1: 6379> exec

1) OK

2) (error) ERR Operation against a keyholding the wrong kind of value

For this type of err, the client must give a reasonable prompt.

Note that all commands in the queue will be executed, and redis will not terminate the command execution.

Ii. Cancel queue commands
Discard is used to cancel the command queue. A thing can be broken. No commands will be executed, and the connection status is normal.

For example:

> SET foo 1

OK

> MULTI

OK

> INCR foo

QUEUED

> DISCARD

OK

> GET foo

"1"

  • 1
  • 2
  • 3
  • Next Page

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.