Hiredis
is a simple C client library of the Redis database. It is minimalist because it only adds minimal support for the Protocol, but at the same time it uses a high-level printf-like API, so it is very easy to use for C programming users accustomed to printf style, and the API does not explicitly bind each Redis command.
Github:https://github.com/redis/hiredis
Installing Hiredis
Compile and install:
gitcdmakemake install
The following is make install
the output:
mkdir -p /usr/local/include/hiredis /usr/local/include/hiredis/adapters /usr/local/libcp -pPR hiredis.h async.h read.h sds.h /usr/local/include/hirediscp -pPR adapters/*.h /usr/local/include/hiredis/adapterscp -pPR libhiredis.so /usr/local/lib/libhiredis.so.0.13cd /usr/local/lib && ln -sf libhiredis.so.0.13 libhiredis.socp -pPR libhiredis.a /usr/local/libmkdir -p /usr/local/lib/pkgconfigcp -pPR hiredis.pc /usr/local/lib/pkgconfig
Copy the dynamic link library to Lib:
cp
If the 32 system only needs to run:
cp libhiredis.so /usr/lib
You also need to update the dynamic-link library cache:
/sbin/ldconfig
ldconfig
The purpose of the command is to search for a shareable dynamic-link library (in the form of a format) in the default search directory and in the /lib
/usr/lib
directories listed in the dynamic library configuration file, and to /etc/ld.so.conf
create the lib*.so*
ld.so
connection and cache files required for the dynamic loader (). The cache file defaults to /etc/ld.so.cache
, this file holds the ordered list of dynamic link library names, in order to let the dynamic link library for the system to share, need to run the dynamic Link Library Management command ldconfig
, this execution program is stored in the/sbin directory.
ldconfig
Typically runs at system startup, and when a user installs a new dynamic-link library, it is necessary to run the command manually.
Client code
TEST.c
#include <stdio.h> #include int main() { redisContext* conn = redisConnect("127.0.0.1"6379); if(conn->err) printf("connection error:%s\n", conn->errstr); "set foo 1234"); freeReplyObject(reply); "get foo"); printf("%s\n", reply->str); freeReplyObject(reply); redisFree(conn); return0; }
And then:
gcc test.c -o test -lhiredis
Compiling is sure to use the -l
connect dynamic library.
-lstack
Tells the compiler to link the lhiredis
library.
Run:
$ ./test1234
Function prototype Redisconnect
redisContext *redisConnect(constcharint port)
Note: This function is used to connect the Redis database with the IP address and port of the database, and the general Redis database has a port of 6379.
The function returns a struct body redisContext
.
Rediscommand
voidconstchar *format, ...);
Description: The function executes commands, just like SQL statements in SQL database, only executes the redis
operation commands in the database, the first parameter is returned when the database is connected redisContext
, and the remaining arguments are variables, as in the case of C standard function printf
functions.
The return value for the void*
general cast becomes redisReply
the type for further processing.
Freereplyobject
void freeReplyObject(void *reply);
Description: Releases the redisCommand
occupied memory returned after execution redisReply
.
Redisfree
void redisFree(redisContext *c);
Description: Releases the redisConnect()
resulting connection.
Reference
1. Redis: Installation, configuration, operation and simple code example (C-language client)-CSDN Blog
8724907
2. C-language access to Redis (Hiredis)-CSDN Blog
38015765
3. Error while loading shared libraries problem Resolution-CSDN Blog
50971142
4, ldconfig command _linux ldconfig command usage Detailed: Dynamic link Library Management command
Http://man.linuxde.net/ldconfig?mstqrk=qdwrv1
C language access to Redis using Hiredis