Redis series----(ii) Data structure types in Redis

Source: Internet
Author: User
Tags arrays data structures naming convention readable redis semaphore set time
I. Preface

Redis is not a simple key-value data container, it can not be understood as static storage data, it is a dynamic interaction of the data structure of the server, could be used as a cache, high-performance k-v database and so on.

It supports many types of data structures, supporting not only string-type value, but also many complex types of data. The following is a simple list of the data types supported by Redis, followed by a detailed description of each data type and their common operations commands

1. Security binary String type: String type as the element value;

2.Lists Type: A collection of elements based on the insertion Order of string type, based on the linked list implementation, non-array type;

3.Sets type: No duplicate and unordered string type is a collection of elements;

4.Sorted set Type: No duplicate and ordered string type is a collection of elements;

5.Hashes type: A data structure that maps fields to value types, where fields and values are of type string;
two. Detailed Redis data Types

Before introducing the data type, let's look at the very important key form of the Redis K-V data structure. The key in Redis is binary security, which means that any binary sequence can be a key, such as the string "foo", the contents of a picture, and so on. Even empty strings are available.

a good key naming convention is as follows:

1.key not too long. The maximum capacity in Redis is 512M, but the actual key over 1024byte is not a good way to design. The key value is too large not only to waste memory space, but also in the request for key ' to find more time to compare;

2.key value should not be too short. This principle seems to be in conflict with the first one, but not in reality. Sometimes the variable length of the key makes the space increase relative to make the key more readable and secondary;

The 3.key naming can be subdivided with colons and dashes, making key more readable and flexible. For example: Aticle:1000:tags. Where the first digit represents the type, the second bit represents the ID of the object of that type, and the third digit represents the tag in the article with ID 1000;

common commands that Act on key:

Redis offers a number of very useful and effective commands that act on key in Redis and manage the space of key.

1.EXISTS

The exists is primarily to find out whether the specified key exists, or return (integer) 1 if present, otherwise return (integer) 0. The usage is as follows:

First, the utility REDIS-CLI command enters Redis's client command-line mode:

127.0.0.1:6379> SET mystring "MYSTRING0"
OK
127.0.0.1:6379> GET mystring
"Mystring0
" 127.0.0.1:6379> EXISTS mystring
(integer) 1
127.0.0.1:6379> EXISTS mystring1
(integer) 0

2.DEL

Del is primarily used to remove key and delete the value corresponding to key.

127.0.0.1:6379> EXISTS mystring
(integer) 1
127.0.0.1:6379> 
127.0.0.1:6379> DEL mystring
( Integer) 1
127.0.0.1:6379> EXISTS mystring
(integer) 0
127.0.0.1:6379> GET mystring
(nil)


3. EXPIRE

Expire sets the validity period of the key, and if it exceeds the set survival time, the key will be automatically invalidated and deleted. The opposite command, persist, makes the key permanent, and the command TTL that is associated with it looks at the remaining valid time of key.

127.0.0.1:6379> SET mystring1 "string1"
OK
127.0.0.1:6379> EXISTS mystring1
(integer) 1
127.0.0.1:6379> EXPIRE mystring1 10000
(integer) 1
127.0.0.1:6379> GET mystring1
"string1"
127.0.0.1:6379> TTL mystring1
(integer) 9976
127.0.0.1:6379> ttl mystring1
(integer) 9975
127.0.0.1:6379> ttl mystring1

127.0.0.1:6379> 
127.0.0.1:6379> SET mystring2 "string2"
OK
127.0.0.1:6379> EXPIRE mystring2 20
(integer) 1
127.0.0.1:6379> 
127.0.0.1:6379> GET mystring2
"string2"
127.0.0.1:6379> TTL mystring2
(integer) 6
127.0.0.1:6379> ttl mystring2
(integer) 5
127.0.0.1:6379> TTL Mystring2
(integer) 2
127.0.0.1:6379> TTL mystring2
(integer) 1
127.0.0.1:6379> GET Mystring2
(Nil)


data type of value in Redis

sting Type Let's take a look at the string type first. The string type is the simplest data type in Redis, and the string type is essentially a string mapped to another string, and it is also the base type of the complex type. String type value must not exceed 512M and can be used in many situations, for example: Cache page.

127.0.0.1:6379> SET Mystring7 String7
ok
127.0.0.1:6379> GET mystring7
"String7"
127.0.0.1:6379> SET Mystring8 "String8"
OK
127.0.0.1:6379> GET mystring8
"String8"

