In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the example analysis of redis string type, which is very detailed and has certain reference value. Friends who are interested must finish it!
We all know that redis is developed in C, so string in C uses the char [] array, and then you might think, that's not easy. When I execute the following command, I must have shoved it directly into the char [] array.
If you really think so, there will be several questions coming to cut you. First, let's find a redis manual, http://doc.redisfans.com/.
First: if you execute the Append function every time, does redis's char [] need to be expanded again every time, and is it time-consuming every time?
Second: if you execute StrLen in String every time, does the bottom layer of redis have to traverse the char array every time to get the results?
1. Explore how String is stored in Redis
According to the small cases mentioned above, the author of redis is not that stupid, and the normal logic should be to encapsulate another layer at the level of the char [] array.
1. SDS structure
In redis, SDS (simple dynamic string) is used to encapsulate char [], which is also the smallest unit of redis storage. The next question is where can you see it? When I was in the wget package, there was redis source code in it, and it is said that there are only more than 3w lines, which tells us that if there is any problem, we should have plenty of food and clothing by ourselves, right? in order to find out conveniently, I will drag the source code of redis to window and open it with vs. Next, let's see what SDS looks like.
You can see that it is defined in the sds.h source file in the redis source code, and you may wonder, what are these three attributes used for? Let me say a few words.
Len: Mark the length of char [], which is somewhat similar to the length of List in our C #.
Free: marking the number of unused elements in char [] means that there are several holes.
Buf []: the pit in which elements are stored is not necessarily equal to the actual number of elements, such as the cnblogs mentioned earlier. It could also be [c] [n] [b] [l] [o] [g] [s] [/ 0] [].
2. Explore Redis objects (RedisObject)
The SDS mentioned earlier is only the encapsulation of the char [] array and does not identify the five types in redis, so it is conceivable that redis also needs to be encapsulated on SDS, so there is the next
RedisObject object, let's see what it looks like first.
You can see that RedisObject is in the redis.h source code file, so I'll talk briefly about the type and ptr attributes, and I'll talk about it in more detail later.
Type is used to identify which type redisObject is. Since it is which type, there must be a type enumeration, right? there must be one. Let me show you.
* ptr can see that this thing is also a pointer type, and the memory address it points to, you should also know, is the so-called SDS enumeration type.
Well, by now you can integrate what the blog started with:
127.0.0.1 cnblogs 6379 > set name cnblogsOK127.0.0.1:6379 > get name "cnblogs" 127.0.0.1
For the above set command, redis actually creates two RedisObject objects, the RedisObject of the key and the RedisOjbect of the value, in which their type=REDIS_STRING, that is, the string object type, where the SDS stores the characters of name and cnblogs respectively, well, that's about it.
Third, choose some interesting orders
1. Incr,incrby,decr,decrby
These four commands are a bit like the methods of the Interlocked class in C#. If you know Interlocked, you should know that there are various methods of atomic self-increment, self-subtraction, and so on, as shown in the following figure:
What are the benefits of this self-increasing redis? I think it's good to use this to generate the order number. I remember that when I was in Ctrip, the order number was generated by a special func function in OrderIDDB, so that OrderID does not depend on any business library, and then we can relatively convenient sub-library sub-table, now it is good to use redis to do so.
There is nothing to say about some other commands, you can take a look at the redis manual.
The above is all the content of the article "sample Analysis of redis string types". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.