Let's talk in depth about the 5 basic data types in Redis
This article will give you a detailed understanding of the 5 basic data types in Redis (String string, List list, Set collection, Hash hash, Zset ordered collection), I hope it will be helpful to you!
Introduction to Redis data structure
For redis, all keys are strings. When we talk about basic data structures, we discuss the data types for storing values, which mainly include 5 common data types: String, List, Set, Zset, and Hash. [Related recommendations: Redis video tutorial]
The value stored in the structure | Structure reading and writing capabilities | |
---|---|---|
String string | can be a string or an integer or floating point numberOperate on the entire string or a part of the string; perform increment or decrement operation on integer or floating point number; | |
List List | A linked list, each node on the linked list contains a stringPerform push and pop operations on both ends of the linked list, and read single or multiple elements; according to Value finds or deletes elements; | |
Set collection | An unordered collection containing stringsA collection of strings, Basic methods include checking whether there is addition, acquisition, and deletion; it also includes calculation of intersection, union, difference, etc. | |
Hash hash | Unordered hash table containing key-value pairsContains methods for adding, getting, and deleting single elements | |
Zset ordered set | Same as hashing, used to store key-value pairsOrdered mapping between string members and floating point scores; the order of elements is determined by the size of the scores; the inclusion method has been added , obtain, delete a single element and obtain elements based on score range or member |
Detailed explanation of basic data structure
## StringString is the most basic data type in redis. One key corresponds to one value.
The String type is binary safe, which means that the string of redis can contain any data. Such as numbers, strings, jpg images or serialized objects.
- Command usage
Brief description | Get the value stored in a given key using | |
---|---|---|
GET name | ||
Set the value stored in the given key | SET name value | |
Delete The value stored in the given key | DEL name | |
Increase the value stored by the key by 1 | INCR key | |
Decrease the value stored in the key by 1 | DECR key | |
Add the integer to the value stored in the key | INCRBY key amount | |
Subtract the integer from the value stored in the key | DECRBY key amount |
Command | Brief description | Use |
---|---|---|
RPUSH | to push the given value to the right end of the list | RPUSH key value |
LPUSH | Push the given value to the left end of the list | LPUSH key value |
RPOP | Pops a value from the right end of the list and returns the popped value | RPOP key |
LPOP | Pops a value from the left end of the list and returns Return the popped value | LPOP key |
LRANGE | Get all values in the list in the given range | LRANGE key 0 -1 |
LINDEX | Get the element in the list by index. You can also use negative subscripts, with -1 representing the last element of the list, -2 representing 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 (limited collection)
- lpush brpop=Message Queue (message queue)
- Command execution
127.0.0.1:6379> lpush mylist 1 2 ll ls mem (integer) 5 127.0.0.1:6379> lrange mylist 0 -1 1) "mem" 2) "ls" 3) "ll" 4) "2" 5) "1" 127.0.0.1:6379> lindex mylist -1 "1" 127.0.0.1:6379> lindex mylist 10 # index不在 mylist 的区间范围内 (nil)
- Actual scenario
- Weibo TimeLine: Someone posts on Weibo, uses lpush to add to the timeline, and displays New list information.
- Message Queue
Set Collection
Redis’ Set is An unordered collection of type String. Set members are unique, which means that duplicate data cannot appear in the set.
Collections in Redis are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).
- Command usage
Command | Brief description | Add one or more members to the collection using |
---|---|---|
SADD | SADD key value | |
SCARD | Get the number of members in the set | SCARD key |
SMEMBER | Return all members in the set | SMEMBER key member |
SISMEMBER | Determine whether the member element is a member of the set key | SISMEMBER key member |
For other set operations, please refer here
https://www.runoob.com/redis/redis-sets.html
- Command execution
127.0.0.1:6379> sadd myset ycf ycf1 xiao ycf (integer) 3 127.0.0.1:6379> smember myset 1) "xiao" 2) "ycf1" 3) "ycf" 127.0.0.1:6379> sismember myset ycf (integer) 1
- Actual scenario
- Tag (tag), add to the user Tags, or users add tags to messages, so that those with the same tag or similar tags can recommend things or people to follow.
- Like, dislike, collect, etc. can be placed in the set to achieve
Hash hashing
Redis hash is a mapping table of string type fields and values. Hash is particularly suitable for storing objects.
- Command usage
Brief description | Use | |
---|---|---|
Add key-value pair | HSET hash-key sub-key1 value1 | |
Get the value of the specified hash key | HGET hash-key key1 | |
Get all key-value pairs contained in the hash | HGETALL hash-key | |
If the given key exists in the hash , then remove this key | HDEL hash-key sub-key1 |
命令 | 简述 | 使用 |
---|---|---|
ZADD | 将一个带有给定分值的成员添加到哦有序集合里面 | ZADD zset-key 178 member1 |
ZRANGE | 根据元素在有序集合中所处的位置,从有序集合中获取多个元素 | ZRANGE zset-key 0-1 withccores |
ZREM | 如果给定元素成员存在于有序集合中,那么就移除这个元素 | ZREM zset-key member1 |
更多命令请参考这里
https://www.runoob.com/redis/redis-sorted-sets.html
- 命令执行
127.0.0.1:6379> zadd myscoreset 100 ycf 90 xiaoycf (integer) 2 127.0.0.1:6379> ZRANGE myscoreset 0 -1 1) "xiaoycf" 2) "ycf" 127.0.0.1:6379> ZSCORE myscoreset ycf "100"
- 实战场景
- 排行榜:有序集合经典使用场景。例如小说视频等网站需要对用户上传的小说视频做排行榜,榜单可以按照用户关注数,更新时间,字数等打分,做排行。
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of Let's talk in depth about the 5 basic data types in Redis. For more information, please follow other related articles on the PHP Chinese website!

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools