C基础 redis缓存访问
引言
先說redis安裝, 這里采用的環(huán)境是.
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?安裝后我的環(huán)境是
那我們測試一下. 安裝結(jié)果. 先啟動(dòng) redis-server 服務(wù)器.
再啟動(dòng) redis-cli 客戶端
我們開始測試一下.?
?測試之后一切正常. redis linux上安裝基本完畢了. 更加詳細(xì)的參照
Redis 官網(wǎng)教程 很詳細(xì) http://www.redis.net.cn/tutorial/3501.html
?
前言
現(xiàn)在我們安裝 redis c 訪問的驅(qū)動(dòng). hiredis. 一開始都是下載安裝. 我是直接從 hiredis git官網(wǎng)下載安裝的.
hiredis? 源碼 https://github.com/redis/hiredis
wget https://github.com/redis/hiredis/archive/master.zip unzip master.zip?安裝完畢會(huì)看見這樣環(huán)境
執(zhí)行安裝命令
make sudo make install?
本質(zhì)對于 make install 執(zhí)行了下面步驟
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 驅(qū)動(dòng)已經(jīng)安裝完畢. 后面解釋一下, 驅(qū)動(dòng)提供的api.
常用的 api如下.
/** redis鏈接函數(shù), 返回redis上下文.* ip : 鏈接地址的ip* port : 鏈接端口* : 返回 redis上下文, NULL表示獲取失敗*/ redisContext *redisConnect(const char *ip, int port)/** 執(zhí)行redis操作命令, 返回得到的結(jié)果集* context : redisConnect 返回的redis上下文對象* format : 等同于 printf格式控制符* ... : 后面可變參數(shù), 需要和 format中格式符對應(yīng)* : 返回 得到的結(jié)果集*/ void *redisCommand(redisContext *context, const char *format, ...);/** 釋放redis命令操作返回過來的結(jié)果集* reply : redisCommand返回的結(jié)果集*/ void freeReplyObject(void *reply);/** 釋放鏈接上下文* context : redisConnect返回的鏈接上下文*/ void redisFree(redisContext *context);?更加詳細(xì)的解釋我們可以看 源碼接口文件 hiredis/hiredis.h .? 例如
第一個(gè)是 redisConnect 返回的 redisContext上下文結(jié)構(gòu) /* 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;還有一個(gè)是 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;關(guān)于 hiredis基本的C驅(qū)動(dòng)接口,解釋完畢. 后面開始寫demo測試一番.最好的理解方式還是看官方源碼和測試代碼.
?
正文
首先來個(gè)簡單的demo測試. simpleget.c
#include <stdio.h> #include <stdlib.h> #include <hiredis/hiredis.h>/** 請求 redis網(wǎng)絡(luò)緩存服務(wù)器內(nèi)存.*/ 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 鏈接對象創(chuàng)建完畢了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);// 釋放這個(gè)對象 freeReplyObject(reply);// 釋放hiredis 上下文對象 redisFree(conn);return 0; }?
編譯命令是
gcc -Wall -ggdb -o simpleget.out simpleget.c -lhiredis?
最終測試結(jié)果是
這里表明流程是跑通了. 這里擴(kuò)展一下, 有時(shí)候在Linux上查找函數(shù)或宏定義聲明好麻煩. 我用的方式是
?
笨方法也挺實(shí)用的. 查找的結(jié)果是 上面 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?
?通過這些宏枚舉區(qū)分返回的值. 其實(shí)到這里基本上 關(guān)于 redis接口使用基本入門了. 后面再舉一個(gè) 操作list的操作代碼 setlist.c
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <hiredis/hiredis.h>/** 請求 redis網(wǎng)絡(luò)緩存服務(wù)器內(nèi)存.*/ int main(int argc, char* argv[]) {// 忽略服務(wù)器退出,導(dǎo)致當(dāng)前進(jìn)程退出 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 鏈接對象創(chuàng)建完畢了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);} // 釋放這個(gè)對象 freeReplyObject(reply);// 釋放hiredis 上下文對象 redisFree(conn);return 0; }?
編譯代碼?
gcc -Wall -ggdb -o setlist.out setlist.c -lhiredis?
?運(yùn)行結(jié)果如下
更加詳細(xì)介紹請參照 hiredis git上 源碼.
?
后記
? 到這里關(guān)于C簡單使用控制redis服務(wù)器, 基本講完了. 錯(cuò)誤是難免的. 歡迎指正. ?
/**********************************************************************
** 直接向大師們而不是他們得的學(xué)生領(lǐng)悟。?
** ? —— 阿貝爾
***********************************************************************/
?
轉(zhuǎn)載于:https://www.cnblogs.com/life2refuel/p/5560944.html
總結(jié)
以上是生活随笔為你收集整理的C基础 redis缓存访问的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sprint2-2.0
- 下一篇: 第二次冲刺阶段每日任务10