#!/usr/bin/env python
#-*- coding: utf-8 -*-
import redis
#分别将host和port的值替换为实例的连接地址、端口号。
host = 'r-bp10noxlhcoim2****.redis.rds.aliyuncs.com'
port = 6379
#将pwd的值替换为实例的密码。
pwd = 'testaccount:Rp829dlwa'
r = redis.StrictRedis(host=host, port=port, password=pwd)
#连接建立后即可执行数据库操作,下述代码为您提供数据结构模块(如TairString)的使用示例。
print(r.execute_command('CAS foo bar bzz'))
print(r.execute_command('CAD foo bzz'))
print(r.execute_command('EXSET foo 200 VER 1'))
r.execute_command('EXSET foo 300 VER 10')
except:
print("The attached version is different from the server version, the operation will fail. ")
print(r.execute_command('EXCAS foo 300 1'))
redisReply *reply;
if (argc < 4) {
printf("Usage: example r-bp10noxlhcoim2****.redis.rds.aliyuncs.com 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", "foo", "hello world");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key using binary safe API */
reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
printf("SET (binary API): %s\n", reply->str);
freeReplyObject(reply);
/* Try a GET and two INCR */
reply = redisCommand(c,"GET foo");
printf("GET foo: %s\n", reply->str);
freeReplyObject(reply);
reply = redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* again ... */
reply = redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* Create a list of numbers, from 0 to 9 */
reply = redisCommand(c,"DEL mylist");
freeReplyObject(reply);
for (j = 0; j < 10; j++) {
char buf[64];
snprintf(buf,64,"%d",j);
reply = redisCommand(c,"LPUSH mylist element-%s", buf);
freeReplyObject(reply);
/* Let's check what we have inside the list */
reply = redisCommand(c,"LRANGE mylist 0 -1");
if (reply->type == REDIS_REPLY_ARRAY) {
for (j = 0; j < reply->elements; j++) {
printf("%u) %s\n", j, reply->element[j]->str);
freeReplyObject(reply);
/* Disconnects and frees the context */
redisFree(c);
return 0;
#include <iostream>
#include <string>
#include <string.h>
#include <hiredis/hiredis.h>
using namespace std;
int main(int argc, char **argv) {
unsigned int j;
redisContext *c;
redisReply *reply;
if (argc < 3) {
printf("Usage: example r-bp10noxlhcoim2****.redis.rds.aliyuncs.com 6379 password\n");
exit(0);
const char *hostname = argv[1];
const int port = atoi(argv[2]);
const char *password = argv[3];
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 = (redisReply *)redisCommand(c, "AUTH %s", password);
printf("AUTH: %s\n", reply->str);
freeReplyObject(reply);
/* PING server */
reply = (redisReply *)redisCommand(c,"PING");
printf("PING: %s\n", reply->str);
freeReplyObject(reply);
/* 下述代码为您提供数据结构模块(如TairString)的使用方法 */
reply = (redisReply *)redisCommand(c,"SET foo bar");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
reply = (redisReply *)redisCommand(c,"CAS foo bar bzz");
printf("CAS: %lld\n", reply->integer);
freeReplyObject(reply);
reply = (redisReply *)redisCommand(c,"CAD foo bzz");
printf("CAD: %lld\n", reply->integer);
freeReplyObject(reply);
/* TairString exstrtype */
reply = (redisReply *)redisCommand(c,"EXSET foo 200 VER 1");
printf("EXSET: %s\n", reply->str);
freeReplyObject(reply);
/* The attached version is different from the server version, the operation will fail */
reply = (redisReply *)redisCommand(c,"EXSET foo 300 VER 10");
printf("EXSET: %s\n", reply->str);
freeReplyObject(reply);
/* Compare the specified version to update the value, and the update is successful
when the version in the engine is the same as the specified one */
reply = (redisReply *)redisCommand(c,"EXCAS foo 300 1");
if (reply->type == REDIS_REPLY_ARRAY) {
/* ["OK", "", version], The middle value is an empty string, meaningless when successful */
for (j = 0; j < reply->elements; j++) {
printf("%u) %s\n", j, reply->element[j]->str);
freeReplyObject(reply);
/* Disconnects and frees the context */
redisFree(c);
return 0;
读写分离架构,且需要执行切换或选择数据库的操作(即使用多数据库功能),您必须先将
cluster_compat_enable参数设置为
0(即关闭原生Redis Cluster语法兼容),然后重启客户端应用,否则将提示报错:
Multiple databases are not supported on this server; cannot switch to database。具体操作,请参见
设置实例参数。
var redis = require("redis");
// 分别将host和port的值替换为实例的连接地址和端口。
var client = redis.createClient({host : 'r-bp10noxlhcoim2****.redis.rds.aliyuncs.com', port : 6379});
// 设置实例的密码。
client.auth("testaccount:Rp829dlwa", redis.print)
client.set("foo", "bar");
client.get("foo", function (err, reply) {
if (err) {
console.log(err);
} else {
console.log(reply.toString()); // print `OK`
// 如果传入一个Buffer,返回也是一个Buffer。
client.get(new Buffer("foo"), function (err, reply) {
if (err) {
console.log(err);
} else {
console.log(reply.toString()); // print `<Buffer 4f 4b>`
// 下述代码为您提供数据结构模块(如TairString)的使用示例。
client.sendCommand('CAS', ['foo', 'bar', 'bzz'], function (err, reply) {
if (err) {
console.log(err);
} else {
console.log('CAS : %s', reply.toString());
client.sendCommand('CAD', ['foo', 'bzz'], function (err, reply) {
if (err) {
console.log(err);
} else {
console.log('CAD : %s', reply.toString());
client.sendCommand('EXSET', ['foo', '200', 'VER', '1'], function (err, reply) {
if (err) {
console.log(err);
} else {
console.log('EXSET : %s', reply.toString());
client.sendCommand('EXSET', ['foo', '300', 'VER', '10'], function (err, reply) {
if (err) {
console.log(err);
} else {
console.log('EXSET : %s', reply.toString());
client.sendCommand('EXCAS', ['foo', '300', '1'], function (err, reply) {
if (err) {
console.log(err);
} else {
console.log('EXCAS : %s', reply.toString()); // print `<Buffer 4f 4b>`
client.quit();
读写分离架构,且需要执行切换或选择数据库的操作(即使用多数据库功能),您必须先将
cluster_compat_enable参数设置为
0(即关闭原生Redis Cluster语法兼容),然后重启客户端应用,否则将提示报错:
RedisCommandException: Multiple databases are not supported on this server; cannot switch to database: 1。具体操作,请参见
设置实例参数。