首页 / 数据库 / MySQL / C基础 redis缓存访问
引言 先说redis安装, 这里采用的环境是. Linux version 4.4.0-22-generic (buildd@lgw01-41) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2) ) #40-Ubuntu SMP Thu May 12 22:03:46 UTC 2016 对于 ubuntu 安装 redis是非常简单的. 这里采用源码安装. 安装代码如下wget http://download.redis.io/releases/redis-3.0.6.tar.gztar xzf redis-3.0.6.tar.gzcd redis-3.0.6make 安装后我的环境是那我们测试一下. 安装结果. 先启动 redis-server 服务器.再启动 redis-cli 客户端我们开始测试一下. 测试之后一切正常. redis linux上安装基本完毕了. 更加详细的参照Redis 官网教程 很详细 http://www.redis.net.cn/tutorial/3501.html前言 现在我们安装 redis c 访问的驱动. hiredis. 一开始都是下载安装. 我是直接从 hiredis git官网下载安装的.hiredis 源码 https://github.com/redis/hirediswget https://github.com/redis/hiredis/archive/master.zipunzip master.zip 安装完毕会看见这样环境执行安装命令makesudo make install本质对于 make install 执行了下面步骤mkdir -p /usr/local/include/hiredis /usr/local/libcp -a hiredis.h async.h read.h sds.h adapters /usr/local/include/hirediscp -a libhiredis.so /usr/local/lib/libhiredis.so.0.13cd /usr/local/lib && ln -sf libhiredis.so.0.13 libhiredis.socp -a libhiredis.a /usr/local/libmkdir -p /usr/local/lib/pkgconfigcp -a hiredis.pc /usr/local/lib/pkgconfig此刻基本上 hiredis 驱动已经安装完毕. 后面解释一下, 驱动提供的api.常用的 api如下./* * redis链接函数, 返回redis上下文. * ip: 链接地址的ip * port: 链接端口 * : 返回 redis上下文, NULL表示获取失败 */redisContext *redisConnect(const char *ip, int port)/* *执行redis操作命令, 返回得到的结果集 *context: redisConnect 返回的redis上下文对象 *format: 等同于 printf格式控制符 *...: 后面可变参数, 需要和 format中格式符对应 *: 返回 得到的结果集 */void *redisCommand(redisContext *context, const char *format, ...);/* * 释放redis命令操作返回过来的结果集 * reply: redisCommand返回的结果集 */void freeReplyObject(void *reply);/* * 释放链接上下文 * context: redisConnect返回的链接上下文 */void redisFree(redisContext *context); 更加详细的解释我们可以看 源码接口文件 hiredis/hiredis.h . 例如第一个是 redisConnect 返回的 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 */enum redisConnectionType connection_type;struct timeval *timeout;struct {char *host;char *source_addr;int port;} tcp;struct {char *path;} unix_sock;} redisContext;还有一个是 redisCommand 返回的命令集/* This is 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_ARRAY */struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */} redisReply; 关于 hiredis基本的C驱动接口,解释完毕. 后面开始写demo测试一番.最好的理解方式还是看官方源码和测试代码.正文 首先来个简单的demo测试. simpleget.c#include <stdio.h>#include <stdlib.h>#include <hiredis/hiredis.h>/* * 请求 redis网络缓存服务器内存. */int main(int argc, char* argv[]) {redisContext *conn = redisConnect("127.0.0.1", 6379);if(NULL == conn) {fprintf(stderr, "redisConnect 127.0.0.1:6379 error!
");exit(EXIT_FAILURE);} if(conn->err) {fprintf(stderr, "redisConect error:%d
", conn->err);redisFree(conn);exit(EXIT_FAILURE);} // 这里redisConnect 链接对象创建完毕了redisReply *reply = redisCommand(conn, "get foo");if(reply && reply->type == REDIS_REPLY_STRING) {printf("get foo => %s
", reply->str);} printf("reply->type = %d
", reply->type);// 释放这个对象freeReplyObject(reply);// 释放hiredis 上下文对象 redisFree(conn);return 0;}编译命令是gcc -Wall -ggdb -o simpleget.out simpleget.c -lhiredis最终测试结果是
这里表明流程是跑通了. 这里扩展一下, 有时候在Linux上查找函数或宏定义声明好麻烦. 我用的方式是 find . -name *.h | xargs grep "REDIS_REPLY_STRING"笨方法也挺实用的. 查找的结果是 上面 REDIS_REPLY_STRING 定义在 hiredis/read.h 中 摘录部分如下#define REDIS_REPLY_STRING 1#define REDIS_REPLY_ARRAY 2#define REDIS_REPLY_INTEGER 3#define REDIS_REPLY_NIL 4#define REDIS_REPLY_STATUS 5#define REDIS_REPLY_ERROR 6 通过这些宏枚举区分返回的值. 其实到这里基本上 关于 redis接口使用基本入门了. 后面再举一个 操作list的操作代码 setlist.c#include <stdio.h>#include <stdlib.h>#include <signal.h>#include <hiredis/hiredis.h>/* * 请求 redis网络缓存服务器内存. */int main(int argc, char* argv[]) {// 忽略服务器退出,导致当前进程退出signal(SIGPIPE, SIG_IGN);redisContext *conn = redisConnect("127.0.0.1", 6379);if(NULL == conn) {fprintf(stderr, "redisConnect 127.0.0.1:6379 error!
");exit(EXIT_FAILURE);} if(conn->err) {fprintf(stderr, "redisConect error:%d
", conn->err);redisFree(conn);exit(EXIT_FAILURE);} // 这里redisConnect 链接对象创建完毕了freeReplyObject(redisCommand(conn, "lpush mylist foo"));freeReplyObject(redisCommand(conn, "lpush mylist bar"));redisReply *reply = redisCommand(conn, "lrange mylist 0 -1");if(reply && reply->type == REDIS_REPLY_ARRAY && reply->elements == 2) {printf("%s %s
", reply->element[0]->str, reply->element[1]->str);} else {printf("redisCommand [lrange mylist 0 -1] error:%d. %s
", reply->type, reply->str);} // 释放这个对象freeReplyObject(reply);// 释放hiredis 上下文对象 redisFree(conn);return 0;} 编译代码 gcc -Wall -ggdb -o setlist.out setlist.c -lhiredis 运行结果如下更加详细介绍请参照 hiredis git上 源码.后记 到这里关于C简单使用控制redis服务器, 基本讲完了. 错误是难免的. 欢迎指正. 下面关于Redis的文章您也可能喜欢,不妨参考下:Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htmRedis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htmRedis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htmUbuntu 12.10下安装Redis(图文详解)+ Jedis连接Redis http://www.linuxidc.com/Linux/2013-06/85816.htmRedis系列-安装部署维护篇 http://www.linuxidc.com/Linux/2012-12/75627.htmCentOS 6.3安装Redis http://www.linuxidc.com/Linux/2012-12/75314.htmRedis安装部署学习笔记 http://www.linuxidc.com/Linux/2014-07/104306.htmRedis配置文件redis.conf 详解 http://www.linuxidc.com/Linux/2013-11/92524.htmRedis 的详细介绍:请点这里
Redis 的下载地址:请点这里本文永久更新链接地址