From the above command, you can see that the value returned by get is of type string and the value parameter in the SET command can contain either double quotation marks or not. The set and get commands are basic commands for setting and getting string types, and they can attach parameters, but the commands are not discussed here, and details of the use of commands are described in more detail later in this chapter.
If a value of type string is a numeric type, you can use the addition and subtraction operations, in which Redis parses its string type into an integer type when using arithmetic operations. Let's take a look at the following example:
127.0.0.1:6379> SET mystring12
OK
127.0.0.1:6379> INCR mystring12
(integer) 101
127.0.0.1:6379> GET mystring12
"101"
127.0.0.1:6379> incrby mystring12
(integer) 136
127.0.0.1:6379> GET mystring12
"136"
127.0.0.1:6379> decr mystring12
(integer) 135
127.0.0.1:6379> GET mystring12
"135"
127.0.0.1:6379> decrby mystring12
(integer) -65
127.0.0.1:6379> GET mystring12
"-65"

The string type is very simple and we'll describe it here. After the introduction of the command Time to supplement.
There are two common implementations of the lists type lists type: 1. List data structure implemented based on arrays linear array 2. The implementation of list data structures based on the linked list chain node implementations in each application is almost followed by the idea that such as: Java ArrayList and LinkedList, C language data structure of the linear linked list and chain list, but also in Redis follow this idea. But the lists in Redis uses the second algorithm. The implementation of LinkedList determines that it has high-performance insert and delete operations, while the arrays-type list performance lies in query acquisition. Because performance is more important than caching or database systems for operations that are plugged in or updated, the performance loss of the query is lower than the query operation. Balancing performance is indeed more advantageous with linked list implementations. The base elements of a composite type in Redis are all string types. Lists is no exception. The lists type is essentially a sequence of string elements, and Redis provides many rich commands for manipulating the lists type. 1.LPUSH, Rpush, and Lpop, Rpop, and Lrange lpush insert elements from the left of the list, and Rpush insert elements from the right side of the list. Lpop pops the element from the left of the list and removes the element from the list, rpop the element from the right of the list and removes it. This element of manipulation is similar to the implementation of a two-way queue. So you can use the list type as a queue and make it available for thread interaction. Lrange is the range element in the Get list. Next, let's look at how these commands work:
127.0.0.1:6379> Lpush mylist0 name age address Phoneno
(integer) 4
127.0.0.1:6379> rpush mylist1 name age AD Dress Phoneno
(integer) 4
127.0.0.1:6379> lrange mylist0 0-1
1) "Phoneno"
2) "Address"
3) "Age"
4) "Name"
127.0.0.1:6379> lrange mylist1 0-1
1) "Name"
2) "Age"
3) "Address"
4) "Phoneno"

Where the second parameter in Lrange represents the starting bit to find the element, and the third element represents the ending bit (if negative, it starts counting from the tail of the list.) For example:-1 means the countdown first, 2 is the second-to-last digit, where the mylist0 and mylist1 elements are exactly the opposite.
127.0.0.1:6379> rpop mylist1
"Phoneno"
127.0.0.1:6379> rpop mylist1
"Address"
127.0.0.1:6379 > Rpop mylist1
"age"
127.0.0.1:6379> rpop mylist1
"name"
127.0.0.1:6379> rpop mylist1
( Nil

The Rpop command causes the Mylist1 to pop up new elements from the right side and remove them. Until the element is empty. Here's what to note:
127.0.0.1:6379> EXISTS mylist1
(integer) 0

The Mylist1 key was found to be absent. Redis offers great features: Automatically create or remove keys. This means that there is no need to pre-create a key that is empty, and to automatically create a key if the key is not present when the element is stored in the specified key. Similarly, you do not need to delete the key that is empty, and if the element is empty in a complex type, the key will be automatically deleted. This ensures a more efficient use of key space.
As mentioned in the previous list can be used for interactive communication between threads, here we simply say the list of the applicable scenario: 1.List Push and pop characteristics determine that it can be used as a queue; the 2.List feature 1 determines its memory function, can save the most recent operations, and quickly get; The 3.List feature 1 determines that it can be used in Product/consumer mode (producer/consumer mode), where producers put elements, consumers acquire and consume; Blocking on ListRedis also provides a blocking list (blocked list) based on list, which is very similar to the blocking queue in Java. With the semaphore mechanism, the wait and notify, condition of the object in Java are very similar to the semaphore implementations. Normal list If the FETCH element is not returned to the client is null, but blocking on list if the Get element is empty, then does not return, at this time blocking the client request until there is a value to return, or can set the time-out period, more than the set time to return; The Redis Implementation command Brpop, Blpop reaches the blocked list, and if the list is empty, then the client is not returned to wait until a new element joins the list to return or arrive at the specified time-out.
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> brpop mylist 10000




If you use the Brpop command above, the client blocks the wait. The second parameter is the time-out, if 0 means waiting. Then open a REDIS-CLI command window and add a new element to the MyList:
127.0.0.1:6379> Lpush mylist lxy
(integer) 1

The waiting client gets notified and returns the new element:
127.0.0.1:6379> Brpop mylist 10000





1) "MyList"
2) "Lxy"
(218.69s)
Because Brpop can wait for multiple lists, returns the element that returns the list name at the same time. The third parameter is the waiting time.
There are some rules for blocking list:

A few things to note about Brpop:clients is served in the ordered Way:the first client that blocked waiting for a list, is served first if an element was pushed by some other client, and so forth. The return value is different compared to rpop:it is a two-element array since it also includes the name of the key, Beca Use Brpop and Blpop is able to block waiting for elements from multiple lists. If The timeout is reached, NULL is returned.

Sets Type The set type is a collection of unordered and non-repeating string elements. Redis also provides a number of operations for sets types, such as intersection, set, and complement across multiple sets, and test for the existence of a given element. The 1.SADD sadd command inserts a new element into the set primarily, overwriting if any, which can be followed by multiple parameters as an inserted element.

127.0.0.1:6379> sadd myset 0 0 1 2 3 4 5 6 6 6 7 8 8
(integer) 9
As you can see, there are a lot of repeating elements, but 9 elements are inserted straight in.
2.SISMEMBER Sismember As long as the specified element is present in set, returns 1 if present, otherwise 0
127.0.0.1:6379> sismember myset 0
(integer) 1
127.0.0.1:6379> sismember myset 9
(integer) 0
127.0.0.1:6379> Sismember myset 7
(integer) 1

3.SMEBERS Smembers gets all the elements in the set, and is unordered
127.0.0.1:6379> smembers myset
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
7) "6"
8) "7"
9) "8"

4.SINTER SINTER for the intersection of multiple sets there are also a lot of operation commands about set, but here we mainly discuss the data structure mainly, the command does not do a detailed explanation. In a later Redis series there will be a special section on common commands.

Sorted set Type The Sorted set type is an ordered set type, somewhat like a mixed-type data structure of hashes and set. The only feature with set is no repetition, and an orderly implementation is determined by an additional floating-point value called Score, which is somewhat similar to the hashes key. Sorted set collation, assuming there are two elements a, B 1. If A.score > B.score, then a > b; 2. If A.score = b.score,a > B, then a > b; 1.ZADD Zadd inserting elements into the sorted set

127.0.0.1:6379> zadd mysortset 1 "BACD"
(integer) 1
127.0.0.1:6379> zadd mysortset 2 "Abcd<span style=" Font-family:arial, Helvetica, Sans-serif; > "</span>
(integer) 0
127.0.0.1:6379> zadd mysortset 2" BCDE "
(integer) 1
1,2,2 in Zadd is the score of this element, sorted by score. 2.ZRANGE, Zrevrange Zrang gets the elements in the sorted set, Zrevrange gets the elements in the sorted set and the direction output.
127.0.0.1:6379> zrange mysortset 0-1
1) "BACD"
2) "ABCD"
3) "BCDE"
127.0.0.1:6379> zrevrange mysortset 0-1
1) "BCDE"
2) "ABCD"
3) "BACD"
127.0.0.1:6379>
The above elements can also carry parameters, output score
127.0.0.1:6379> zrange mysortset 0-1 withscores
1) "BACD"
2) "1"
3) "ABCD"
4) "2"
5) "BCDE"
6) "2"

Sorted set is not just above the function, it also provides the operation on the scope: 1.ZRANGBYSCORE zrangbyscore gets the output element according to score, the-inf parameter indicates that the element is less than the specified score
127.0.0.1:6379> zrangebyscore mysortset-inf 4 withscores
1) "BACD"
2) "1"
3) "ABCD"
4) "2"
5) " BCDE "
6)" 2 "
127.0.0.1:6379>

2.ZREMRANGBYSCORE Zremrangbyscore Gets the number of elements in the specified score range
127.0.0.1:6379> Zremrangebyscore Mysortset 2 6
(integer) 3
These are just simple commands for sorted set, which are described in detail in subsequent chapters for complex commands, and only the data types are discussed here.
Hashes Type

The hashes type in Redis resembles a mapped data structure for Map,key-value key-value pairs in Java. Redis provides a number of commands for manipulating hashes.

1.HMSET

Hmset is used to insert elements into the hashes, and the rules for inserting elements are as follows:

127.0.0.1:6379> hmset usr:1000 username lxy Birth 1991 verified 1
OK
127.0.0.1:6379>

2.HMGET, Hget

Both Hmge and Hget are used to get the elements in hashes, and the rules are as follows:

127.0.0.1:6379> hmget usr:1000 Username Birth verified
1) "Lxy"
2) "1991"
3) "1"
127.0.0.1:6379> <span style= "font-family:arial, Helvetica, Sans-serif;" >	</span>

127.0.0.1:6379> hget usr:1000 username Birth
(error) ERR wrong number of arguments for ' hget ' command
127.0.0.1 :6379> 
127.0.0.1:6379> hget usr:1000 username
"Lxy"
As you can see from the above usage, Hmget is getting the value of multiple keys in hashes at the same time, and Hget can only get one at a time.


if the article found errors in the place, I hope that the vast number of readers in time to communicate with me, help me correct the mistake, thank you for your support!

This article references:

An introduction to Redis data types and Abstractions:http://redis.io/topics/data-types-intro


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.