[redis data structure] Quickly understand the five redis data structures

Posted by Skawn on Wed, 08 May 2019 18:48:04 +0200

redis is an advanced key:value storage system where value supports five data types:

1. strings
 2. lists of strings
 3. String sets
 4. Ordered string sets
 5. hashes

There are a few points to remind you about key:

1. Keys should not be too long, try not to exceed 1024 bytes, which not only consumes memory, but also reduces the efficiency of search;
2. Keys should not be too short, and key s will be less readable if they are too short.
3. In a project, key s are best named using a uniform naming pattern, such as user:10000:passwd.

[redis data structure - strings]

Some people say that redis is very much like memcache if you only use string types in redis and do not use the persistence capabilities of redis.This indicates that the strings type is a very basic data type and a necessary data type for any storage system.

Let's take a look at the simplest example:

The code is as follows:

set mystr "hello world!" //Set string type
get mystr //Read string type

String type usage is as simple as that, because it is binary safe, you can store the contents of a picture file as a string.
In addition, we can perform numeric operations with string types:

The code is as follows:

127.0.0.1:6379> set mynum "2"
OK
127.0.0.1:6379> get mynum
"2"
127.0.0.1:6379> incr mynum
(integer) 3
127.0.0.1:6379> get mynum
"3"

Look, redis converts a string type to a numeric value when it encounters a numeric operation.

Since INCR and other instructions have the characteristics of atomic operation, we can fully use redis'INCR, INCRBY, DECR, DECRBY and other instructions to achieve the effect of atomic count. If three clients read the value of mynum (value 2) at the same time in a certain scenario, and then add 1 to it at the same time, then the value of mynum must be 5 at the end.Many websites use this feature of redis to meet their business statistics requirements.

[redis data structure - lists]

Another important data structure for redis is called lists, which are translated into Chinese as "lists".

First, make it clear that lists in redis are not arrays but chains in the underlying implementation. That is, for a lists with millions of elements, inserting a new element at the head and tail is a constant level of time complexity, such as inserting a new element at the lists head of 10 elements with LPUSH and a new element at the lists head of tens of millions of elements with LPUSH.The speed should be the same.

Although lists have this advantage, they have the same disadvantage: the element positioning of the chain phenotype lists is slower, while the element positioning of the array lists is much faster.

Common operations for lists include LPUSH, RPUSH, LRANGE, and so on.We can use LPUSH to insert a new element to the left of lists, RPUSH to insert a new element to the right of lists, and the LRANGE command to specify a range from lists to extract elements.Let's look at a few examples:

//Create a new list called mylist and insert elements at the head of the list"1"
127.0.0.1:6379> lpush mylist "1" 
//Returns the number of elements in the current mylist
(integer) 1 
//Insert element "2" to the right of mylist
127.0.0.1:6379> rpush mylist "2" 
(integer) 2
//Insert element "0" to the left of mylist
127.0.0.1:6379> lpush mylist "0" 
(integer) 3
//List elements from number 0 to number 1 in mylist
127.0.0.1:6379> lrange mylist 0 1 
1) "0"
2) "1"
//List the first element from number 0 to the last in mylist
127.0.0.1:6379> lrange mylist 0 -1 
1) "0"
2) "1"
3) "2"

lists are widely used, for example:

1. We can use lists to implement a message queue, and we can ensure that the order is sequential, without the ORDER BY sort required like MySQL.
2. Using LRANGE, paging can also be easily implemented.
3. In the blog system, comments for each blog post can also be stored in a separate list.

[redis data structure-set]

A redis collection is an unordered collection in which the elements are not sequential.

Collection-related operations are also rich, such as adding new elements, deleting existing elements, taking intersections, taking unions, taking differences, and so on.Let's take an example:

//Add a new element to the collection myset"one"
127.0.0.1:6379> sadd myset "one" 
(integer) 1
127.0.0.1:6379> sadd myset "two"
(integer) 1
//List all elements in the collection myset
127.0.0.1:6379> smembers myset 
1) "one"
2) "two"
//Determines if element 1 is in the set myset and returns 1 for existence
127.0.0.1:6379> sismember myset "one" 
(integer) 1
//Determining whether element 3 is in the set myset returns 0 to indicate nonexistence
127.0.0.1:6379> sismember myset "three" 
(integer) 0
//New collection yourset
127.0.0.1:6379> sadd yourset "1" 
(integer) 1
127.0.0.1:6379> sadd yourset "2"
(integer) 1
127.0.0.1:6379> smembers yourset
1) "1"
2) "2"
//Summarize the union of two sets
127.0.0.1:6379> sunion myset yourset 
1) "1"
2) "one"
3) "2"
4) "two"

For the use of collections, there are also some common ways, such as, QQ has a social function called "Friend Tag". You can tag your friends, such as "Big Beauty", "Tuhao", "Oba", and so on. Then you can use the collection of redis to achieve, and store each user's tag in a collection.

[redis data structure - ordered set]
redis provides not only sets but also sorted sets, which are thoughtful.Each element in an ordered set is associated with a score, which is the basis for sorting.

Many times, we call ordered sets in redis zsets, because in redis, ordered set-related operation instructions start with z, such as zrange, zadd, zrevrange, zrangebyscore, and so on.

As a rule, let's look at some vivid examples:
//Add an ordered collection, myzset, and add an element, baidu.com, given a sequence number of 1:

127.0.0.1:6379> zadd myzset 1 baidu.com 
(integer) 1
//Add an element 360.com to myzset, giving it the number 3
127.0.0.1:6379> zadd myzset 3 360.com 
(integer) 1
//Add an element to myzset, google.com, given a number of 2
127.0.0.1:6379> zadd myzset 2 google.com 
(integer) 1
//By listing all the elements of myzset and their serial numbers, you can see that myzset is already in order.
127.0.0.1:6379> zrange myzset 0 -1 with scores 
1) "baidu.com"
2) "1"
3) "google.com"
4) "2"
5) "360.com"
6) "3"
//List only the elements of myzset
127.0.0.1:6379> zrange myzset 0 -1 
1) "baidu.com"
2) "google.com"
3) "360.com"

[redis data structure - hash]

Finally, we want to introduce hashes, or hashes.Hashes are data structures that have been around since redis-2.0.0.

hashes store mappings between strings and string values, such as a user's full name, last name, age, and so on, which makes them ideal for hashing.

Let's take an example:

//Establish a hash and assign values
127.0.0.1:6379> HMSET user:001 username antirez password P1pp0 age 34 
OK
//List the contents of the hash
127.0.0.1:6379> HGETALL user:001 
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "34"
//Change a value in the hash
127.0.0.1:6379> HSET user:001 password 12345 
(integer) 0
//List the contents of the hash again
127.0.0.1:6379> HGETALL user:001 
1) "username"
2) "antirez"
3) "password"
4) "12345"
5) "age"
6) "34"

Topics: Redis Google less MySQL