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
# Proxy Modegcc -o redis_example redis_example.c -lhiredis
Instance ID:Password format, and custom accounts use the Username@Password format. Instances with password-free authentication do not require a password.#include <stdio.h>#include <stdlib.h>#include <string.h>#include <hiredis/hiredis.h>int main() {// Connection parameters.const char *host = "192.xx.xx.2"; // Instance VIP addressint port = 6379; // Instance portconst char *password = "crs-xxxx:password"; // Default account password format: Instance ID:Passwordstruct timeval timeout = {2, 0}; // Connection timeout of 2 seconds// Establish a connection.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;}// AuthenticationredisReply *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);// Write data.reply = redisCommand(c, "SET name %s", "Distributed Cache");printf("SET result: %s\\n", reply->str);freeReplyObject(reply);// Read data.reply = redisCommand(c, "GET name");printf("GET name: %s\\n", reply->str);freeReplyObject(reply);// Set a Key with an expiration time.reply = redisCommand(c, "SETEX session:user123 3600 %s", "session_data");printf("SETEX result: %s\\n", reply->str);freeReplyObject(reply);// Hash operations.reply = redisCommand(c, "HSET user:1001 name %s age %s", "Zhang San", "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);// List operations.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);// Close the connection.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:password";const char *ca_cert = "/path/to/ca.pem";struct timeval timeout = {2, 0};// Initialize SSL.redisInitOpenSSL();// Create an SSL context.redisSSLContext *ssl_context = redisCreateSSLContext(ca_cert, // CA certificateNULL, // Client certificate (not required)NULL, // Client private key (not required)NULL, // Certificate passwordNULL, // Error messageNULL // Error message length);if (ssl_context == NULL) {printf("Failed to create SSL context\\n");return 1;}// Establish an SSL connection.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;}// Initialize the SSL connection.if (redisInitiateSSLWithContext(c, ssl_context) != REDIS_OK) {printf("SSL init error: %s\\n", c->errstr);redisFreeSSLContext(ssl_context);redisFree(c);return 1;}// AuthenticationredisReply *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);// Operate datareply = 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);// Clean up resources.redisFree(c);redisFreeSSLContext(ssl_context);return 0;}
Parameter | Description | Recommended Value |
host | Instance connection address. | - |
port | Instance port number | 6379 |
timeout | Connection timeout | 2 seconds |
password | Authentication password | Default account format: instance ID:password |
max_redirect | Maximum number of redirections | 3 |
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários