Redis---1. Data structure characteristics and operations

Redis

Redis: full nameRemote Fromctionary Sservice, remote dictionary service.

1. Redis basic data structure

All data structures of Redis use a unique string as the key, and then obtain different data structures by going to this key.

Basic data structures are divided into 5 types:

  • string: string
  • list: list
  • hash: hash
  • set: collection
  • Zset: ordered set

1.1 string

1.1.1 Concept

Redis strings have the concepts of len (length) and cap (capacity). len is the actual length of the string, and cap is the capacity of the string. When the cap size is less than 1m, it will double each time it increases. When it exceeds 1m, it will increase by 1m each time. The maximum cap size is 512m.

Redis strings use pre-allocated redundant space (cap > len) to avoid frequent allocation of memory.

1.1.2 Operation

String type operations
modify a single string KV, get, setcommand

# key-value的增删改查
>set key value #新增和修改
OK
>get key	#查询value
"value"
>exiist key #查询key
(integer) 1
>del key	#删除key
(integer) 1
>get key   #删除后查询不到
(nil)

Batch key-value pair setting acquisition
command mget,mset

>mset name1 11111 name2 22222 name3 33333
OK
>mget name1 name2 name3
"11111"
"22222"
"33333"

expire, key expiration time setting and setexextension setnxof set command

#key过期时间设置
>set name "test"
OK
>get name 
"test"
>expire name 5 # 单位为秒
#wait for 5s
>get name 
(nil)
#set扩展
>setex name 5 "test" # 在设置的同时设置过期时间
>setnx name "test" # 设置key,存在则设置失败,不存在则设置成功

The auto-increment command incr, incrby, is used when value is an integer. The range of self-increment is the maximum value and minimum value of signed long

>set name 5
OK
>get name
5
>incr name
>get name
6
>incrby name 5
>get name
11
>incrby name -5
>get name
6

bitmap: A string is composed of bytes, and 1 byte is composed of 8 bits, so a string is composed of multiple bits. This is the bitmap data structure.

1.2 list

1.2.1 Concept

Redis's list is equivalent to Java's LinkedList. It is a linked list rather than an array. The characteristic of a linked list is that addition and deletion are very fast O(1). But finding a specific location is slow O(n).

When the last element is popped out of the list, the data structure is automatically deleted and the memory is recycled.

The List data structure is often used as an asynchronous queue. The structure that needs to be deferred is serialized into a string and inserted into the redis list. Another thread polls the data from the list for processing.

1.2.2 Operation

rpushand Redis are all built with rpop. Based on and taking data in different directions. The number stored in the list first is taken, similar to a queue, with first-in, first-out characteristics. The data stored in the list later is taken first, similar to a stack, first-in-last-out.lpop
rpushrpoplpoprpoplpop

> rpush books Math English Golang
(integer) 3
> llen books
(integer) 3
> rpop books
"Golang"
> lpop books
"Math"

lindex, equivalent to the get(int index) operation in a linked list in Java, which requires traversing the linked list and then taking the first value.
ltrim, keep the ones within the range, remove the ones outside the range, and obtain a new linked list.
lrange, print the value within the interval

> rpush days Monday Tuesday Wednesday Thursday Friday Saturday Sunday 	#生成一个新的rpush
(integer) 7
> llen days
(integer) 7
> lindex days 2 	#获取下标为2的值
"Wednesday"
> lindex days 0		#下标是由0开始的
"Monday"
> llen days
(integer) 7
> lrange days 0 -1		#-1是倒数第一个数,-2是倒数第二个数
1) "Monday"
2) "Tuesday"
3) "Wednesday"
4) "Thursday"
5) "Friday"
6) "Saturday"
7) "Sunday"
> llen days		
(integer) 7
> ltrim days 1 -1 # 获取从第一个元素到最后一个元素的列表
OK
> llen days		# 已经将原先第0位的Monday去除
(integer) 6
> lrange days 0 -1		#新生成列表2
1) "Tuesday"
2) "Wednesday"
3) "Thursday"
4) "Friday"
5) "Saturday"
6) "Sunday"
> ltrim days 1 -2	# 获取新生成列表2从第一个元素到最后二个元素的列表就是从Wednesday到Saturday
OK
> llen days	
(integer) 4
> lrange days 0 -1 # 遍历列表,发现已经将原来的
1) "Wednesday"
2) "Thursday"
3) "Friday"
4) "Saturday"

A preliminary solution to the bottom layer of list.
When there are few elements in the list ziplist, it is used to compress and store the elements in a continuous memory. When there are many elements, it is used to combine them quicklistthrough linked lists and combine multiple elements. Bidirectional concatenation avoids the space waste and memory fragmentation problems of ordinary linked lists.ziplistziplist

1.3 hash (dictionary)

1.3.1 Concept

The dictionary in Redis is equivalent to the HashMap in Java.
The same points are: array combined with linked list, unordered.
Difference: The hash value in Redis can only store strings. Java's rehash operation is allin, rehash all at once, redis is a progressive rehash, in the progressive rehash, the old and new hashes will be retained in the rehash, and then the two will be queried during the query, and the content of the old hash will be retrieved later. Slowly synchronize to the new hash. When the last old hash is removed, the original data structure will be deleted.

