git clone https://github.com/redis/hiredis.gitcd hiredismakesudo make install
Operating System: Ubuntu 24.04.3 LTS / x86_64
Runtime Version: GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
git clone https://github.com/Nordix/hiredis-cluster.gitcd hiredis-clustermkdir build && cd buildcmake ..makesudo make install
# プロキシモードgcc -o redis_example redis_example.c -lhiredis
インスタンスID:パスワード形式を使用し、カスタムアカウントはユーザー名@パスワード形式を使用します。パスワード不要の認証インスタンスはパスワードが不要です。#include <stdio.h>#include <stdlib.h>#include <string.h>#include <hiredis/hiredis.h>int main() {接続パラメータconst char *host = "192.xx.xx.2"; // インスタンスVIPアドレスint port = 6379; // インスタンスポートconst char *password = "crs-xxxx:パスワード"; // デフォルトアカウントのパスワード形式:インスタンスID:パスワードstruct timeval timeout = {2, 0}; // 接続タイムアウト2秒// 接続を確立します。redisContext *c = redisConnectWithTimeout(host, port, timeout);if (c == NULL || c->err) {if (c) {printf("Connection error: %s\\n", c->errstr);redisFree(c);} else {printf("Connection error: can't allocate redis context\\n");}return 1;}// 認証redisReply *reply = redisCommand(c, "AUTH %s", password);if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {printf("Auth failed: %s\\n", reply ? reply->str : "unknown error");freeReplyObject(reply);redisFree(c);return 1;}freeReplyObject(reply);// データを書き込みます。reply = redisCommand(c, "SET name %s", "Distributed Cache");printf("SET result: %s\\n", reply->str);freeReplyObject(reply);// データを読み取ります。reply = redisCommand(c, "GET name");printf("GET name: %s\\n", reply->str);freeReplyObject(reply);// 有効期限付きのKeyを設定します。reply = redisCommand(c, "SETEX session:user123 3600 %s", "session_data");printf("SETEX result: %s\\n", reply->str);freeReplyObject(reply);// ハッシュ操作reply = redisCommand(c, "HSET user:1001 name %s age %s", "張三", "28");printf("HSET result: %lld\\n", reply->integer);freeReplyObject(reply);reply = redisCommand(c, "HGET user:1001 name");printf("HGET name: %s\\n", reply->str);freeReplyObject(reply);// リスト操作reply = redisCommand(c, "LPUSH queue:tasks %s %s %s", "task1", "task2", "task3");printf("LPUSH result: %lld\\n", reply->integer);freeReplyObject(reply);reply = redisCommand(c, "RPOP queue:tasks");printf("RPOP: %s\\n", reply->str);freeReplyObject(reply);// 接続を閉じます。redisFree(c);return 0;}
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <hiredis/hiredis.h>#include <hiredis/hiredis_ssl.h>int main() {const char *host = "192.xx.xx.2";int port = 6379;const char *password = "crs-xxxx:パスワード";const char *ca_cert = "/path/to/ca.pem";struct timeval timeout = {2, 0};// SSLを初期化します。redisInitOpenSSL();// SSLコンテキストを作成します。redisSSLContext *ssl_context = redisCreateSSLContext(ca_cert, // CA証明書NULL, // クライアント証明書(不要)NULL, // クライアント秘密鍵(不要)NULL, // 証明書パスワードNULL, // エラーメッセージNULL // エラーメッセージ長);if (ssl_context == NULL) {printf("Failed to create SSL context\\n");return 1;}// SSL接続を確立します。redisContext *c = redisConnectWithTimeout(host, port, timeout);if (c == NULL || c->err) {printf("Connection error: %s\\n", c ? c->errstr : "unknown");redisFreeSSLContext(ssl_context);if (c) redisFree(c);return 1;}// SSL接続を初期化します。if (redisInitiateSSLWithContext(c, ssl_context) != REDIS_OK) {printf("SSL init error: %s\\n", c->errstr);redisFreeSSLContext(ssl_context);redisFree(c);return 1;}// 認証redisReply *reply = redisCommand(c, "AUTH %s", password);if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {printf("Auth failed: %s\\n", reply ? reply->str : "unknown");if (reply) freeReplyObject(reply);redisFreeSSLContext(ssl_context);redisFree(c);return 1;}freeReplyObject(reply);// データを操作します。reply = redisCommand(c, "SET ssl_test %s", "encrypted_connection");printf("SET: %s\\n", reply->str);freeReplyObject(reply);reply = redisCommand(c, "GET ssl_test");printf("GET: %s\\n", reply->str);freeReplyObject(reply);// リソースをクリーンアップします。redisFree(c);redisFreeSSLContext(ssl_context);return 0;}
パラメータ | 説明 | 推奨値 |
host | インスタンス接続アドレス。 | - |
port | インスタンスポート番号 | 6379 |
timeout | 接続タイムアウト時間 | 2秒 |
password | 認証パスワード | デフォルトアカウント形式: インスタンスID:パスワード |
max_redirect | 最大リダイレクト回数 | 3 |
フィードバック