【Practice what you have learned】Operate Redis in C++

Insert picture description here

I've been working on the redis source code these days, ignoring the application aspects. My computer is relatively stuck, so I didn't cut a few pictures tonight, forgive me.

Take two minutes to configure hiredis with me

When we downloaded redis 6.0.6, we actually already had a C++ version of the operating library, but some people didn't find it.
Insert picture description here

If you really can’t find it, or the version you downloaded is not the 6th generation or above, and you didn’t bring it with you, privately message me and I will give you.

You can see that there is a makefile in this directory, so it's easy to handle.
However, if you have to drag around after finishing the library, it will be trouble after all.

make install

complete in one step.

In fact, they have even prepared the test function for you. There is a folder in the hedis folder, example, which contains an example.c file.

Compile like this, if not: First, you need to change the header file inside: #include<hiredis/hiredis.h>
remember to bring dependencies when compiling:
gcc example.c -o example -L/usr/local/lib -lhiredis

When you run it, (don't tell me you can't run it: ./example) If nothing happens, it will tell you that the dependencies cannot be found.
Normal, teach you a symptom treatment:

Create a new file usr-libs.conf in the /etc/ld.so.conf.d/ directory, the content is: /usr/local/lib

Then use the command /sbin/ldconfig to update the configuration.

After configuring this thing, your virtual machine will be gone after restarting. The permanent configuration seems to be in another blog of mine, under the dynamic library column.

The final running effect:
Insert picture description here

Let’s talk about those functions

I have used others, of course, I need to know more about them:

If the computer is out of power, I will make a long story short. Examples can be found in that example.

Hiredis mainly contains four function interfaces:


redisContext* redisConnect(const char *ip, int port);

Parameter definition:
This function is used to connect to the redis database. The two parameters are the ip and port of the redis database. The port number is generally 6379.


void *redisCommand(redisContext *c, const char *format...);

This function is used to execute commands in the redis database. The first parameter is the redisContext returned by connecting to the database, and the remaining parameters are variable parameters.

The return value of this function is void*, but generally it will be cast to redisReply type for further processing.


void freeReplyObject(void *reply);

Release the memory occupied by the redisReply returned after the redisCommand is executed.


void redisFree(redisContext *c)

Release the connection created by redisConnect().


Long tail flow optimization

Finally, let’s get another wave of long-tail traffic optimization:

It is recommended that you collect it directly, or you will not find it if you paddle it.

Follow the good habit easily.

Follow me, the next article will analyze the source code of hiredis, hehe

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43762191/article/details/108611231