Basic use of Hiredis, use of Hiredis
0. Preface
Hiredis is a Redis C client library function that basically implements the minimal set of Redis protocols. Here we will give a basic introduction and application of hiredis APIs. We will mainly refer to the README file of hiredis and relevant source code.
1. synchronous API
RedisContext: the context of the database.
1 /* Context for a connection to Redis */ 2 typedef struct redisContext { 3 int err; /* Error flags, 0 when there is no error */ 4 char errstr[128]; /* String representation of error when applicable */ 5 int fd; 6 int flags; 7 char *obuf; /* Write buffer */ 8 redisReader *reader; /* Protocol reader */ 9 10 enum redisConnectionType connection_type;11 struct timeval *timeout;12 13 struct {14 char *host;15 char *source_addr;16 int port;17 } tcp;18 19 struct {20 char *path;21 } unix_sock;22 23 } redisContext;
A. Connect to Redis
1 // connect to redis. If the error redisContext. err is set to 1, redisContext. errstr will contain the description error message 2 redisContext * redisConnect (const char * ip, int port );
B. Execute the Redis command synchronously.
1/* 2 3 execute the redis command synchronously. Similar to printf, % B imports binary data, which requires the length specified by the size_t parameter. For example, redisCommmand (c, "SET foo % B", arr, (size_t) len); 4 failure: NULL is returned, and the err field is SET to 1. Once an error occurs, this redisContext can no longer be used. You need to reconnect to 5 successfully: return the redisReply pointer 6 7 */8 void * redisCommand (redisContext * c, const char * format ,...);
1/* 2 send a group of commands, which are specified by argv and argvlen. When argvlen is NULL, the length of each argv string is calculated by strlen. Therefore, when binary data needs to be transmitted, all argvlen data must be input. 3 */4 void * redisCommandArgv (redisContext * c, int argc, const char ** argv, const size_t * argvlen); 5 6/* 7 Piplining, after the APPEND Command, you can use redisGetReply to obtain the return value of the command. 8 */9 void redisAppendCommand (redisContext * c, const char * format ,...); 10 void redisAppendCommandArgv (redisContext * c, int argc, const char ** argv, const size_t * argvlen); 11 12 // obtain the command return value. Note: use freeReplyObject to release 13 int redisGetReply (redisContext * c, void ** reply );
C. Response Data Structure
Typedef struct redisReply {int type;/* REDIS_REPLY _ ** // specify The returned long integer type;/* 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_ARRAY */struct redisReply ** element; /* elements vector for REDIS_REPLY_ARRAY */} redisReply;
Type of data returned by Redis, redisReply. type field
1 # define REDIS_REPLY_STRING 1 // string 2 # define REDIS_REPLY_ARRAY 2 // array, multiple reply, access 3 # define REDIS_REPLY_INTEGER 3 through the element array and elements array size, integer field 4 # define REDIS_REPLY_NIL 4 // null, no data 5 # define REDIS_REPLY_STATUS 5 // STATUS, str string and len6 # define REDIS_REPLY_ERROR 6 // error, same as STATUS
D. release resources
1 // release the reply structure 2 void freeReplyObject (void * reply );
// Close the connection and release the memory void redisFree (redisContext * c );
E. Description of error fields redisContext. err
1 // error field, it is set to one of the following values: 2 # define REDIS_ERR_IO 1/* Error in read or write */3 # define REDIS_ERR_EOF 3/* End of file */4 # define REDIS_ERR_PROTOCOL 4 /* protocol error */5 # define REDIS_ERR_OOM 5/* Out of memory */6 # define REDIS_ERR_OTHER 2/* Everything else... */
2. asynchronous API
To be supplemented
3. synchronous API example (implement subscription function)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "hiredis.h" 4 5 void ProcessReply( redisReply * pReply ) 6 { 7 redisReply * pSubReply = NULL; 8 9 if ( pReply != NULL && pReply->elements == 3 )10 {11 pSubReply = pReply->element[2];12 printf( "Msg [%s]\n", pSubReply->str );13 }14 }15 16 int main( int argc, char const *argv[] )17 {18 redisContext * pContext = redisConnect( "127.0.0.1", 6379 );19 20 if ( NULL == pContext || pContext->err == 1 )21 {22 printf( "%s\n", pContext->errstr );23 exit( -1 );24 }25 26 char * pKey = "DATABUS:REQ";27 28 redisReply * pReply = redisCommand( pContext, "SUBSCRIBE %s", pKey );29 freeReplyObject( pReply );30 while ( redisGetReply( pContext, (void **)&pReply ) == REDIS_OK )31 {32 ProcessReply( pReply );33 freeReplyObject( pReply );34 }35 36 redisFree( pContext );37 38 return 0;39 }
The running result is as follows: