Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic data structure of Redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article is about what the basic data structures of Redis are. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

Redis basic data structure

Redis has five basic data structures: String (string), list (list), set (collection), hash (hash), zset (ordered set)

String string

String type is the simplest data structure of Redis's value, similar to ArrayList (list of numbers) in Java, but String is a dynamic string in Redis

String in Redis adopts the method of pre-allocating redundant space.

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

Set & get

> set keyname testOK > get keynametest//key returns 0 > setnx keyname test0 > exists keyname > del keyname1// batch setting > mset key1 test1 key2 test2OK// batch acquisition > mget key1 key21) test12) test2

Key expires

/ / set to expire after 5s > expire keyname 5//setex is a combination of expire and set > query after setex keyname 5 testOK//5s > get keynameNULL

Count

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

> set num 10OK//incr default plus 1 > incr num11//incrby followed by a number > incrby numERR wrong number of arguments for 'incrby' command// correct count > incrby num 516 list list

Let's introduce another data structure of redis, list.

Before we said that string in redis is similar to ArrayList in java language, then the list in redis is similar to LinkList (linked list), the linked list is especially fast to update and add, and the query index is slow.

Why is it similar to linklist? Because redis's list is not the same as linklist, it is actually a form of quick list (quicklist), which is structured as shown in the figure:

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

Here's a little bit about the compressed list (ziplist). What is the compressed list? It's actually continuous memory space.

You can see from the figure that quick lists are actually made up of compressed lists and two-way pointers, but we know that linked lists have two pointers, that is, prev and next execution, which is a difference between quick lists and linklist.

PS: then when redis was designed, why did it change to a two-way pointer? If and linked list, with two pointers prev, next, you can also achieve traversal, but two-way pointers have an obvious advantage, that is, the memory space is relatively less.

Queues and stacks

/ * queue: First in first out * / / add two value > rpush keynameskey1 key22// calculation > llen keynames2 > lpop keynameskey1 > lpop keynameskey2//rpush will expire automatically > rpop keynamesNULL/* stack: First in last out * / / similarly, add two elements > rpush keynameskey1 key22 > rpop keynameskey2 > rpop keynameskey1 dictionary hash

The dictionary of Redis is similar to the hashmap of java language, which is also an unordered two-dimensional structure, that is, the structure of array plus list. This is where the redis dictionary is similar to hashmap.

Then there are differences, such as rehash, refresh dictionary operation, hashmap is all hot hash, when there are enough dictionaries, the performance is not very good, so redis is modified, using a gradual approach, why is it progressive? Because redis does not have all the reload, but saves the old and new dictionaries, and then uses scheduled tasks to move the data of the old hash to the new hash, and reclaims the hash memory space after moving.

The array plus link structure of the dictionary (hash):

[failed to upload picture... (image-f5660f-1537973556457)]

> hset keynames key1 "test1" 1 > hset keynames key2 "test2" 1 set > hmset keynames key1 "test1" key2 "test2" OK// gets the value of key1 > hget keynames key1test1// gets the length of hash as keynames > hlen keynames2// gets all > hgetall keynames1) key12) test13) key24) test2 collection set

The hashset type in redis's set and java languages is unordered and unique.

> sadd keynames key11//key1 has been added, so return 1 > sadd keynameskey1 key21 > smembers keynames1) key1// to query whether a key exists, which is equivalent to contains > sismember keynames key11// is equivalent to count > scard keynames2// popping up key1 > spop keynameskey1 ordered collection zSet

Ordered set is a feature of redis, which is similar to the combination of SortedSet and HashMap. Its internal implementation is a data structure called a jump list. Ordered set on the one hand, it is a set, so each element is unique, and then it can assign a score to each value, and then sort according to this score, score is equivalent to a permission sort identity.

Ps: for this reason, ordered collections can be used to store fan information. Value is fan id,score is follow time.

/ / 9.0 is score, that is, weight > zadd keyname 9.0 math1 > zadd keyname 9.2 history1// order > zrange keyname 0-11) history2) math// reverse order > zrevrange keyname 0-11) math2) history// is equivalent to count () > zcard keyname2 to obtain score > zscore keyname math9 of the specified key

Jump list TODO

Thank you for reading! About what the basic data structure of Redis is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report