(ii) Redis notes--Publish & Subscribe, transactions, database operations

Source: Internet
Author: User

1. Redis Publish Subscription

1.1 Overview

A Redis Publish Subscription (PUB/SUB) is a message communication pattern: the Sender (pub) sends a message and the Subscriber (sub) receives the message.

Redis clients can subscribe to any number of channels.

Shows the relationship between channel Channel1 and the three client--client2, CLIENT5, and client1 subscribing to this channel:

When a new message is sent to channel Channel1 via the PUBLISH command, the message is sent to the three clients subscribed to it:

1.2 Steps:

1.2.1. Created a subscription channel named Redischat:

127.0. 0.1:6379>  SUBSCRIBE redischatreading messages ... (Press Ctrl -  to quit) 1 ) "Subscribe"2) "Redischat"3) (integer1

1.2.2. Re-open a Redis client and then publish two messages on the same channel Redischat, the Subscriber will be able to receive the message.

[[email protected] Desktop]# Redis-CLI127.0.0.1:6379>PUBLISH Redischat "Redis isvery useful "(integer)1127.0.0.1:6379>PUBLISH redischat "Learn Redis isneccessary "(integer)1

1.2.3. subscriber's client Auto-presence content

127.0.0.1:6379>SUBSCRIBE redischatreading Messages ... (Press Ctrl-C toquit)1) "Subscribe"2) "Redischat"3) (integer)1#订阅者的客户端出现如下内容1) "Message"2) "Redischat"3) "Redis isvery useful "1) "Message"2) "Redischat"3) "Learn Redis isNeccessary "

2. Redis Transactions

Redis transactions can execute multiple commands at once, with the following two important guarantees:

    • A transaction is a separate quarantine operation: All commands in a transaction are serialized and executed sequentially. The transaction is not interrupted by a command request sent by another client during execution.
    • A transaction is an atomic operation: the commands in a transaction are either all executed or none of them are executed.

A transaction goes through the following three stages from start to execution:

    • Begins a transaction.
    • command to queue.
    • Executes a transaction.

Here is an example of a transaction that begins a transaction with MULTI , then enqueue multiple commands into the transaction, and finally the EXEC command triggers the transaction, executing all the commands in the transaction:

127.0.0.1:6379>Multiok127.0.0.1:6379> Setname "Xiaofeng" QUEUED127.0.0.1:6379>Get namequeued127.0.0.1:6379>sadd Girlfriends "Jenny" "Amy" "Lily" QUEUED127.0.0.1:6379>smembers girlfriendsqueued127.0.0.1:6379> EXEC1) OK2) "Xiaofeng"3) (integer)34)1) "Jenny"2) "Amy"3) "Lily"
3. Redis Script

The Redis script uses the LUA interpreter to execute the script. The usual commands for executing scripts are EVAL. Basic syntax

EVAL script Numkeys key [key ...] arg [arg ...]
127.0.0.1:6379>EVAL "return{KEYS[1], KEYS[2]Argv[1]Argv[2]}"2key1 Key2 First Second1) "Key1"2) "Key2"3) "First"4) "Second"

4. Data backup and Recovery

Data backup


Ok

The change command creates the file Dump.rdb in the Redis installation directory and saves the data in that file.

Viewing the installation directory for Redis

127.0. 0.1:6379>  config get dir1) "dir"2) "/ Root/\xe6\xa1\x8c\xe9\x9d\xa2 "

Data recovery

Simply copy the backup file Dump.rdb to the Redis installation directory.

5. Database Operations

Redis, a total of 16 databases, respectively, is 0~15, in general, enter the database default number is 0, if we want to enter the specified database, you can use the SELECT statement

Switch to a database that is numbered 5

127.0. 0.1:6379>Select5OK127.0. 0.1:6379[5]>

To view all the key values in the database

127.0.0.1:6379[5]> SetKey1 oneok127.0.0.1:6379[5]> SetKey6 Sixok127.0.0.1:6379[5]>Keys*1) "Key6"2) "Key1"

Returns the number of all keys in the current database: Dbsize

Delete all key:flushdb in the current database

Clears all key:flushall in all databases

Transfer the key from the current database to the specified database: move a aim_db

127.0.0.1:6379> Select 2OK127.0.0.1:6379[2]> Setname Davidok127.0.0.1:6379[2]>Move Name5(integer)1127.0.0.1:6379[2]> Select 5OK127.0.0.1:6379[5]>Keys*1) "Name"127.0.0.1:6379[5]>Get Name "David"127.0.0.1:6379[5]>Dbsize (integer)1127.0.0.1:6379[5]>Flushdbok127.0.0.1:6379[5]>Keys*(Empty listor Set)127.0.0.1:6379[5]>Flushallok

Flushall will erase data from all databases and use them with caution when working.

 0.1 :  6379  [ 5    2  ok  Span style= "color: #800000; Font-weight:bold; " >127.0 . 0.1 :  6379  [ 2    >  keys *   or  set ) 

(ii) Redis notes--Publish & Subscribe, transactions, database operations

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.