1. How to install Hiredis under Linux
1) Download Address
Https://github.com/redis/hiredis
2) Compiling and installing
The unpacked folder executes Make;make install;
3) header file contains
Include 4) Compilation options
Add ldflags =-lhiredis in makefile file
2. Main structure
The main focus is on 2 structural bodies,
1) Rediscontext
Corresponds to the Redis connection
/* Context for a connection to Redis
/typedef struct REDISCONTEXT {
int err;/* Error flags, 0 when there is no Error *
/char errstr[128];/* String representation of error when applicable
/int fd;
int flags;
Char *obuf; * Write buffer *
/redisreader *reader/* Protocol reader/
enum Redisconnectiontype connection_type;
struct Timeval *timeout;
struct {
char *host;
char *source_addr;
int port;
} TCP;
struct {
char *path;
} unix_sock;
} rediscontext;
2) redisreply
Response to Redis command
/* This are the reply object returned by Rediscommand ()/
typedef struct REDISREPLY {
int type;/* redis_reply_* * /
Long Long Integer/* The integer when type is Redis_reply_integer
/int len/* Length of String */
char * Str /* Used for both redis_reply_error and redis_reply_string
/size_t elements,/* Number of elements, for redis_reply_a Rray *
/struct redisreply **element;/* elements vector for Redis_reply_array
/} redisreply;
2. Main interface
There are 4 main interfaces,
1) rediscontext* redisconnect (const char *IP, int port)
Connect Redis.
2 void *rediscommand (Rediscontext *c, const char *format, ...);
Execute Redis Operation command
3) void Freereplyobject (void *reply);
Frees the memory that executes the Redis operation command reply
4) void Redisfree (Rediscontext *c);
Releases the connection context. 3. Exception Handling
There are 4 major anomalies,
1 The obtained rediscontext pointer is null
Exception handling: Try again to establish a new connection context with Redis.
2 Get the Rediscontext pointer err is not 0
Exception handling: Try again to establish a new connection context with Redis.
3 The obtained redisreply pointer is null
Exception handling: Disconnect the Redis connection and then try to execute the command again with Redis.
4 the type of the reply pointer obtained is not expected.
Exception handling: Disconnect the Redis connection and then try to execute the command again with Redis.
Now that Hiredis is finished, you can call it in another module.