国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C基礎 redis緩存訪問詳解

2020-01-26 14:33:28
字體:
來源:轉載
供稿:網友

引言

先說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/hiredis

wget 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!/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

最終測試結果是

最終測試結果是


這里表明流程是跑通了. 這里擴展一下, 有時候在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

 運行結果如下

更加詳細介紹請參照 hiredis git上 源碼.

后記

 到這里關于C簡單使用控制redis服務器, 基本講完了. 錯誤是難免的. 歡迎指正.  

以上這篇C基礎 redis緩存訪問詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 寻乌县| 枣庄市| 射阳县| 绥中县| 武乡县| 邵东县| 固始县| 教育| 长寿区| 平谷区| 宁德市| 根河市| 新绛县| 漳浦县| 大宁县| 墨脱县| 齐河县| 莲花县| 尖扎县| 许昌市| 阿克陶县| 瑞丽市| 海阳市| 宜城市| 鄯善县| 恭城| 鹿邑县| 沙坪坝区| 安新县| 克什克腾旗| 犍为县| 丹江口市| 南开区| 蒙山县| 漯河市| 尼木县| 丁青县| 栾城县| 红河县| 遵义市| 沅江市|