Installation and use of Hiredis under Linux (i.) __linux

Source: Internet
Author: User
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.

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.