码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-03 13:29:02      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

在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
}

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



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

标签:

原文地址:http://my.oschina.net/bxxfighting/blog/374773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!