Hiredis_API description, hiredisapi
A) Compile and install
Make
Make install (/usr/local)
Make install PREFIX = $ HOME/progs (you can specify the installation path as needed)
B) Synchronous API
RedisContext * redisConnect (const char * ip, int port );
Void * redisCommand (redisContext * c, const char * format ,...);
Void freeReplyObject (void * reply );
1) establish a connection
RedisContext * c = redisConnect ("127.0.0.1", 6379 );
If (c! = NULL & c-> err ){
Printf ("Error: % s \ n", c-> errstr );
// Handle error
}
The redisConnect function is used to create a redisContext object that contains connection-related information.
It contains an err field. 0 indicates normal, and others indicate an error! The error message can be known through the errstr field.
2) execute the command
Reply = redisCommand (context, "SET key value ");
Reply = redisCommand (context, "SET key % s", value );
Reply = redisCommand (context, "SET key % B", value, (size_t) valuelen );
Reply = redisCommand (context, "SET key: % s", myid, value );
The call format of redisCommand is similar to that of the printf function. The function of the second call statement above is
Enter the value content in binary format, and the length of the binary must be indicated later!
3) The redisCommand function returns a redisReply. We need to determine its type field.
To know the specific content returned:
REDIS_REPLY_STATUS indicates the status. The content can be viewed using the str field. the string length is the len field.
REDIS_REPLY_ERROR indicates an error. view the error information, as shown in the str and len fields above.
REDIS_REPLY_INTEGER returns an integer and obtains the value from the integer field.
No data is returned for REDIS_REPLY_NIL.
REDIS_REPLY_STRING returns a string. Check the str and len fields.
REDIS_REPLY_ARRAY returns an array to view the values of elements (number of arrays ).
Element [index] method to access array elements. Each array element is
Pointer of A redisReply object
4) there is another similar function that executes commands in batches:
Void * redisCommandArgv (redisContext * c, int argc,
Const char ** argv, const size_t * argvlen );
5) After redisReply is used, use the freeReplyObject function to release and destroy the object.
Void redisFree (redisContext * c) is used to disconnect and release redisContext content.
6) Description of the redisCommand function execution process:
A. Format redis command
B. Put the formatted command content in the output buffer of redisContext
C. Call the redisGetReply function to execute the command. The result is displayed.
7) pipeline usage:
A. Enter the command to be executed.
Void redisAppendCommand (redisContext * c, const char * format ,...);
Void redisAppendCommandArgv (redisContext * c, int argc,
Const char ** argv, const size_t * argvlen );
B. Obtain the output result of the command.
Int redisGetReply (redisContext * c, void ** reply );
C. Release output results
Void freeReplyObject (void * reply );
Example:
RedisReply * reply = NULL;
RedisAppendCommand (context, "set key1 value ");
RedisAppendCommand (context, "get key2 ");
RedisGetReply (context, & reply); // reply for set
FreeReplyObject (reply );
RedisGetReply (context, & reply); // reply for get
FreeReplyObject (reply );
Subscription mode:
Reply = redisCommand (context, "SUBSCRIBE test ");
FreeReplyObject (reply );
While (redisGetReply (context, & reply) = REDIS_ OK ){
// Consume message
FreeReplyObject (reply );
}
8) redisReply:
REDIS_ OK is normal
An error occurred while reading/writing REDIS_ERR_IO. Use errno to check the cause.
The REDIS_ERR_EOF server closes the link and the read ends.
REDIS_ERR_PROTOCOL error in analyzing redis protocol content
Other unknown EDIS_ERR_OTHER errors
You can use the errstr field of redisReply to view the brief description of the error type.
C) asynchronous API (asynchronous API usage is similar to synchronous API, so list different functions here)
1. Connect to the redis Server
RedisAsyncContext * c = redisAsyncConnect ("127.0.0.1", 6379 );
If (c-> err ){
Printf ("Error: % s \ n", c-> errstr );
// Handle error
}
2. Set the hook function for connection and disconnection
Int redisAsyncSetConnectCallback (redisAsyncContext * ac,
RedisConnectCallback * fn );
Int redisAsyncSetDisconnectCallback (redisAsyncContext * ac,
RedisDisconnectCallback * fn );
3. Insert Command Information
Int redisAsyncCommand (
RedisAsyncContext * ac, redisCallbackFn * fn, void * privdata,
Const char * format ,...);
Int redisAsyncCommandArgv (
RedisAsyncContext * ac, redisCallbackFn * fn, void * privdata,
Int argc, const char ** argv, const size_t * argvlen );
The command output is the same as the synchronization API.
4. Close the connection
Void redisAsyncDisconnect (redisAsyncContext * ac );
D) Auxiliary API
The following APIs are used to read and analyze data after being bound to other programming languages.
RedisReader * redisReaderCreate (void );
Void redisReaderFree (redisReader * reader );
Int redisReaderFeed (redisReader * reader, const char * buf, size_t len );
Int redisReaderGetReply (redisReader * reader, void ** reply );