Home > Database > Redis > body text

Redis learning basic data structure

coldplay.xixi
Release: 2020-12-04 15:44:02
forward
7914 people have browsed it

Redis usage tutorialThe column introduces its basic data structure

Redis learning basic data structure

Recommended (free): redis usage tutorial

Redis basic data structure

Redis has 5 basic data structures: String (string), list (list), set (set), hash (hash), zset (Ordered Collection)

String string

The string type is the simplest data structure of Redis value, similar to ArrayList (number list) in Java language, but in Redis String It is a dynamic string

String in Redis uses the method of pre-allocating redundant space

[Image upload failed...(image-724c60-1537973556456)]

set & get

>set keyname test
OK

>get keyname
test

//key如果存在就返回0
>setnx keyname test
0

>exists keyname

>del keyname
1

//批量设置
>mset key1 test1 key2 test2
OK

//批量获取
>mget key1 key2
1) test1
2) test2
Copy after login

key expiration

//设置5s后过期
>expire keyname 5

//setex是expire和set的复合写法
>setex keyname 5 test
OK

//5s后查询
>get keyname
NULL
Copy after login

count
ps: When the value is a number, you can use incr and incrby to count

>set num 10
OK

//incr默认加1
>incr num
11

//incrby后面要加上数字
>incrby num
ERR wrong number of arguments for 'incrby' command

//正确计数
>incrby num 5
16
Copy after login

list list

Let’s introduce another data structure of redis, list
Earlier we said that the string in redis is similar to the ArrayList in the java language, then the list in redis is similar to the LinkList (linked list). One special feature of the linked list is update And the new addition is very fast, but the index query is slow.

Why is it similar to a linklist? Because the redis list is not the same as a linklist. It is actually a form of quick list (quicklist). The list structure is as follows:

[Image upload failed. ..(image-625c1b-1537973556457)]

Here we would like to introduce the ziplist. What is the ziplist? In fact, it is a continuous memory space

As can be seen from the figure, the quick list is actually composed of a compressed list and bidirectional pointers, but we know that the linked list has two pointers, that is, prev and Next execution, this is a difference between quick list and linklist.

PS: Then when redis was designed, why was it changed to a two-way pointer? If, like a linked list, two pointers prev and next are used, traversal can also be achieved, but bidirectional pointers have an obvious advantage, that is, they occupy relatively less memory space.

Queue and stack

/* 队列:First in first out */

//加两个value
>rpush keynames key1 key2
2

//计算
>llen keynames
2

>lpop keynames
key1

>lpop keynames
key2

//rpush会自动过期的
>rpop keynames
NULL

/* 栈:First in last out */

//同样,加两个元素
>rpush keynames key1 key2
2

>rpop keynames
key2

>rpop keynames
key1
Copy after login

Dictionary hash

The dictionary of Redis is similar to the hashmap of the Java language. It is also an unordered two-dimensional structure, that is, the structure of an array plus a list. This is similar to redis dictionary and hashmap.

Then there are differences, such as rehash, dictionary refresh operation, hashmap is all hot hashing, when there are enough dictionaries, the performance is not very good, so redis is transformed and adopts gradual method , why is it called progressive? Because redis will not reload all, but save the old and new dictionaries, and then use scheduled tasks to move the data of the old hash to the new hash, and then recycle the hash memory space after the move.

Array of dictionary (hash) Add link structure:
[Image upload failed...(image-f5660f-1537973556457)]

>hset keynames key1 "test1"
1

>hset keynames key2 "test2"
1

//批量set
>hmset keynames key1 "test1" key2 "test2"
OK

//获取key1的值
>hget keynames key1
test1

//获取hash为keynames的长度
>hlen keynames
2

//获取全部
>hgetall keynames
1) key1
2) test1
3) key2
4) test2
Copy after login

Collection set

The set of redis and the hashset type in the java language are the same Kind of unordered unique .

>sadd keynames key1
1

//key1已经加过了,所以返回1
>sadd keynames key1 key2
1

>smembers keynames
1) key2
2) key1

//查询某个key是否存在,相当与contains
>sismember keynames key1
1

//相当于count
>scard keynames
2

//随意弹出key1
>spop keynames
key1
Copy after login

Ordered set zSet

Ordered set is more distinctive in redis. It is similar to the combination of SortedSet and HashMap. Its internal implementation is a data structure called a jump list. On the one hand, an ordered set is a set, so each element is unique. Then it can assign a score to each value, and then sort according to this score. The score is equivalent to a permission sorting identifier. ps: For this reason, ordered sets can be used to store fan information, value is the fan id, and score is the follow time

//9.0是score也就是权重
>zadd keyname 9.0 math
1

>zadd keyname 9.2 history
1

//顺序
>zrange keyname 0 -1
1) history
2) math

//逆序
>zrevrange keyname 0 -1
1) math
2) history

//相当于count()
>zcard keyname
2

获取指定key的score
>zscore keyname math
9
Copy after login

Jump list TODO

The above is the detailed content of Redis learning basic data structure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jianshu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!