阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

C基础 redis缓存访问

150次阅读
没有评论

共计 5122 个字符,预计需要花费 13 分钟才能阅读完成。

引言

先说 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.gz
tar xzf redis-3.0.6.tar.gz
cd redis-3.0.6
make

 安装后我的环境是

C 基础 redis 缓存访问

那我们测试一下. 安装结果. 先启动 redis-server 服务器.

C 基础 redis 缓存访问

再启动 redis-cli 客户端

C 基础 redis 缓存访问

我们开始测试一下. 

C 基础 redis 缓存访问

 测试之后一切正常. redis linux 上安装基本完毕了. 更加详细的参照

Redis 官网教程 很详细 http://www.redis.net.cn/tutorial/3501.html

前言

现在我们安装 redis c 访问的驱动. hiredis. 一开始都是下载安装. 我是直接从 hiredis git 官网下载安装的.

hiredis  源码 https://github.com/redis/hiredis

wget https://github.com/redis/hiredis/archive/master.zip
unzip master.zip

 安装完毕会看见这样环境

C 基础 redis 缓存访问

执行安装命令

make
sudo make install

本质对于 make install 执行了下面步骤

mkdir -p /usr/local/include/hiredis /usr/local/lib
cp -a hiredis.h async.h read.h sds.h adapters /usr/local/include/hiredis
cp -a libhiredis.so /usr/local/lib/libhiredis.so.0.13
cd /usr/local/lib && ln -sf libhiredis.so.0.13 libhiredis.so
cp -a libhiredis.a /usr/local/lib
mkdir -p /usr/local/lib/pkgconfig
cp -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!\n");
        exit(EXIT_FAILURE);
    }   
    if(conn->err) {fprintf(stderr, "redisConect error:%d\n", 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\n", reply->str);
    }   
    printf("reply->type = %d\n", reply->type);

    // 释放这个对象
    freeReplyObject(reply);
    // 释放 hiredis 上下文对象   
    redisFree(conn);

    return 0;
}

编译命令是

gcc -Wall -ggdb -o simpleget.out simpleget.c -lhiredis

最终测试结果是

C 基础 redis 缓存访问
这里表明流程是跑通了. 这里扩展一下, 有时候在 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!\n");
        exit(EXIT_FAILURE);
    }   
    if(conn->err) {fprintf(stderr, "redisConect error:%d\n", 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\n", reply->element[0]->str, reply->element[1]->str);
    }   
    else {printf("redisCommand [lrange mylist 0 -1] error:%d. %s\n", reply->type, reply->str);
    }   

    // 释放这个对象
    freeReplyObject(reply);
    // 释放 hiredis 上下文对象   
    redisFree(conn);

    return 0;
} 

编译代码 

gcc -Wall -ggdb -o setlist.out setlist.c -lhiredis

 运行结果如下

C 基础 redis 缓存访问

更加详细介绍请参照 hiredis git 上 源码.

后记

  到这里关于 C 简单使用控制 redis 服务器, 基本讲完了. 错误是难免的. 欢迎指正. 

下面关于 Redis 的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis 集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htm

Ubuntu 12.10 下安装 Redis(图文详解)+ Jedis 连接 Redis http://www.linuxidc.com/Linux/2013-06/85816.htm

Redis 系列 - 安装部署维护篇 http://www.linuxidc.com/Linux/2012-12/75627.htm

CentOS 6.3 安装 Redis http://www.linuxidc.com/Linux/2012-12/75314.htm

Redis 安装部署学习笔记 http://www.linuxidc.com/Linux/2014-07/104306.htm

Redis 配置文件 redis.conf 详解 http://www.linuxidc.com/Linux/2013-11/92524.htm

Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-06/132207.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计5122字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中