The advantage of hash is that different fields of an object can be stored separately during storage. However, when storing strings, the entire object needs to be stored, and the cost of accessing complex objects is less than that of strings.

For a single string, the storage consumption of the hash structure will be higher than that of a single string.

1.3.2 Operation

hget, hset, hgetall, hmset,
hget: Get the value corresponding to a single key.
hset:Set a kv key-value pair.
hgetall: Get all values ​​under this key.
hmset:Set multiple kv key-value pairs.

> hset books java "first book"
1
> hset books golang "second book"
1
> hset books php "third book"
1
> hgetall books
1) "java"
2) "first book"
3) "golang"
4) "second book"
5) "php"
6) "third book"
> hlen book
0
> hget books java
"first book"
> hset books golang "second book" # 更新前后没有改变
0
> hget books golang
"second book"
> hmset books java "update first book" golang "update second book" php "update third book" # 根据key批量修改value
OK
> hgetall books  # 根据key批量获取value
1) "java"
2) "update first book"
3) "golang"
4) "update second book"
5) "php"
6) "update third book"

1.4 Set

1.4.1 Concept

A Redus set is equivalent to a HashSet in Java. The internal key-value pairs are unordered and unique, that is, they are randomly deduplicated and all values ​​are null.

1.4.2 Operation

saddspopsmemberssismemberscard

sadd: Add value into the key collection.
spop: Get the value from the key collection.
smembers: Displays all elements in the collection.
sismember: Query whether there is an element in the collection.
scard: Counts the total number of elements in the collection.

> sadd member golang
(integer) 1
> sadd member golang
(integer) 0
> sadd member java php python
(integer) 3
> smembers member
1) "java"
2) "python"
3) "golang"
4) "php"
> sismember member java
(integer) 1
> sismember member javas
(integer) 0
> scard member
4
> spop member
"php"
> spop member
"golang"
> spop member
"java"
> spop member
"python"
> spop member
(nil)

1.5 ZSet (ordered list)

1.5.1 Concept

ZSet has two characteristics. One is that the internal value is unique, and the other is that each value has its own score, and sorting can be performed by this score. Similarly, when the values ​​are removed, the data structure will be deleted and the memory will be automatically recycled.

1.5.2 Operation

zadd:Add elements
zrange: Output according to sorting parameters
zrevrange: Output according to sorting parameters in reverse order
zcard: Count the number of kvs
zscore: Get the score of the specified kv
zrank: Get the ranking of the kv
zrankbyscore: Traverse the values ​​in the output score interval, and withscoresyou can output value and score by carrying parameters.
zrem:remove element

> zadd items 9.99 "math" # zadd添加元素
(integer) 1
> zadd items 21.11 "chinese"
(integer) 1
> zadd items 101.11 "MaxMoney"
(integer) 1
> zadd items 3033.11 "RMB"
(integer) 1
> zrange items 0 -1  # 遍历范围内的value,按score顺序输出
1) "math"
2) "chinese"
3) "MaxMoney"
4) "RMB"
> zrevrange items 0 -1  # 遍历范围内的value,按score逆序输出
1) "RMB"
2) "MaxMoney"
3) "chinese"
4) "math"
> zcard items # 计算k-v的数量
4
> zscore items "RMB" # 输出k-v对应的score
3033.11
> zrank items "RMB"  # 输出k-v对应的排序,从零开始,顺序
3
> zrank items "math"
0
> zrangebyscore items 0 100 # 遍历score在区间内的value,输出value
1) "math"
2) "chinese"
> zrangebyscore items 0 100 withscores # 遍历score在区间内的value,输出value和score
1) "math"
2) 9.99
3) "chinese"
4) 21.11
> zrem items math		# 移除元素
1
> zrangebyscore items 0 100 withscores # 移除元素后的结果展示,用于对比,展示zrem的作用
1) "chinese"
2) 21.11

1.6 Jump linked list

The skip connection list is the internal data structure of the ZSet list. Its basic data structure is an ordinary linked list that strings together all the elements.

Then select higher-level nodes among these basic linked list nodes, and then connect these higher-level nodes with a linked list. This way

In this way, some linked list nodes may play multiple roles, even if the nodes of the low-level linked list are also nodes of the high-level list. New section when added

The probability of a node becoming a multi-tasking node is random. The probability of the lowest layer, which is the layer where all elements are worn, is 1, and the probability of each upper level is *0.5.

1.7 General rules for container data structures

Container data structure:

  • list
  • set
  • check
  • hash

Rule 1: create if not exists
If the container does not exist, create a new data structure.

Rule 2: drop if no elements
If there are no elements in the container, delete the elements immediately and release the memory.

1.8 Expiration time

All data structures in Redis can have an expiration time. Once the time is up, the kv object will be automatically deleted instead of just deleting a value.

Learn from "Redis Deep Adventure: Core Principles and Application Practice"

Guess you like

Origin blog.csdn.net/Srwici/article/details/121078041