Redis Memory Database C client Hiredis API Chinese description

Source: Internet
Author: User
Tags redis server

A) Compile and install

Makemake Install (/usr/local) make install PREFIX= $HOME/progs (You can freely specify the installation path)

B) Synchronous API interface

Rediscontext *redisconnect (constcharint  port); void 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 something called Rediscontext, which contains information about the connection, has an ERR field, 0 is normal, and the other indicates an error! You can know the error message through the Errstr field.


2) Execute the command

" SET Key Value "  "set key%s" "set key%b"   "SET key:%s%s", myID, value);

The invocation format of the Rediscommand is similar to the printf function, where the second invocation statement is the value content of the input binary format, which must then indicate the byte length of the binary!


3) The Rediscommand function returns a thing called redisreply, and we need to know what content to return by judging its Type field:

Redis_reply_status      represents the state, content is viewed through the Str field, the string length is the Len field Redis_reply_error       indicates an error, and an error message is viewed, such as the Str,len field on Redis_reply_ Integer    returns integers, gets the value from the integer field Redis_reply_nil            no data returned redis_reply_string      return string, view Str,len field Redis_reply_ The array       returns an array that looks at the value of the elements (number of arrays) and accesses the elements in the form of Element[index], each of which is a pointer to a Redisreply object

4) There is also a similar function for batch execution of commands:

void int Const Char Const size_t *argvlen);

5) After the redisreply is used, the function Freereplyobject will be used to release the destruction

The role of void redisfree (Rediscontext *c) is to disconnect and release rediscontext content

6) Description of the Rediscommand function execution process:
A. Formatting Redis command
B. Formatted command contents into the output buffer of Rediscontext
C. Call the Redisgetreply function to execute the command to get the result

7) How to use the pipe:
A. Fill in the commands that need to be executed

void Const Char *format, ...); void int Const Char Const size_t *argvlen);

B. Getting the output of a command

int void **reply);

C. Releasing output results

void freereplyobject (void *reply);


Example:

Redisreply *reply = Null;redisappendcommand (context,"set key1 value"); Redisappendcommand (context,"get Key2"); redisgetreply (context,//  reply for setfreereplyobject (reply); Redisgetreply (context,//  reply for GetFreereplyobject (reply);


Subscription mode:

 reply = Rediscommand (Context, subscribe Test    while  (Redisgetreply (context,&reply) == Redis_ok) { //  consume message   Freereplyobject ( Reply);}  


8) redisreply return result processing:
redis_ok                     Normal
redis_err_io         IO read/write exception, view reason by errno
redis_err_eof             Server closed link, read end
redis_err_protocol  Analysis REDIS Protocol content error
edis_err_other           Other unknown error
The above error types can be viewed through the Errstr field of redisreply for a short description

C) Asynchronous API ( Asynchronous APIs are used in the same way as the synchronization API, where you list different functions.
1. Connect to 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 to connect and disconnect

int redisasyncsetconnectcallback (redisasynccontext *ac, Redisconnectcallback *fn); int redisasyncsetdisconnectcallback (redisasynccontext *ac, Redisdisconnectcallback *FN);

3. Inserting command information

int void *privdata,constchar *format, ...); int void int Const Char Const size_t *argvlen);

Get command output same as synchronization API


4. Close the connection

void redisasyncdisconnect (Redisasynccontext *ac);


D) Auxiliary API
The following API is mainly used for other programming language bindings after the operation, can read the analysis data

Redisreader *redisreadercreate (void); void redisreaderfree (redisreader *reader); int Const Char *buf, size_t len); int void **reply);

Organized from: http://www.mamicode.com/info-detail-501902.html

Redis Memory Database C client Hiredis API Chinese description

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.