In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what are the common Redis data structures". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what are the common Redis data structures"?
What is a data structure?
Those who have basic Java should know some commonly used data structures, such as arrays, queues, stacks and so on. Is there any difference between the data structure of Redis and that of Java? The answer is yes, if some partners know about Map, they will know that there is a key value and a value value in Map, and the data structure of Redis is the same.
The data structure of Redis: String, List, Hash, Set all have a key value and a value value.
The Key value is the name, and the value value is the data. These two are bound together, a Key value corresponds to a value value, which we call a key-value pair.
Basic commands commonly used in Redis
In Redis, different commands are used in different scenarios. But not every command needs to be remembered? No, you just need to be familiar with the most commonly used commands. If you need to use some less-used commands, you can refer to the official documentation. Here are some common commands in Redis.
1.# exists (determine whether the key value exists): return 1 if it exists, otherwise return 0 example: exists name (name is key) 2.# del (delete key): delete successfully return 1, otherwise return 03.# type (determine the type of key) 4.ttl (check the survival time of key
If you want to learn more commands, you can query the Chinese documents by yourself: http://www.redis.cn/
Introduction and usage scenario of String type
String string type is the most basic data type in Redis, and it is widely used in Redis. A key corresponds to a value.
The String of Redis is a dynamic string, a modifiable string, and its internal structure is similar to that of Java's ArrayList. It uses pre-allocation of redundant space to reduce the allocation of memory.
The String type is binary safe, meaning that the String of Redis can contain any data. Such as numbers, strings, pictures, etc.
Application scenarios of String type: CAPTCHA, counter, repeated order submission, user login information, implementation of product details
The common command of String 1.# set/get setting and getting key-value Note must add a space example: set xxx (key) xxx (value) get xxx (key) 2.# mget/mset batch setting or getting multiple key values mset user:name jack user:age 2 mget user:name user:age 3.# incr incr adds 1 to the corresponding key And return the new value incr video:uv:1 4. # incrby add the number corresponding to key to increment. If key does not exist, before the operation, key will be set to 0 incrby video:uv:1 10 5. # setex to set the string value corresponding to key, and set key to time out after a given seconds time setex code 20 778899 (set a key to 778899 and expire in 20 seconds) 46. # setnx sets key to value, and does nothing if the key does not exist. If key does not exist, it is equivalent to the set command setnx name xdclass.net 7. # getset sets the value of key and returns the old value get name uuuuu of key (in this case, xdclass.net will be returned. If you get again, you will get the reset value uuuuu)
Note: the length of the value cannot exceed 512MB. Follow the naming convention of key: business name: indicate: ID (not too long, separated by colons)
The internal structure of String (internal structure for in-depth exploration)
Instead of using the traditional string representation of the C language, Redis builds an abstract type called simple dynamic string (SDS). Why would Redis use a self-built SDS instead of a C-language String? The reason is actually very simple, is to improve the performance of Redis operations.
What are the advantages of SDS strings versus C language strings?
Constant complexity gets the length of a string: if the C language wants to get the length, it has to traverse an entire string. The time complexity of SDS getting SDS string length through the len attribute has changed from O (N) to O (1), which ensures that getting string length will not become a performance bottleneck of Redis.
Reduce the number of memory reallocation caused by modification: memory reallocation means that when modifying a string, it is necessary to perform a memory reallocation operation due to insufficient or excessive memory space, which will involve memory. Therefore, the cost of time is high, so we should try our best to avoid memory reallocation. In SDS, there are two attributes, len and free, and optimization strategies can be used to reduce the number of redistributions of memory.
Introduction and usage scenario of List type
To put it bluntly, the List type is a linked list. After the elements are inserted, the values can be repeated, and the corresponding values can be obtained through the corresponding subscript. The left and right sides of the linked list can insert and delete data. At insert time, if the key does not exist, Redis creates a new linked list for the key. In contrast, if all elements in the linked list are deleted, the key will also be deleted.
Application scenarios: simple queue, latest comment list, non-real-time ranking list: timed calculation list, such as mobile phone daily sales list
List's common command 1. # lpush inserts one or more values into the list header lpush phone:rank:daily iphone6 2. # rpop removes and gets the last element of the list rpor phone:rank:daily 3. # llen gets the list length llen phone:rank:daily 4. # lrange gets the specified subscript range element of the list corresponding to key, where 0 represents the first element of the list -1 means to get all the elements of the list lrange phone:rank:daily 0-15. # rpush add an element rpush phone:rank:daily xiaodi 6. # lpop to the tail of the list corresponding to key. Delete an element from the tail of the List corresponding to key and return the element lpop phone:rank:daily 7. # bropo remove and get the last element of the list If there are no elements in the list, the list will block the list until the wait timeout or the popup element is found. Brpop phone:rank:daily 20 8. # lrem removes the internal structure of the element lrem phone:rank:daily 2 aList (the internal structure is in-depth)
Now that we've seen the use of List's common commands, let's take a look at the underlying structure of list.
There are two ways to implement List
Compressed linked list (ziplist): developed by Redis to save memory space, it is a sequential data structure composed of specially encoded contiguous blocks of memory. A compressed list can contain many arbitrary nodes, focusing on memory contiguity!
Double-ended linked list: using prev and next pointers, you can implement lpush and rpush instructions from front to back. Because it is a linked list, it also causes the lindex instruction to get an element that needs to traverse the linked list to get it. Time complexity O (n).
When the list object satisfies both of the following conditions, the list object is encoded with a compressed linked table:
(1) the length of all elements saved by the list object is less than 64 bytes
(2) the number of elements saved by list elements is less than 512.
After Redis3.2, the fast linked list-quicklist is used. Quicklist is a bi-directional linked list, and it is also a bi-directional linked list with ziplist characteristics, which means that every node of quicklist is ziplist. This quick linked list combines the advantages of both.
Introduction and usage scenario of Hash type
The Hash type in Redis is a mapping table of field and value of type String, Hash is particularly suitable for storing objects, and Hash is similar to the Map structure. Because in Redis, Hash is another key-value pair structure, and Redis itself is a key-value type, so this Hash structure is nested in the value under Redis.
Let's talk about the underlying data structure of Hash:
One is ziplist, which changes to the structure of Hashtable when the stored data exceeds the configured amount. This kind of conversion consumes performance, so use this conversion operation as little as possible.
The other is hashtable, which has a time complexity of O (1) but consumes more memory space.
For the application scenario of Hash in Redis: the realization of shopping cart, user's personal information and product details.
Hash's common command 1. # hset sets the value of the specified field in the hash set specified by key hset product:detail:1 title iphone11 2. # hget returns the value hget product:detail:1 title3 associated with this field in the hash set specified by key. # hgetall returns all fields and values in the hash set specified by key hegetall product:detail:1 4. # hdel removes the specified field hdel product:detail:1 title from the hash set specified by key. # hexists returns whether hexists product:detail:1 title exists in the field in hash (existence returns 1, no existence returns 0) 6. # hincrby increases the number of specified fields in the hash set specified by key If it is-1, it is decreasing hincrby product:detail:1 key 1 (increasing or decreasing the value in key) 7. # hmset sets the value of the specified field in the hash set specified by key hmset product:detail:2 title xiaomi price 1000 stock 10 8. # hmget returns the value hmget product:detail:2 title price of the specified field in the hash set specified by key
Note: each Hash can store 232-1 key-value pairs.
Introduction and usage scenario of Set type
The Set type in Redis is a collection, and the concept of a collection is a bunch of non-repeating combinations. Some collective data can be stored by using the Set data structure provided by Redis. Because Redis is very friendly for the collection to provide set, union, difference and other operations (PS: students who do not understand can ask the previous math teacher, oh), then it can be very convenient to achieve common concern, common preferences and other functions. For the above collection operations, you can also use different commands to choose whether the award results are returned to the client or saved to a new collection.
Set application scenarios in Redis: de-weight, social apps follow (fans, mutual friends), statistics site PV (UV, IP) big data user profile tag collection
Set's common command 1. # sadd adds one or more specified elements to the collection, and ignores sadd user:tags female sadd user:tags bmw 2. # scard to return the cardinality of the key stored in the collection (the number of collection elements) scard user:tags3 if the specified element is already in the collection key. The collection element returned by # sdiff is the difference between the first key collection and all subsequent key collections sdiff user:tags:1 user:tags:2 4. # sinter returns the intersection of all the members of the specified collection sinter user:tags:1 user:tags:2 5. # sismember returns whether members have stored members of the collection sismember user:tags:1 bmw 6. # srem removes the specified element from the collection If the specified element is not in the key collection, the introduction and usage scenario of srem user:tags:1 bmw 7. # sunion returns the union of a given set of all members of the sunion user:tags:1 user:tags:2SortedSet type is ignored
Compared with Set, SortedSet adds a weight parameter score to the elements in Set, so that the elements in the collection can be sorted according to score. For example, a SortedSet collection that stores the scores of students in the class can be stored. The collection value can be set as the student number of students, while score can be the test results. When the data is inserted in this way, the data is already sorted. In addition, SortedSet can also be used to make weighted queues.
Application scenarios: real-time ranking, priority tasks (queue), moments (articles) like-cancel
Introduction of Jump Table in SortedSet
A jump list is actually a linked list, and if you want to find an element in a traditional linked list, you have to start from the original place until you find the element. In the jump list, it will extract the linked list, the layer will be separated into several elements into a node, after the layer is also a linked list, the number of nodes will become fewer. As shown in the following figure, when looking for element 78, if it is an ordinary linked list, it will be queried from left to right one by one, and it will take eight queries to find the element. After using the jump table, starting from the beginning of the second layer, when the query reaches the node 79 and finds that there is no 78, then return to the node 57 and return to the first layer to find the element.
SortedSet's common command 1. # zadd to add one or more members to an ordered collection Or update the score of existing members zadd video:rank 90 springcloud zadd video:rank 80 springboot zadd video:rank 50 redis 2.# zcard to get the number of members of the ordered set zcard video:rank 3.# zcount calculate the number of members of the specified interval score in the ordered set zcount video:rank 0 60 4.# zincrby add incremental zincrby video:rank 2 springcloud 5 to the score of the specified member in the ordered set . # zrange returns the members within the specified ordered set through the index interval The member position score is sorted by (sort from smallest to largest) zrange voideo:rank 0-1 zrange voideo:rank 0-1 withscores (return score) 6 # zrevrange through the ranking of the specified members in the indexed collection, where the ordered set members increase by score value (sort from largest to smallest) zrevrange voideo:rank 0-1 7.# zrevrank returns the specified member ranking in the ordered collection Ordered set members decrease by fraction (sort from largest to smallest) zrevrank voideo:rank springcloud 8.# zrank returns the ranking of member member in ordered set key, where ordered set members increase by score value (sort from smallest to largest) zrank voideo:rank 9.# zrem removes one or more members of ordered set zrem voideo:rank redis 10.# zscore returns the member score value zscore voideo:rank springcloud in ordered set to this I believe that you have a deeper understanding of "what are the common Redis data structures", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.