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)境是

那我們測試一下. 安裝結果. 先啟動 redis-server 服務器.

再啟動 redis-cli 客戶端

我們開始測試一下.

測試之后一切正常. redis linux上安裝基本完畢了. 更加詳細的參照
Redis 官網(wǎng)教程 很詳細 http://www.redis.net.cn/tutorial/3501.html
前言
現(xiàn)在我們安裝 redis c 訪問的驅動. 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
本質對于 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 驅動已經(jīng)安裝完畢. 后面解釋一下, 驅動提供的api.
常用的 api如下.
/* * redis鏈接函數(shù), 返回redis上下文. * ip : 鏈接地址的ip * port : 鏈接端口 * : 返回 redis上下文, NULL表示獲取失敗 */ redisContext *redisConnect(const char *ip, int port) /* * 執(zhí)行redis操作命令, 返回得到的結果集 * context : redisConnect 返回的redis上下文對象 * format : 等同于 printf格式控制符 * ... : 后面可變參數(shù), 需要和 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網(wǎng)絡緩存服務器內存.
*/
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
最終測試結果是
最終測試結果是

這里表明流程是跑通了. 這里擴展一下, 有時候在Linux上查找函數(shù)或宏定義聲明好麻煩. 我用的方式是
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
通過這些宏枚舉區(qū)分返回的值. 其實到這里基本上 關于 redis接口使用基本入門了. 后面再舉一個 操作list的操作代碼 setlist.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <hiredis/hiredis.h>
/*
* 請求 redis網(wǎng)絡緩存服務器內存.
*/
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 鏈接對象創(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
運行結果如下

更加詳細介紹請參照 hiredis git上 源碼.
后記
到這里關于C簡單使用控制redis服務器, 基本講完了. 錯誤是難免的. 歡迎指正.
以上這篇C基礎 redis緩存訪問詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 【Redis緩存機制】詳解Java連接Redis_Jedis_事務
- Python的Flask框架使用Redis做數(shù)據(jù)緩存的配置方法
- CI框架中redis緩存相關操作文件示例代碼
- windows環(huán)境下Redis+Spring緩存實例講解
- Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)
- Redis整合Spring結合使用緩存實例
- 圖文詳解Windows下使用Redis緩存工具的方法
- spring結合redis如何實現(xiàn)數(shù)據(jù)的緩存
- PHP使用redis實現(xiàn)統(tǒng)計緩存mysql壓力的方法
- php操作redis緩存方法分享
- Redis緩存詳解
相關文章
C語言編程銀行ATM存取款系統(tǒng)實現(xiàn)源碼
這篇文章主要為大家介紹了C語言編程銀行ATM存取款系統(tǒng)實現(xiàn)的源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11
Cocos2d-x學習筆記之CCLayerColor層的使用實例
這篇文章主要介紹了Cocos2d-x學習筆記之CCLayerColor層的使用實例,CCLayerColor是一個顏色布景層類,本文依然使用Hello World作為例子講解,需要的朋友可以參考下2014-09-09
Matlab實現(xiàn)讀寫txt文件數(shù)據(jù)與進制轉換
這篇文章主要為大家詳細介紹了Matlab實現(xiàn)讀寫txt文件數(shù)據(jù)與進制轉換的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12
C/C++中CJSON的使用(創(chuàng)建與解析JSON數(shù)據(jù))
cJSON是一個超輕巧的JSON解析器,本文主要介紹了C/C++中CJSON的使用(創(chuàng)建與解析JSON數(shù)據(jù)),具有一定的參考價值,感興趣的可以了解一下2021-09-09

