In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what are the common data types of Redis". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the common data types of Redis"?
All key (keys) of Redis are strings. When we talk about the basic data structure, we are talking about the data types for storing values, which mainly include five common data types, namely: String, List, Set, Zset, Hash.
Getting started with Redis-data types: details of 5 basic data types
String string
List list
Set collection
Hash hash
Zset ordered set
Brief introduction of Redis data structure
Detailed explanation of basic data structure
Reference article
Brief introduction of Redis data structure
There are many basic articles on Redis. For the types of basic data structures, I recommend that you take a look at the official website first, and then take a look at the following summary.
First of all, for redis, all key (keys) are strings. When we talk about the basic data structure, we are talking about the data types that store values, including five common data types, namely: String, List, Set, Zset, and Hash.
The String string can be a string, an integer or a floating point number to operate on the whole string or part of a string; a self-increment or self-subtraction operation on an integer or floating-point number; a linked list of List lists, each node on the linked list contains a string to perform push and pop operations on both ends of the linked list to read single or more elements; to find or delete elements according to the value Set set contains a collection of unordered sets of strings, including basic methods to see if there are additions, fetches, deletions, etc.; it also includes unordered hash tables containing key-value pairs for calculating intersection, union, difference and other Hash hashes, including methods for adding, getting, and deleting individual elements Zset ordered sets and hashes, which are used to store ordered mappings between string members and floating-point fractions of key-value pairs. The order of the elements is determined by the size of the score; the inclusion methods include adding, obtaining, and deleting individual elements, and obtaining a detailed description of the element's basic data structure according to the score range or member.
The content is actually relatively simple. I think the key point of understanding is how to use this structure and what can be done with it. So when I sort it out, I elaborate around the legend, command, execution and scene. @ pdai
String string
String is the most basic data type in redis, and one key corresponds to one value.
The String type is binary safe, meaning that the string of redis can contain any data. Such as numbers, strings, jpg images, or serialized objects.
Legend
The following figure shows an example of String type, where the key is hello and the value is world
Command u
Command description: use GET to get the value stored in the given key GET nameSET set the value stored in the given key SET name valueDEL delete the value stored in the given key DEL nameINCR add the key stored value plus 1INCR keyDECR subtract the key stored value 1DECR keyINCRBY the key stored value plus the integer INCRBY key amountDECRBY subtract the key stored value minus the integer DECRBY key amount
Command execution
127.0.0.1 world 6379 > set hello worldOK127.0.0.1:6379 > get hello "world" 127.0.0.1 Virtue 6379 > del hello (integer) 1127.0.0.1 world 6379 > get hello (nil) 127.0.0.1 world 6379 > get counter "2" 127.0.0.1 purl 6379 > incr counter (integer) 3127.0.1 0.1 purl 6379 > get counter "3" 127.0.1 0.1 purge 6379 > incrby counter 100 (integer) 103127.0.1 1purl 6379 > get Counter "103" 127.0.0.1 integer 6379 > decr counter (integer) 102127.0.1 purl 6379 > get counter "102"
Actual combat scene
Cache: classic usage scenarios: put common information, strings, pictures or videos in redis, redis as cache layer and mysql as persistence layer to reduce the read and write pressure of mysql.
Counter: redis is a single-threaded model, one command will not be executed until the next, and the data can be landed to other data sources in one step.
Session: common scheme spring session + redis to achieve session sharing
List list
The List in Redis is actually a linked list (Redis uses a double-ended linked list to implement List).
Using the List structure, we can easily implement the latest message queuing function (such as Sina Weibo's TimeLine). Another application of List is message queue, which can take advantage of the PUSH operation of List to store the task in List, and then the worker thread uses the POP operation to fetch the task for execution.
Legend
Command u
Command description use RPUSH to push the given value to the right end of the list RPUSH key valueLPUSH push the given value to the left end of the list LPUSH key valueRPOP pops a value from the right end of the list, and returns the pop-up value RPOP keyLPOP pops up a value from the left end of the list and returns the pop-up value LPOP keyLRANGE to get all values of the list in a given range LRANGE key 0-1LINDEX gets the elements in the list through the index. You can also use a negative subscript, with-1 for the last element of the list,-2 for the penultimate element of the list, and so on. LINEX key index
Tips for using lists
Lpush+lpop=Stack (stack)
Lpush+rpop=Queue (queue)
Lpush+ltrim=Capped Collection (finite set)
Lpush+brpop=Message Queue (message queuing)
Command execution
127.0.0.1 ll ls mem (integer) 5127.0.0.1 lrange mylist 0-11) "mem" 2) "ls" 3) "ll" 4) "2" 5) "1" 127.0.0.1mem 6379 > lindex mylist-1 "1" 127.0.0.16379 > lindex mylist 10 # index is not within the range of mylist (nil)
Actual combat scene
Weibo TimeLine: someone posts Weibo and joins the timeline with lpush to show new list information.
Message queue
Set collection
The Set of Redis is an unordered collection of type String. Collection members are unique, which means that there can be no duplicate data in the collection.
Collections in Redis are implemented through hash tables, so the complexity of adding, deleting and searching is O (1).
Legend
Command u
Command brief use SADD to add one or more members to a collection SADD key valueSCARD gets the number of members of the collection SCARD keySMEMBER returns all members of the collection SMEMBER key memberSISMEMBER determines whether the member element is a member SISMEMBER key member of the collection key
For other collection operations, please refer to https://www.runoob.com/redis/redis-sets.html here.
Command execution
Smember myset1) "xiaohao" 2) "hao1" 3) "hao" 127.0.0.1 sadd myset hao hao1 xiaohao hao 6379 > sismember myset hao (integer) 1
Actual combat scene
Tag, add tags to users, or users tag messages, so that people with the same or similar tags can recommend things or people to follow.
Like, or click on, collection, etc., can be put into set to achieve
Hash hash
Redis hash is a mapping table of field (field) and value (value) of type string, and hash is particularly suitable for storing objects.
Legend
Command u
Command brief use HSET add key value pair HSET hash-key sub-key1 value1HGET to get the value of the specified hash key HGET hash-key key1HGETALL gets all the key value pairs contained in the hash HGETALL hash-keyHDEL if the given key exists in the hash, remove the key HDEL hash-key sub-key1
Command execution
127.0.0.1 hset user name1 hao (integer) 1127.0.0.1 hset user email1 hao@163.com (integer) 1127.0.0.1 hset user email1 hao@163.com > hgetall user1) "name1" 2) "hao" 3) "email1" 4) "hao@163.com" 127.0.0.1 > hget user user (nil) 127.0.1 > hget user name1 "hao" 127.0.1 > hset user name2 xiaohao (integer) 1127.0.0.1 hset user email2 xiaohao@163.com (integer) 1127.0.0.1 hgetall user1) "name1" 2) "hao" 3) "email1" 4) "hao@163.com" 5) "name2" 6) "xiaohao" 7) "email2" 8) "xiaohao@163.com"
Actual combat scene
Cache: can be intuitive, save more space than string, and maintain cache information, such as user information, video information and so on.
Zset ordered set
Redis ordered collections, like collections, are collections of elements of type string, and duplicate members are not allowed. The difference is that each element is associated with a score of type double. Redis sorts the members of the collection from small to large by scores.
The members of an ordered set are unique, but the score can be repeated. The collection is implemented through a hash table, so the complexity of adding, deleting, and finding is all O (1).
Legend
Command u
Command description: use ZADD to add a member with a given score to an ordered set ZADD zset-key 178member1ZRANGE gets multiple elements ZRANGE zset-key 0-1 withccoresZREM from the ordered set based on the position of the element in the ordered set. If the given element member exists in the ordered set, remove the element ZREM zset-key member1.
For more commands, please refer to https://www.runoob.com/redis/redis-sorted-sets.html here
Command execution
127.0.0.1 xiaohao xiaohao (integer) 2127.0.0.1 xiaohao (integer) 2127.0.0.1 xiaohao > ZRANGE myscoreset 0-11) "xiaohao" 2) "hao" 127.0.0.1 xiaohao 6379 > ZSCORE myscoreset hao "100"
Actual combat scene
Ranking: collect classic usage scenarios in an orderly manner. For example, websites such as novel videos need to rank the novel videos uploaded by users, which can be ranked according to the number of users' followings, update time, word count and so on.
Reference article
Http://ddrv.cn/a/260579
Https://www.cnblogs.com/haoprogrammer/p/11065461.html
Https://www.pianshen.com/article/6479421770/
Https://www.runoob.com/redis/redis-sorted-sets.html
Knowledge system
Knowledge system
Related articles
First of all, we learn the conceptual basis of Redis to understand the scenarios in which it applies.
Getting started with Redis-Redis concepts and basics
Redis is a storage system that supports multiple data structures such as key-value. Can be used for caching, event publishing or subscribing, high-speed queuing and other scenarios. Support network, provide string, hash, list, queue, collection structure direct access, memory-based, persistent.
Secondly, these applicable scenarios are based on the data types supported by Redis, so we need to learn the data types it supports; at the same time, we also need to understand the underlying data structure in redis optimization, so we also need to understand the design and implementation of some underlying data structures.
Getting started with Redis-data types: details of 5 basic data types
All key (keys) of Redis are strings. When we talk about the basic data structure, we are talking about the data types for storing values, which mainly include five common data types, namely: String, List, Set, Zset, Hash.
Getting started with Redis-data types: details of 3 special types
In addition to the five basic data types mentioned above, Redis also has three special data types, namely HyperLogLogs (cardinality Statistics), Bitmaps (Bitmap) and geospatial (Geographic location)
Getting started with Redis-data types: Stream details
Redis5.0 also adds a data structure Stream, which draws lessons from the design of Kafka and is a new and powerful persistent message queue that supports multicast.
Redis Advanced-underlying data structure: detailed explanation of object Mechanism
In the previous article, we have described the detailed explanation of the five basic data types of Redis, namely, string, list, hash, set, zset, and the detailed explanation of Redis Stream structure in version 5.0. so how is the underlying layer of these basic types implemented? In fact, each kind of object in Redis is composed of object structure (redisObject) and corresponding encoded data structure. This paper mainly introduces the part of object structure (redisObject).
Redis Advanced-underlying data structure: detailed explanation of underlying data structure
The previous article is the first part of the underlying design: a detailed description of the object mechanism, this paper mainly introduces the underlying data structure.
Redis Advanced-underlying data structure: detailed explanation of the corresponding relationship between redis objects and coding (underlying structure)
After learning the underlying data structure, we can finally explain the relationship between redis objects and coding combined with the previous content.
In addition, you need to learn the core features supported by Redis, including persistence, messaging, transactions, high availability; high availability, master-slave, sentry, etc.; and high scalability, such as slicing mechanism.
Redis Advanced-persistence: a detailed explanation of RDB and AOF mechanisms
In order to prevent data loss and recover data when the service is restarted, Redis supports data persistence, which is mainly divided into two ways, namely RDB and AOF;. Of course, these two mixed modes will also be used in actual scenarios.
Redis advance-messaging: a detailed explanation of the publish and subscribe model
Redis publish subscription (pub/sub) is a mode of message communication: the sender (pub) sends the message and the subscriber (sub) receives the message.
Redis Advanced-event: a detailed explanation of Redis event Mechanism
Redis uses event-driven mechanism to deal with a large number of network IO. Instead of using mature open source solutions such as libevent or libev, it implements a very concise event-driven library, ae_event.
Redis Advanced-transaction: detailed explanation of Redis transaction
The essence of a Redis transaction is a collection of commands. Transactions support the execution of multiple commands at a time, and all commands in a transaction are serialized. During transaction execution, commands in the queue are serialized sequentially, and command requests submitted by other clients are not inserted into the transaction execution command sequence.
Redis Advanced-High availability: master-Slave replication details
We know that in order to avoid a single point of failure, that is, to ensure high availability, we need to provide cluster services in a redundant (replica) manner. On the other hand, Redis provides the master-slave library mode to ensure the consistency of data copies, and the master-slave libraries are separated from each other by reading and writing. This paper mainly describes the master-slave replication of Redis.
Redis Advanced-High availability: detailed explanation of Sentinel Mechanism (Redis Sentinel)
On the basis of the master-slave copy above, what if the note node fails? In the Redis master-slave cluster, the sentry mechanism is the key mechanism to realize the automatic switching of the master-slave database, and it effectively solves the problem of failover in the master-slave replication mode.
Redis Advanced-highly Extensible: detailed explanation of sharding Technology (Redis Cluster)
In the previous two articles, master-slave replication and sentinel mechanism ensure high availability. For read-write separation, although slave nodes expand the read concurrency capability of master-slave nodes, the write capacity and storage capacity cannot be expanded, so it can only be the upper limit of the capacity of master nodes. If you are faced with massive data, it is necessary to build a cluster between master (master node sharding) and absorb high availability (master-slave replication and sentry mechanism) capabilities, that is, each master sharding node also needs to have slave nodes, which is the embodiment of typical vertical expansion (cluster sharding technology) in distributed systems. Therefore, the corresponding design in Redis version 3.0 is Redis Cluster.
Finally, it is the specific practice and the problems encountered in practice and solutions: there are different characteristics in different versions, so we also need to understand the version; and performance optimization, large factory practice and so on.
Redis Advanced-caching issues: consistency, penetration, penetration, avalanche, pollution, etc.
One of the most commonly used scenarios in Redis is as a cache. This article mainly discusses what problems there may be in practice as a cache. Such as consistency, penetration, avalanche, pollution, etc.
Redis Advanced-release Features: Redis4.0, 5.0,6.0 feature collation
When learning the Redis knowledge system, we will inevitably need to look at the differences between version implementations. This article mainly collates the features of the newer versions of Redis.
Redis Advanced-Operation and maintenance Monitoring: detailed explanation of Redis Monitoring
The actual combat of Redis includes development, cluster and operation and maintenance. Whether Redis is used well or not, and how to make it better, is what operation and maintenance should do. This article mainly helps you build your understanding of redis operation and maintenance / monitoring system in terms of Redis's own status and commands, visual monitoring tools, and Redis monitoring system, which is a prerequisite for performance optimization.
Redis Advanced-performance tuning: detailed description of Redis performance tuning
The performance issues of Redis involve a wide range of knowledge points, covering almost all aspects of CPU, memory, network, and even disks; at the same time, you also need to have a detailed understanding of some of the basics or underlying layers mentioned above. For Redis performance tuning, here is an article about droplets and silver bullets (official account), which can help you build a knowledge system of Redis performance tuning.
Redis experience-Weibo: Redis's 9-year optimization process in Weibo with trillions of daily visitors
Also share an article about Weibo's experience in using redis, because Redis is distributed in various application scenarios within Weibo, such as the "Red packet Flying" activities that must be fought for in the Spring Festival Gala, as well as fans, users, readings, recomments, comments, building buildings, advertising recommendations, negative feedback, music lists and so on, which are all useful to Redis;. We can strengthen our understanding of the use of redis through the experience of big companies using redis.
At this point, I believe you have a deeper understanding of "what are the common data types of Redis?" 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.