Type of string for Redis Foundation

Source: Internet
Author: User

redis--the second base string type

We all know that Redis is developed in C language, so in c it means that string is in the char[] array, and then you might think, that's not easy, when I execute the following command, it's definitely straight.

The char[] array to be plugged into.

If you really think so, there will be a few questions to come and cut you, first we find a Redis handbook, http://doc.redisfans.com/

First: If you execute the APPEND function every time, is it not Redis's char[] every time it needs to be expanded again, so it is time-consuming operation?

Second: If you execute the strlen in the string every time, is the Redis layer going through the char array every time to get the result?

One: Explore how the string in Redis is stored

  

According to the above-mentioned small situation, so the Redis author is not so silly, the normal logic should be in the char[] array of the level of their own to encapsulate a layer, you say right???

1. SDS Structural BODY

In Redis, which uses SDS (simple dynamic string) to encapsulate char[], this is the smallest unit of Redis storage, and the next question is where can I see it? I'm in wget pressure.

Shrink the time, there is a redis source, it is said that there are only 3w more lines, which tells us, what problems, self-clothed, right, for the convenience of finding, I will be the source of Redis

to the window with VS open, next we look at SDS grow what kind???

You can see that it is defined in the Sds.h source file in the Redis source code, you may wonder, what are these three properties for??? Let me just say it briefly.

<1> Len: The length of the marker char[] is a bit like the size of the list in our C #.

<2> Free: The number of unused elements in the tag char[] is the meaning of a few empty pits.

<3>buf[]: The hole that holds the element is not necessarily equal to the actual number of elements, such as the Cnblogs mentioned earlier. It may also be [c][n][b][l][o][g][s][/0][][][].

II: Explore Redis objects (Redisobject)

The SDS mentioned earlier is only a char[] array and does not identify the top 5 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 the redisobject is in the Redis.h source code file, and below I briefly say the type and PTR attributes, detailed things in the follow-up said.

<1> type This is used to identify what type of redisobject is, since which type, there must be a type enumeration, right, definitely have, show you.

      

<2> *ptr can see this thing as a pointer type, it points to the memory address, you should also know, is called the SDS enumeration type.

Well, by now you can integrate the blog at the beginning:

For the set command above, REDIS actually creates two Redisobject objects, the redisobject of the keys, and the redisojbect of the values where they are type=redis_string,

is also a String object type, where the SDS is stored in the name and cnblogs of the word Fu Yi, well, probably so.

Three: Pick a few interesting commands

1. Incr,incrby,decr,decrby

These four commands are a bit like the Interlocked class method in C #, and if you know interlocked, you should know that there are various kinds of atomic self-increment, self-reduction and so on, such as:

What is the benefit of Redis's self-increment? I think it's nice to use this to generate the order number, I remember when Ctrip, the generation of the order number is a special orderiddb in the Func function to generate,

So OrderID is not dependent on any business library, and then we can be relatively convenient sub-library sub-table, now using Redis is also very good.

Some other orders have nothing to say, you can look at the Redis Handbook to see it, and this concludes, ready to go to the company.

Type of string for Redis Foundation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.