C基礎(chǔ) 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é)果. 先啟動 redis-server 服務(wù)器.
再啟動 redis-cli 客戶端
我們開始測試一下.
測試之后一切正常. redis linux上安裝基本完畢了. 更加詳細(xì)的參照
Redis 官網(wǎng)教程 很詳細(xì) http://www.redis.net.cn/tutorial/3501.html
前言
現(xiàn)在我們安裝 redis c 訪問的驅(qū)動. hiredis. 一開始都是下載安裝. 我是直接從 hiredis git官網(wǎng)下載安裝的.
hiredis 源碼 https://github.com/redis/hiredis
wget https://github.com/redis/hiredis/archive/master.zipunzip master.zip
安裝完畢會看見這樣環(huán)境
執(zhí)行安裝命令
makesudo 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ū)動已經(jīng)安裝完畢. 后面解釋一下, 驅(qū)動提供的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 . 例如
第一個是 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; 還有一個是 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ū)動接口,解釋完畢. 后面開始寫demo測試一番.最好的理解方式還是看官方源碼和測試代碼.
正文
首先來個簡單的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); // 釋放這個對象 freeReplyObject(reply); // 釋放hiredis 上下文對象 redisFree(conn); return 0; }
編譯命令是
gcc -Wall -ggdb -o simpleget.out simpleget.c -lhiredis
最終測試結(jié)果是
最終測試結(jié)果是
這里表明流程是跑通了. 這里擴(kuò)展一下, 有時候在Linux上查找函數(shù)或宏定義聲明好麻煩. 我用的方式是
find . -name *.h | xargs grep 'REDIS_REPLY_STRING'
笨方法也挺實用的. 查找的結(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ū)分返回的值. 其實到這里基本上 關(guān)于 redis接口使用基本入門了. 后面再舉一個 操作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); } // 釋放這個對象 freeReplyObject(reply); // 釋放hiredis 上下文對象 redisFree(conn); return 0; }
編譯代碼
gcc -Wall -ggdb -o setlist.out setlist.c -lhiredis
運行結(jié)果如下
更加詳細(xì)介紹請參照 hiredis git上 源碼.
后記
到這里關(guān)于C簡單使用控制redis服務(wù)器, 基本講完了. 錯誤是難免的. 歡迎指正.
以上這篇C基礎(chǔ) redis緩存訪問詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 【Redis緩存機(jī)制】詳解Java連接Redis_Jedis_事務(wù)
- Python的Flask框架使用Redis做數(shù)據(jù)緩存的配置方法
- CI框架中redis緩存相關(guān)操作文件示例代碼
- windows環(huán)境下Redis+Spring緩存實例講解
- Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)
- Redis整合Spring結(jié)合使用緩存實例
- 圖文詳解Windows下使用Redis緩存工具的方法
- spring結(jié)合redis如何實現(xiàn)數(shù)據(jù)的緩存
- PHP使用redis實現(xiàn)統(tǒng)計緩存mysql壓力的方法
- php操作redis緩存方法分享
- Redis緩存詳解
相關(guān)文章
C++實現(xiàn)LeetCode(116.每個節(jié)點的右向指針)
這篇文章主要介紹了C++實現(xiàn)LeetCode(116.每個節(jié)點的右向指針),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Qt數(shù)據(jù)庫應(yīng)用之?dāng)?shù)據(jù)打印到pdf
因為xls打開以后用戶可以修改數(shù)據(jù)造假之類的,而pdf默認(rèn)是不可編輯的,除非借助專業(yè)的工具,所以如果想要限定用戶導(dǎo)出數(shù)據(jù)不能被更改,那導(dǎo)出pdf是最佳選擇。所以本文將為代價介紹Qt實現(xiàn)數(shù)據(jù)打印到pdf的方法,需要的可以參考一下2022-01-01如何使用C語言將數(shù)字、字符等數(shù)據(jù)寫入、輸出到文本文件中
在分析數(shù)據(jù)時,首先要解決數(shù)據(jù)的保存問題,下面這篇文章主要給大家介紹了關(guān)于如何使用C語言將數(shù)字、字符等數(shù)據(jù)寫入、輸出到文本文件中的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Objective-C中常用的結(jié)構(gòu)體NSRange,NSPoint,NSSize(CGSize),NSRect實例分析
這篇文章主要介紹了Objective-C中常用的結(jié)構(gòu)體NSRange,NSPoint,NSSize(CGSize),NSRect實例分析,有助于更加直觀的理解Object-C常用的結(jié)構(gòu)體,需要的朋友可以參考下2014-07-07