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!
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 |
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.
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 |
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)
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 | 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
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
Redis hash is a mapping table of string type fields and values. Hash is particularly suitable for storing objects.
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!