首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C 語言操作 Redis:hiredis 客戶端庫安裝與基礎 API 實戰

     編輯:關於C語言

C 語言操作 Redis:hiredis 客戶端庫安裝與基礎 API 實戰

環境準備:Redis 服務端安裝(Ubuntu 16.04 示例)

wget https://download.redis.io/releases/redis-3.0.6.tar.gz
tar xzf redis-3.0.6.tar.gz
cd redis-3.0.6
make
# 啟動服務端
src/redis-server
# 新終端啟動客戶端測試
src/redis-cli
# 測試
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"

hiredis C 客戶端庫安裝

wget https://github.com/redis/hiredis/archive/master.zip
unzip master.zip
cd hiredis-master
make
sudo make install
# 安裝路徑:
# /usr/local/include/hiredis/  頭文件
# /usr/local/lib/libhiredis.so  動態庫
# /usr/local/lib/pkgconfig/hiredis.pc  pkg-config 支持

核心 API 速查

函數用途關鍵參數/返回
redisConnect(ip, port)建立連接返回 redisContext*,NULL 表失敗;ctx->err 非 0 表錯誤
redisCommand(ctx, fmt, ...)發送命令printf 風格格式化;返回 redisReply*,NULL 表失敗
freeReplyObject(reply)釋放回覆對象必須調用,否則洩漏
redisFree(ctx)關閉連接釋放上下文程序結束前調用

關鍵結構體定義

// 連接上下文
typedef struct redisContext {
    int err; char errstr[128];
    int fd; int flags;
    char *obuf;
    redisReader *reader;
    // ... tcp/unix_sock 信息
} redisContext;

// 命令回覆
typedef struct redisReply {
    int type;              // REDIS_REPLY_* 類型
    long long integer;     // 整數回覆
    int len; char *str;    // 字符串/錯誤/狀態回覆
    size_t elements;       // 數組元素數
    struct redisReply **element; // 數組元素指針陣列
} redisReply;

回覆類型常量(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

實戰示例 1:簡單 GET 操作

#include <stdio.h> #include <stdlib.h> #include <hiredis/hiredis.h>

int main() {
    redisContext *ctx = redisConnect("127.0.0.1", 6379);
    if (!ctx || ctx->err) {
        fprintf(stderr, "Connect error: %s\n", ctx ? ctx->errstr : "NULL context");
        if (ctx) redisFree(ctx);
        return EXIT_FAILURE;
    }

    redisReply *reply = redisCommand(ctx, "GET foo");
    if (reply && reply->type == REDIS_REPLY_STRING)
        printf("GET foo => %s\n", reply->str);
    else
        printf("Reply type: %d\n", reply ? reply->type : -1);

    freeReplyObject(reply);
    redisFree(ctx);
    return 0;
}

編譯:gcc -Wall -ggdb -o simpleget simpleget.c -lhiredis

GET 測試輸出

實戰示例 2:List 操作(LPUSH / LRANGE)

#include <signal.h>  // 忽略 SIGPIPE 防止連接斷開導致進程退出
signal(SIGPIPE, SIG_IGN);

redisContext *ctx = redisConnect("127.0.0.1", 6379);
// ... 錯誤檢查同上 ...

freeReplyObject(redisCommand(ctx, "LPUSH mylist foo"));
freeReplyObject(redisCommand(ctx, "LPUSH mylist bar"));

redisReply *reply = redisCommand(ctx, "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("LRANGE error: type=%d\n", reply ? reply->type : -1);
}
freeReplyObject(reply);
redisFree(ctx);
List 操作輸出

編譯與鏈接提示

  • 頭文件:#include <hiredis/hiredis.h>
  • 鏈接:-lhiredis(需 sudo ldconfig 刷新庫緩存)
  • 查找宏定義技巧:find /usr/local/include -name "*.h" | xargs grep REDIS_REPLY_STRING

進階主題預告

  • 異步 API(redisAsyncContext、redisAsyncCommand)
  • 連接池與管道(Pipeline)優化高併發
  • 發布/訂閱、事務(MULTI/EXEC)、Lua 腳本調用
  • 集群模式(Redis Cluster)客戶端路由
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved