Data Structures in Redis

1. Overview of Redis


Today, Internet projects are increasingly favoring Redis.

Redis is an in-memory database. Because it is memory-oriented, its read and write performance is much better than that of disk-oriented databases. This is also the main reason why Redis plays its role in high concurrency scenarios.


Redis

2. 5 data types

Redis is a key-value database. This is the key to understanding its data structure. Because any data stored in a Redis database exists in this form.

In Redis, Key is a simple String. There is no limit to the length of the key, but reducing the length of the key can effectively save memory and is more conducive to the efficiency of key retrieval.

In Redis, Value has 5 data types. In addition to the basic type String, there are four data types: List, Hash, Set, and Zset.

1、String

String is the most basic data type in Redis and the default data type in Redis.
The following are commonly used commands for String.

127.0.0.1> set key1 value1 //向Redis中新增一个键值对
OK
127.0.0.1> get key1 //获得该键指向的字符串的值
"value1"
127.0.0.1> strlen key1 //获得该键指向的字符串的长度
(integer) 6
127.0.0.1> getset key1 newvalue //将该键指向字符串设为新值,返回旧值
"value1"
127.0.0.1> get key1 //测试上面的命令是否成功
"newvalue"
127.0.0.1> getrange key1 3 7 //获取该键指向字符串的子串,范围为0~len
"value"
127.0.0.1> append key1 more //向该键指向字符串后添加新串,返回总长度
(integer) 12
127.0.0.1> get key1  //测试上面命令是否成功
"newvaluemore"

Redis also provides simple operations on integers and floating-point numbers, the commands are as follows.

127.0.0.1> set key1 10 //向Redis中新增键值对
OK
127.0.0.1> incr key1 //对该键所指向的值,做加1运算
(integer) 11
127.0.0.1> incrby key1 10 //对该键所指向的值,加上指定的整数
(integer) 21
127.0.0.1> decr key1 //对该键所指向的值,做减1运算
(integer) 20
127.0.0.1> decrby key1 10 //对该键所指向的值,减去指定的整数
(integer) 10

It should be noted here that if the value value is a floating point number, the above four commands will fail. To manipulate floating point numbers, use the following commands.

127.0.0.1> set key1 1.1
OK
127.0.0.1> incrbyfloat key1 1.1 //这个命令对浮点数和整数都适用
"2.2"

It can be seen that the computing function that comes with Redis is relatively weak. However, Lua language support is provided in Redis, which is the most powerful part of Redis extension function.

2、List

List in Redis is a doubly linked list structure and an ordered linear structure. So its advantages and disadvantages are also very obvious, the advantages are:

  • Insertion and deletion are easy

And its disadvantages:

  • Lookup performance is weak

Because it is a doubly linked list, the commands of the List structure are divided into two types: left operation and right operation. The example is as follows.

127.0.0.1> lpush left_key node_1 node_2 node_3 //链表逆序加入节点
(integer) 3
127.0.0.1> lrange left_key 0 2 //查看链表中下标从0到2的节点值
1) "node_3"
2) "node_2"
3) "node_1"
127.0.0.1> rpush right_key node_1 node_2 node_3 //链表顺序加入节点
(integer) 3
127.0.0.1> lrange right_key 0 2 //查看链表中下标从0到2的节点值
1) "node_1"
2) "node_2"
3) "node_3"
127.0.0.1> lindex left_key 2 //读取指定链表中下标为2的节点,以左操作
"node_1"
127.0.0.1> llen left_key //返回指定链表的长度,以左操作
(integer) 3
127.0.0.1> lpop left_key //删除链表最左边的节点并返回
"node_3"
127.0.0.1> rpop left_key //删除链表最右边的节点并返回
"node_1"
127.0.0.1> lrange left_key 0 0 //测试上面两条命令是否生效
1) "node_2"
127.0.0.1> lpushx left_key node_1 //如果存在该链表,则在最左边
(integer) 2                       //添加一个节点,否则添加失败
127.0.0.1> rpushx left_key node_3 //如果存在该链表,则在最右边
(integer) 3                       //添加一个节点,否则添加失败
127.0.0.1> lrange left_key 0 2 //测试上面两条命令是否生效
1) "node_1"
2) "node_2"
3) "node_3"
127.0.0.1> lset left_key 0 newNode_1 //修改执行下标的节点值
OK
127.0.0.1> lrange left_key 0 2 //测试上面一条命令是否生效
1) "newNode_1"
2) "node_2"
3) "node_3"
127.0.0.1> ltrim left_key 1 1 //修剪链表,保留指定下标区间的节点
OK
127.0.0.1> lrange left_key 0 0 //测试上面一条命令是否生效
1) "node_2"

It should be noted that the operations on the linked list are not process-safe, and there may be situations where multiple clients operate the linked list at the same time. In order to ensure the security of linked list operations, Redis provides blocking commands , and executing blocking commands will lock the linked list, but these commands are not used much in actual development, so I won't introduce them here.

3、Set

set translated into Chinese is a collection. The set here has the same properties as the set in mathematics: unordered and non-repeatable.

Like list, each element in the collection is of String data type.

127.0.0.1:6379> sadd set1 val1 val2 val3 val4 val5 //给键为set1的
(integer) 5                                        //集合添加成员    
127.0.0.1:6379> sadd set2 val4 val5 val6 val7 val8 //同上
(integer) 5
127.0.0.1:6379> smembers set1 //返回该键指向集合的所有成员
1) "val4"                     //从取出的成员能明显看出来集合是无序的
2) "val1"
3) "val3"
4) "val2"
5) "val5"
127.0.0.1:6379> sdiff set1 set2 //求两个集合的差
1) "val1"
2) "val3"
3) "val2"
127.0.0.1:6379> sinter set1 set2 //求两个集合的交
1) "val4"
2) "val5"
127.0.0.1:6379> sunion set1 set2 //求两个集合的并
1) "val4"
2) "val8"
3) "val7"
4) "val3"
5) "val5"
6) "val1"
7) "val6"
8) "val2"
127.0.0.1:6379> sismember set1 val1 //判断val1是否为指定集合的成员
(integer) 1
127.0.0.1:6379> smove set1 set2 val1 //把val1从set1移到set2中
(integer) 1
127.0.0.1:6379> smembers set2 //测试上一条命令是否生效
1) "val4"
2) "val5"
3) "val7"
4) "val8"
5) "val1"
6) "val6"
127.0.0.1:6379> spop set1 //随机弹出一个集合成员
"val2"
127.0.0.1:6379> spop set1 //同上
"val4"
127.0.0.1:6379> spop set1 //同上
"val3"
127.0.0.1:6379> smembers set1 //测试上面三条命令是否生效
1) "val5"

4 、 Zset

zset is called an ordered set. Sorted sets are similar to sets, the only difference is that each element in the sorted set also has a score, and the members of the set can be sorted according to this score.

Here are some common commands for sorted sets.

127.0.0.1> zadd set 1 A 2 B 3 C 4 D 5 E //向有序集合中添加成员
(integer) 5
127.0.0.1> zcount set 2 4 //返回指定分数区间内的成员个数
(integer) 3
127.0.0.1> zincrby set 1 E //为指定成员增加指定分数值
"6"
127.0.0.1> zrange set 0 4 //返回指定下标区间内成员,分数从小到大排序
1) "A"
2) "B"
3) "C"
4) "D"
5) "E"
127.0.0.1> zrank set B //返回指定成员的排名,排名从0开始
(integer) 1
127.0.0.1> zadd set 2.5 X //添加一个分数为浮点数的成员
(integer) 1
127.0.0.1> zrange set 0 5 //测试上面一条命令是否生效
1) "A"
2) "B"
3) "X"
4) "C"
5) "D"
6) "E"

5、Hash

Hash in Redis is like map in Java. This data type is very useful for some scenarios: for example, a large amount of User information needs to be stored in Redis, and each User includes multiple attribute fields. In this case, it is quite convenient to use the Hash structure to store, the example is as follows.

127.0.0.1> hmset user_1 id 001 age 23 sex male //设置多个键值对
OK
127.0.0.1> hgetall user_1 //获取所有的键值对
1) "id"
2) "001"
3) "age"
4) "23"
5) "sex"
6) "male"
127.0.0.1> hexists user_1 id //是否存在id字段/键
(integer) 1
127.0.0.1> hincrby user_1 age 3 //age字段/键上加3
(integer) 26
127.0.0.1> hkeys user_1 //获得所有的键
1) "id"
2) "age"
3) "sex"
127.0.0.1> hvals user_1 //获得所有的值
1) "001"
2) "26"
3) "male"
127.0.0.1> hlen user_1 //返回该hash中键值对的数量
(integer) 3
127.0.0.1> hset user_1 name mary //向该hash中新增一个键值对
(integer) 1
127.0.0.1> hgetall user_1 //测试上面的命令是否生效
1) "id"
2) "001"
3) "age"
4) "26"
5) "sex"
6) "male"
7) "name"
8) "mary"

It can be seen that the key is user_1 at this time, and the value is a hash structure, and multiple key-value pairs are stored in the hash structure. That is to say, Redis needs to index the corresponding hash structure through the key of user_1, and then find the corresponding value through the key of the hash structure.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935608&siteId=291194637