第一 基本的连接与添加数据

在redis中使用如下结构来保存状态,README里有这样一段话

Version 0.9.0 is a major overhaul of hiredis in every aspect. However, upgrading existing
code using hiredis should not be a big pain. The key thing to keep in mind when
upgrading is that hiredis >= 0.9.0 uses a `redisContext*` to keep state, in contrast to
the stateless 0.0.1 that only has a file descriptor to work with.

以前是直接用文件描述符来操作的,现在使用redisContext,下面是这个结构的定义。

/* 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 */
} redisContext;

  1. 下面先来练习连接,在README中是这样说的:

The function `redisConnect` is used to create a so-called `redisContext`. The
context is where Hiredis holds state for a connection. The `redisContext`
struct has an integer `err` field that is non-zero when the connection is in
an error state. The field `errstr` will contain a string with a description of
the error. More information on errors can be found in the **Errors** section.
After trying to connect to Redis using `redisConnect` you should
check the `err` field to see if establishing the connection was successful:
```c
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c != NULL && c->err) {
    printf("Error: %s\n", c->errstr);
    // handle error
}

这里有一个代码介绍,下面就按照这个来写一下。

A#include <hiredis.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    redisContext *c;
    redisReply *reply;
    //1. redisContext *redisConnect(const char *ip, int port);
#if 0
    c = redisConnect("127.0.0.1", 6379);
    if (c == NULL || c->err) {
        if (c) {
            printf("redisConnect() error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("redisConnect() error: can't allocate redis context\n");
        }
        exit(1);
    }
#endif
#if 0
    //2. redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
    struct timeval timeout = {1, 500 * 1000};   //  1.5s
    c = redisConnectWithTimeout("127.0.0.1", 6379, timeout);
    if (c == NULL || c->err) {
        if (c) {
            printf("redisConnectWithTimeout() error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("redisConnectWithTimeout() error: can't allocate redis context\n");
        }
        exit(1);
    }
#endif
#if 1
    //3. redisContext *redisConnectNonBlock(const char *ip, int port);
    c = redisConnectNonBlock("127.0.0.1", 6379);
    if (c == NULL || c->err) {
        if (c) {
            printf("redisConnectNonBlock() error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("redisConnectNonBlock() error: can't allocate redis context\n");
        }
        exit(1);
    }
#endif
#if 0
    //4. redisContext *redisConnectBindNonBlock(const char *ip, int port, const char *source_addr);
    c = redisConnectBindNonBlock("127.0.0.1", 6379, "192.168.22.114");
    if (c == NULL || c->err) {
        if (c) {
            printf("redisConnectBindNonBlock() error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("BindNonBlock() error: can't allocate redis context\n");
        }
        exit(1);
    }
#endif
    //5. redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port, const char *source_addr);
    //6. redisContext *redisConnectUnix(const char *path);
    //7. redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
    //8. redisContext *redisConnectUnixNonBlock(const char *path);
    //9. redisContext *redisConnectFd(int fd);
    //
    //
    reply = redisCommand(c, "PING");
    printf("PING: %s\n", reply->str);
    freeReplyObject(reply);
    return 0;
}


这里有这么多连接的函数,我用头几个可以,但是如果是NonBlock的就会出现“段错误”,应该是有不一样的用法。

去查一查。





你可能感兴趣的:(第一 基本的连接与添加数据)