Home >Database >Redis >Let's talk in depth about the 5 basic data types in Redis

Let's talk in depth about the 5 basic data types in Redis

青灯夜游
青灯夜游forward
2021-11-23 18:56:552378browse

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!

Let's talk in depth about the 5 basic data types in Redis

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]

Lets talk in depth about the 5 basic data types in Redis

##Structure typeThe value stored in the structure Structure reading and writing capabilitiescan be a string or an integer or floating point number Operate on the entire string or a part of the string; perform increment or decrement operation on integer or floating point number; 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; 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.Unordered hash table containing key-value pairsContains methods for adding, getting, and deleting single elementsSame 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
String string
List List
Set collection
Hash hash
Zset ordered set

Detailed explanation of basic data structure

## String

String 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
CommandGETSETDELINCRDECRINCRBYDECRBY
  • Command execution
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> del hello
(integer) 1
127.0.0.1:6379> get hello
(nil)
127.0.0.1:6379> get counter
"2"
127.0.0.1:6379> incr counter
(integer) 3
127.0.0.1:6379> get counter
"3"
127.0.0.1:6379> incrby counter 100
(integer) 103
127.0.0.1:6379> get counter
"103"
127.0.0.1:6379> decr counter
(integer) 102
127.0.0.1:6379> get counter
"102"
  • Actual scenario
    • Cache: Classic usage scenario, common information , strings, pictures or videos and other information are placed in redis. Redis serves as the cache layer and mysql serves as the 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 one is executed. At the same time, the data can be transferred to other data sources in one step.
    • session: A common solution is spring session redis to implement session sharing,

List list

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 the message queue. You can use the PUSH operation of List to store tasks in the List, and then the worker thread uses the POP operation to take out the tasks for execution.

  • 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
##CommandBrief descriptionUse##HSETHGETHGETALLHDEL
  • 命令执行
127.0.0.1:6379> hset user name1 ycf
(integer) 1
127.0.0.1:6379> hset user email1 ycf@163.com
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "ycf"
3) "email1"
4) "ycf@163.com"
127.0.0.1:6379> hget user user
(nil)
127.0.0.1:6379> hget user name1
"ycf"
127.0.0.1:6379> hset user name2 xiaoycf
(integer) 1
127.0.0.1:6379> hset user email2 xiaoycf@163.com
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "ycf"
3) "email1"
4) "ycf@163.com"
5) "name2"
6) "xiaoycf"
7) "email2"
8) "xiaoycf@163.com"
  • 实战场景
    • 缓存: 能直观,相比string更节省空间,的维护缓存信息,如用户信息,视频信息等。

Zset有序集合

Redis 有序集合和集合一样也是 string 类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个 double 类型的分数。redis 正是通过分数来为集合中的成员进行从小到大的排序。

有序集合的成员是唯一的,但分数(score)却可以重复。集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。

  • 命令使用
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!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete