tencent cloud

TencentDB for Tendis

Release Notes
Product Introduction
Overview
Strengths
Use Cases
Product Series
Performance
Command Compatibility
Regions and AZs
Relevant Concepts
Relevant Products
Purchase Guide
Billing Overview
Pricing
Payment Overdue Policy
Getting Started
Creating Tendis Instances
Connecting to Tendis Instances
iptables Forwarding
Operation Guide
Instance Connection Using Programming Languages
Instance Maintenance and Management
Monitoring Features
Configuring Security Groups
Disabling Commands
FAQs
General
Purchase
Connection and Login
Contact Us
Tendis Policy
Privacy Policy
Data Privacy and Security Agreement
Glossary

C Connection Sample

PDF
Focus Mode
Font Size
Last updated: 2023-12-21 21:10:31
Preparations before running: Download and install hiredis.
Sample code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hiredis.h>

int main(int argc, char **argv) {
unsigned int j;
redisContext *c;
redisReply *reply;

if (argc < 4) {
printf("Usage: 192.xx.xx.195 6379 instance_id password\\n");
exit(0);
}
const char *hostname = argv[1];
const int port = atoi(argv[2]);
const char *instance_id = argv[3];
const char *password = argv[4];


struct timeval timeout = { 1, 500000 }; // 1.5 seconds
c = redisConnectWithTimeout(hostname, 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");
}
exit(1);
}

/* AUTH */
reply = redisCommand(c, "AUTH %s", password);
printf("AUTH: %s\\n", reply->str);
freeReplyObject(reply);

/* PING server */
reply = redisCommand(c,"PING");
printf("PING: %s\\n", reply->str);
freeReplyObject(reply);

/* Set a key */
reply = redisCommand(c,"SET %s %s", "name", "credis_test");
printf("SET: %s\\n", reply->str);
freeReplyObject(reply);

/* Try a GET */
reply = redisCommand(c,"GET name");
printf("GET name: %s\\n", reply->str);
freeReplyObject(reply);

/* Disconnects and frees the context */
redisFree(c);

return 0;
}
Execution results:


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback