tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

C Connection Sample

ダウンロード
フォーカスモード
フォントサイズ
最終更新日: 2026-09-07 14:31:32
AI翻訳
This document provides an example of using a C client to connect to a Tencent Cloud Distributed Cache instance.

Preparations

Environment Requirement

GCC 4.8 or later.
CMake or Make build tool.

Installing a client

We recommend using hiredis (the official Redis C client) and hiredis-cluster (for cluster support):
Install hiredis.
git clone https://github.com/redis/hiredis.git
cd hiredis
make
sudo make install

Running Environment

Operating System: Ubuntu 24.04.3 LTS / x86_64

Runtime Version: GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)

Install hiredis-cluster.
git clone https://github.com/Nordix/hiredis-cluster.git
cd hiredis-cluster
mkdir build && cd build
cmake ..
make
sudo make install

Compilation and Linking

# Proxy Mode
gcc -o redis_example redis_example.c -lhiredis

Obtaining Connection Information

1. Log in to the Distributed Cache console.
2. In the instance list, click the target instance ID to go to the instance details page.
3. In the Network Information section, obtain the instance's VIP address and port (default: 6379).
4. In the Configuration Information section, obtain the connection password for the instance.
Default accounts use the Instance ID:Password format, and custom accounts use the Username@Password format. Instances with password-free authentication do not require a password.

Connection Example

In proxy mode, clients connect to instances through the unified VIP address provided by the proxy layer. The proxy automatically handles request routing and failover, so clients do not need to be aware of backend topology changes.

Basic Connection Example for hiredis

#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 address
int port = 6379; // Instance port
const char *password = "crs-xxxx:password"; // Default account password format: Instance ID:Password

struct 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;
}

// Authentication
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);

// 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;
}

SSL Encryption Connection Example

#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 certificate
NULL, // Client certificate (not required)
NULL, // Client private key (not required)
NULL, // Certificate password
NULL, // Error message
NULL // 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;
}

// Authentication
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);

// Operate data
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);

// Clean up resources.
redisFree(c);
redisFreeSSLContext(ssl_context);
return 0;
}

Connection Parameter Description

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

FAQs

Connection Timeout

Confirm that the CVM hosting the client and the distributed cache database instance are in the same VPC.
Confirm that the security group rule has allowed access to the corresponding port.
Confirm that the connection address and port are correct.

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック