Reproduced Redis System Introduction

Source: Internet
Author: User
Tags redis tutorial

Reprinted from Http://blog.nosqlfan.com/html/3139.html?ref=rediszt

Although Redis is already very hot, I believe there are many students to Redis just heard or understand not comprehensive, below is a comparative system of Redis Introduction, the characteristics of Redis and various data types and operations are introduced. is a very good start of Redis tutorial.

1. Introduction to what 1.1 Redis is

REmote DIctionary Server (Redis) is a Sanfilippo storage system written by Salvatore Key-value. Redis offers a number of rich data structures, including lists, sets, ordered sets and hashes, and, of course, memcached structures like strings. Redis, of course, also includes rich operations on these data structures.

Advantages of 1.2 Redis
    • Very high performance –redis can support more than 100k+ per second read and write frequency.
    • Rich data types –redis support binary case Strings, Lists, hashes, sets and Ordered sets data type operations.
    • All atomic –redis operations are atomic, and Redis supports atomic execution of several operations.
    • Rich features –redis also supports publish/subscribe, notifications, key expiration, and more.
2. Data type 2.1 String type

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

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 bulk read and write operations

Redis 127.0.0.1:6379> MSET Age-Sex "male" Okredis 127.0.0.1:6379> MGET Age sex1) "2" "Male"

String types can also be used to store numbers and support the 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> GE T-age ' 127.0.0.1:6379> ' Redis decr Age (integer) 34redis 127.0.0.1:6379> Decrby age 4 (integer) 30redis 127.0.0.1:63 79> GET Age "30"

The string type also supports modification and fetch operations on 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 is able to store data as a linked list and can do a rich operation on that list.

Redis 127.0.0.1:6379> lpush students "John Doe" (integer) 1redis 127.0.0.1:6379> lpush students "Captain Kirk" (Integ ER) 2redis 127.0.0.1:6379> lpush students "Sheldon Cooper" (integer) 3redis 127.0.0.1:6379> llen Students (integer) 3  Redis 127.0.0.1:6379> lrange students 0) "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 Stud Ents (integer) 1redis 127.0.0.1:6379> lrange students 0) "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) "Dexter Morgan" 2) "Captain Kirk" 3) "John Doe" Redis 127.0.0.1:6379> lpush students "Peter Parker" (intege R) 4redis 127.0.0.1:6379> lrange students 0) "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 Stud Ents 0) "Dexter Morgan" 2) "Captain Kirk" 3) "John Doe" Redis 127.0.0.1:6379> lrem students 1 "John Doe" (integer) 1redi S 127.0.0.1:6379> llen Students (integer) 1redis 127.0.0.1:6379> lrange students 0 each) "Captain Kirk"
2.3 Set (sets) type

Redis is able to store a series of distinct values as a collection

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:6  379> 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 also supports the corresponding modification operation

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"

Redis also supports sub-orthogonal-complement operations on collections

Redis 127.0.0.1:6379> SINTER birds mammals1) "bat" Redis 127.0.0.1:6379> sunion birds mammals1) "Crow" 2) "bat" 3) "Hum An "4") "Pigeon" 5) "dog" Redis 127.0.0.1:6379> sdiff birds mammals1) "Crow" 2) "Pigeon"
2.4 Ordered set (Sorted sets) type

Sorted sets and sets structures are similar, the difference is that there is a sets attribute in the data that exists in the Sorted score, and it is sorted by this score at the time of writing.

Redis 127.0.0.1:6379> zadd days 0 mon (integers) 1redis 127.0.0.1:6379> zadd days 1 Tue (integer) 1redis 127.0.0.1:6379 > Zadd days 2 Wed (integers) 1redis 127.0.0.1:6379> zadd days 3 Thu (integers) 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 (integers) 7redis 127.0.0.1:6379> zrange days 0) "Mon" 2) "Tue" 3) "Wed" 4) "Thu" 5) "Fri" 6) "s At "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:6 379> Zrangebyscore days 3) "Thu" 2) "Fri" 3) "sat" 4) "Sun"
2.5 Hash Type

Redis is able to store key data for multiple properties (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) "5" "Sex" 6) "Male" Redis 127.0.0.1:6379&G T Hdel Student Sex (integer) 1redis 127.0.0.1:6379> hgetall student1) "name" 2) "Ganesh" 3) "age" 4) "30"

Hash data structures can be modified and retrieved 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 such a feature that you can push data into an information pipeline, and others can subscribe to these pipelines to get the information that is pushed over.

3.1 Subscription information pipeline

Using a client subscription 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 pipeline

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 get the push information

Redis 127.0.0.1:6379> SUBSCRIBE channelonereading messages ... (Press Ctrl-c to quit) 1) "Subscribe" 2) "Channelone" 3) (integer) one) "message" 2) "Channelone" 3) "Hello" 1) "message" 2) "Chan Nelone "3)" World "
3.2 Bulk Subscription by a certain mode

Subscribe to the information channel at the beginning of all channel with the following command

Redis 127.0.0.1:6379> psubscribe channel*reading messages ... (Press Ctrl-c to quit) 1) "Psubscribe" 2) "channel*" 3) (integer) 1

Two push messages on another client

Redis 127.0.0.1:6379> PUBLISH channelone Hello (integer) 1redis 127.0.0.1:6379> PUBLISH channeltwo World (integer) 1

You can then receive the push message 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) one) "Pmessage" 2) "channel*" 3) "Channelone" 4) "Hello" 1) "PME Ssage "2)" channel* "3)" Channeltwo "4)" World "
4. Data Expiration settings

Redis supports setting the expiration time by key, and the value will be deleted after expiration (the client appears to have been deleted)

Use the TTL command to get the expiration time of a key value (-1 means 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 see if 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> EXP IRE name 5 (integer) 1

Check back in 5 seconds.

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

This value has been lost.

You can also set the expiration at a certain point in time after the number of seconds that are directly set, the following example is set to expire 2011-09-24 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 a number of simple combinations of commands, such as the NX-end command, which determines that a command is performed when the value is not available.

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 that combine several commands with multi and exec to perform

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) (integer) (integer) 3redis 127.0.0.1:6379> GET counter "3"

You can also use the Dicard command to break command sequences 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 of Redis's data is stored in memory, but he also provides persistence for that data.

6.1 Data Snapshots

The principle of a data snapshot is to traverse through all the data stored in the entire Redis into a data file with an RDB extension. This procedure can be called by the Save command.

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 Co Oper "Okredis 127.0.0.1:6379> bgsavebackground Saving started

If you are using the brew on Mac OSX secure Redis, then 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 additional operation logging, called append only file, whose log file is aof ending, we generally aof files. To turn on logging for the AOF log, you need to set the following in the configuration file:

AppendOnly Yes

All of your operations will be logged 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. Management commands

Redis supports multiple db, the default is 16, you can set the data in which DB, the data between different db is isolated. You can also move data between multiple db.

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) 1 Redis 127.0.0.1:6379> SELECT 1OKredis 127.0.0.1:6379[1]> GET name "John Doe"

Redis can also do some of the following to get some operational information

Redis 127.0.0.1:6379[1]> dbsize (integer) 1redis 127.0.0.1:6379[1]> inforedis_version:2.2.13redis_git_sha1:0 0000000redis_git_dirty:0arch_bits:64multiplexing_api:kqueue ...

Redis also supports purging of a DB data (of course, the operation of emptying all data is also supported)

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 1 Okredis 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 client is very rich, almost all popular language has its client, here will not repeat, interested students can go to the Redis clients page to find.

9. Data references
    • Redis Documentation
    • Simon Willison–redis Tutorial
    • Michael J. Russo–redis from ground up
10. Summary

Reproduced 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.