Redis SYSTEM INTRODUCTION

Source: Internet
Author: User

Although redis is already very popular, I believe there are still many people who just heard about redis or do not fully understand it. Below is a more systematic redis introduction, the features and various data types and operations of redis are introduced. Is a very good redis getting started tutorial.

1. What is redis 1.1

ReMoteDiCtionarySErver (redis) is a key-value storage system written by Salvatore sanfilippo. Redis provides a variety of data structures, including lists, sets, ordered sets, and hashes. Of course, it also has the same strings structure as memcached. redis also includes a wealth of operations on these data structures.

1.2 advantages of redis
  • High Performance-redis supports read/write frequencies of over 100 K + per second.
  • Rich data types-redis supports binary case strings, lists, hashes, sets, and Ordered Sets data type operations.
  • All the operations of atomic redis are atomic, and redis also supports atomic execution after the sum of several operations.
  • Rich features-redis also supports features such as Publish/subscribe, notification, and key expiration.
2. Data Type 2.1 string type

Redis can store binary safe strings with a maximum length of 1 GB

redis 127.0.0.1:6379> SET name "John Doe"OKredis 127.0.0.1:6379> GET name"John Doe"

The string type also supports batch read/write operations.

redis 127.0.0.1:6379> MSET age 30 sex "male"OKredis 127.0.0.1:6379> MGET age sex1) "30"2) "male"

The string type can also be used to store numbers and supports addition and subtraction of numbers.

redis 127.0.0.1:6379> INCR age(integer) 31redis 127.0.0.1:6379> INCRBY age 4(integer) 35redis 127.0.0.1:6379> GET age"35"redis 127.0.0.1:6379> DECR age(integer) 34redis 127.0.0.1:6379> DECRBY age 4(integer) 30redis 127.0.0.1:6379> GET age"30"

The string type also supports modification and retrieval of its part.

redis 127.0.0.1:6379> APPEND name " Mr."(integer) 12redis 127.0.0.1:6379> GET name"John Doe Mr."redis 127.0.0.1:6379> STRLEN name(integer) 12redis 127.0.0.1:6379> SUBSTR name 0 3"John"
2.2 list type

Redis can store data into a linked list and perform a variety of operations on the linked list.

redis 127.0.0.1:6379> LPUSH students "John Doe"(integer) 1redis 127.0.0.1:6379> LPUSH students "Captain Kirk"(integer) 2redis 127.0.0.1:6379> LPUSH students "Sheldon Cooper"(integer) 3redis 127.0.0.1:6379> LLEN students(integer) 3redis 127.0.0.1:6379> LRANGE students 0 21) "Sheldon Cooper"2) "Captain Kirk"3) "John Doe"redis 127.0.0.1:6379> LPOP students"Sheldon Cooper"redis 127.0.0.1:6379> LLEN students(integer) 2redis 127.0.0.1:6379> LRANGE students 0 11) "Captain Kirk"2) "John Doe"redis 127.0.0.1:6379> LREM students 1 "John Doe"(integer) 1redis 127.0.0.1:6379> LLEN students(integer) 1redis 127.0.0.1:6379> LRANGE students 0 01) "Captain Kirk"

Redis also supports many modification operations

redis 127.0.0.1:6379> LINSERT students BEFORE "Captain Kirk" "Dexter Morgan"(integer) 3redis 127.0.0.1:6379> LRANGE students 0 21) "Dexter Morgan"2) "Captain Kirk"3) "John Doe"redis 127.0.0.1:6379> LPUSH students "Peter Parker"(integer) 4redis 127.0.0.1:6379> LRANGE students 0 31) "Peter Parker"2) "Dexter Morgan"3) "Captain Kirk"4) "John Doe"redis 127.0.0.1:6379> LTRIM students 1 3OKredis 127.0.0.1:6379> LLEN students(integer) 3redis 127.0.0.1:6379> LRANGE students 0 21) "Dexter Morgan"2) "Captain Kirk"3) "John Doe"redis 127.0.0.1:6379> LREM students 1 "John Doe"(integer) 1redis 127.0.0.1:6379> LLEN students(integer) 1redis 127.0.0.1:6379> LRANGE students 0 11) "Captain Kirk"
2.3 set type

Redis can store a series of non-repeated values into a set.

redis 127.0.0.1:6379> SADD birds crow(integer) 1redis 127.0.0.1:6379> SADD birds pigeon(integer) 1redis 127.0.0.1:6379> SADD birds bat(integer) 1redis 127.0.0.1:6379> SADD mammals dog(integer) 1redis 127.0.0.1:6379> SADD mammals cat(integer) 1redis 127.0.0.1:6379> SADD mammals bat(integer) 1redis 127.0.0.1:6379> SMEMBERS birds1) "bat"2) "crow"3) "pigeon"redis 127.0.0.1:6379> SMEMBERS mammals1) "bat"2) "cat"3) "dog"

The sets structure can also be modified.

redis 127.0.0.1:6379> SREM mammals cat(integer) 1redis 127.0.0.1:6379> SMEMBERS mammals1) "bat"2) "dog"redis 127.0.0.1:6379> SADD mammals human(integer) 1redis 127.0.0.1:6379> SMEMBERS mammals1) "bat"2) "human"3) "dog"

Apsaradb for redis also supports subtransaction and population of collections.

redis 127.0.0.1:6379> SINTER birds mammals1) "bat"redis 127.0.0.1:6379> SUNION birds mammals1) "crow"2) "bat"3) "human"4) "pigeon"5) "dog"redis 127.0.0.1:6379> SDIFF birds mammals1) "crow"2) "pigeon"
2.4 sorted Sets

Sorted sets and sets have similar structures. The difference is that data in sorted sets has a score attribute, Which is sorted according to the score at the time of writing.

redis 127.0.0.1:6379> ZADD days 0 mon(integer) 1redis 127.0.0.1:6379> ZADD days 1 tue(integer) 1redis 127.0.0.1:6379> ZADD days 2 wed(integer) 1redis 127.0.0.1:6379> ZADD days 3 thu(integer) 1redis 127.0.0.1:6379> ZADD days 4 fri(integer) 1redis 127.0.0.1:6379> ZADD days 5 sat(integer) 1redis 127.0.0.1:6379> ZADD days 6 sun(integer) 1redis 127.0.0.1:6379> ZCARD days(integer) 7redis 127.0.0.1:6379> ZRANGE days 0 61) "mon"2) "tue"3) "wed"4) "thu"5) "fri"6) "sat"7) "sun"redis 127.0.0.1:6379> ZSCORE days sat"5"redis 127.0.0.1:6379> ZCOUNT days 3 6(integer) 4redis 127.0.0.1:6379> ZRANGEBYSCORE days 3 61) "thu"2) "fri"3) "sat"4) "sun"
2.5 hash type

Redis can store key-to-multiple attribute data (such as user1.uname user1.passwd)

redis 127.0.0.1:6379> HKEYS student1) "name"2) "age"3) "sex"redis 127.0.0.1:6379> HVALS student1) "Ganesh"2) "30"3) "Male"redis 127.0.0.1:6379> HGETALL student1) "name"2) "Ganesh"3) "age"4) "30"5) "sex"6) "Male"redis 127.0.0.1:6379> HDEL student sex(integer) 1redis 127.0.0.1:6379> HGETALL student1) "name"2) "Ganesh"3) "age"4) "30"

The hash data structure can be modified and obtained in batches.

redis 127.0.0.1:6379> HMSET kid name Akshi age 2 sex FemaleOKredis 127.0.0.1:6379> HMGET kid name age sex1) "Akshi"2) "2"3) "Female"
3. Publish/Subscribe

Redis supports this feature. You can push data to an information pipeline, and others can subscribe to these pipelines to obtain the pushed information.

3.1 subscription information pipeline

Use a client to subscribe to a pipeline

redis 127.0.0.1:6379> SUBSCRIBE channeloneReading messages... (press Ctrl-C to quit)1) "subscribe"2) "channelone"3) (integer) 1

Another client pushes information to this MPs queue

redis 127.0.0.1:6379> PUBLISH channelone hello(integer) 1redis 127.0.0.1:6379> PUBLISH channelone world(integer) 1

Then the first client can obtain the push information.

redis 127.0.0.1:6379> SUBSCRIBE channeloneReading messages... (press Ctrl-C to quit)1) "subscribe"2) "channelone"3) (integer) 11) "message"2) "channelone"3) "hello"1) "message"2) "channelone"3) "world"
3.2 subscription in a certain mode

Use the following command to subscribe to information channels starting with all channels

redis 127.0.0.1:6379> PSUBSCRIBE channel*Reading messages... (press Ctrl-C to quit)1) "psubscribe"2) "channel*"3) (integer) 1

Push two messages to the other client

redis 127.0.0.1:6379> PUBLISH channelone hello(integer) 1redis 127.0.0.1:6379> PUBLISH channeltwo world(integer) 1

Then you can receive the push information on the first client.

redis 127.0.0.1:6379> PSUBSCRIBE channel*Reading messages... (press Ctrl-C to quit)1) "psubscribe"2) "channel*"3) (integer) 11) "pmessage"2) "channel*"3) "channelone"4) "hello"1) "pmessage"2) "channel*"3) "channeltwo"4) "world"
4. Data expiration settings

Redis supports setting the expiration time by key. After expiration, the value will be deleted (in the client's view, it is supplemented)

Use the TTL command to obtain the expiration time of a key value (-1 indicates that the key never expires)

redis 127.0.0.1:6379> SET name "John Doe"OKredis 127.0.0.1:6379> TTL name(integer) -1

The following command first uses the exists command to check whether the key value exists, and then sets the expiration time of 5 seconds.

redis 127.0.0.1:6379> SET name "John Doe"OKredis 127.0.0.1:6379> EXISTS name(integer) 1redis 127.0.0.1:6379> EXPIRE name 5(integer) 1

Check again in 5 seconds

redis 127.0.0.1:6379> EXISTS name(integer) 0redis 127.0.0.1:6379> GET name(nil)

This value is no longer available.

You can also set the expiration time at a certain time point. The following example sets the expiration time from 00:40:00.

redis 127.0.0.1:6379> SET name "John Doe"OKredis 127.0.0.1:6379> EXPIREAT name 1316805000(integer) 1redis 127.0.0.1:6379> EXISTS name(integer) 0
5. transactional

Redis itself supports some simple combined commands. For example, commands ending with NX are used to determine whether a command is executed at this value sometimes.

redis 127.0.0.1:6379> SET name "John Doe"OKredis 127.0.0.1:6379> SETNX name "Dexter Morgan"(integer) 0redis 127.0.0.1:6379> GET name"John Doe"
redis 127.0.0.1:6379> GETSET name "Dexter Morgan""John Doe"redis 127.0.0.1:6379> GET name"Dexter Morgan"

Of course, redis also supports custom command combinations. Through multi and exec, several commands can be combined for execution.

redis 127.0.0.1:6379> SET counter 0OKredis 127.0.0.1:6379> MULTIOKredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> INCR counterQUEUEDredis 127.0.0.1:6379> EXEC1) (integer) 12) (integer) 23) (integer) 3redis 127.0.0.1:6379> GET counter"3"

You can also use the dicard command to interrupt the command sequence in execution.

redis 127.0.0.1:6379> SET newcounter 0OKredis 127.0.0.1:6379> MULTIOKredis 127.0.0.1:6379> INCR newcounterQUEUEDredis 127.0.0.1:6379> INCR newcounterQUEUEDredis 127.0.0.1:6379> INCR newcounterQUEUEDredis 127.0.0.1:6379> DISCARDOKredis 127.0.0.1:6379> GET newcounter"0"
6. Persistence

All redis data is stored in the memory, but it also provides persistence for the data.

6.1 data Snapshot

The principle of data snapshot is to traverse all data stored in redis to a data file with the extension RDB. You can use the Save command to call this process.

redis 127.0.0.1:6379> SET name "John Doe"OKredis 127.0.0.1:6379> SAVEOKredis 127.0.0.1:6379> SET name "Sheldon Cooper"OKredis 127.0.0.1:6379> BGSAVEBackground saving started

If you are using redis safe on Mac OSX, the RDB file will have the following path

/usr/local/var/db/redis/dump.rdb
6.2 append-only file (append operation log records)

Redis also supports an append operation log record called append only file. The log file ends with aof, which is generally Aof. To enable the aof log record, you need to make the following settings in the configuration file:

appendonly yes

At this time, all your operations will be recorded in the aof log file.

redis 127.0.0.1:6379> GET name(nil)redis 127.0.0.1:6379> SET name "Ganesh Gunasegaran"OKredis 127.0.0.1:6379> EXIT→ cat /usr/local/var/db/redis/appendonly.aof*2$6SELECT$10*3$3SET$4name$18Ganesh Gunasegaran
7. Manage commands

Redis supports multiple databases. The default value is 16. You can set the database in which the data exists and the data between different databases is isolated. You can also move data between multiple databases.

redis 127.0.0.1:6379> SELECT 0OKredis 127.0.0.1:6379> SET name "John Doe"OKredis 127.0.0.1:6379> SELECT 1OKredis 127.0.0.1:6379[1]> GET name(nil)redis 127.0.0.1:6379[1]> SELECT 0OKredis 127.0.0.1:6379> MOVE name 1(integer) 1redis 127.0.0.1:6379> SELECT 1OKredis 127.0.0.1:6379[1]> GET name"John Doe"

Redis can also perform the following operations to obtain some running information:

redis 127.0.0.1:6379[1]> DBSIZE(integer) 1redis 127.0.0.1:6379[1]> INFOredis_version:2.2.13redis_git_sha1:00000000redis_git_dirty:0arch_bits:64multiplexing_api:kqueue......

Redis also supports clearing a database (of course, all data can be cleared)

redis 127.0.0.1:6379> SET name "John Doe"OKredis 127.0.0.1:6379> DBSIZE(integer) 1redis 127.0.0.1:6379> SELECT 1OKredis 127.0.0.1:6379[1]> SET name "Sheldon Cooper"OKredis 127.0.0.1:6379[1]> DBSIZE(integer) 1redis 127.0.0.1:6379[1]> SELECT 0OKredis 127.0.0.1:6379> FLUSHDBOKredis 127.0.0.1:6379> DBSIZE(integer) 0redis 127.0.0.1:6379> SELECT 1OKredis 127.0.0.1:6379[1]> DBSIZE(integer) 1redis 127.0.0.1:6379[1]> FLUSHALLOKredis 127.0.0.1:6379[1]> DBSIZE(integer) 0
8. Client

Redis clients are rich, and almost all popular languages have their clients. I will not go into details here. If you are interested, you can go to the redis clients page to find them.

Redis SYSTEM INTRODUCTION

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.