xRedis API  1.5.0
The C++ Client API for Redis server
 All Classes
demo_slice.cpp

This is an example of how to use the xRedis.
Connect to a redis cluster which contains three redis node
More details about this example.

/*
* ----------------------------------------------------------------------------
* Copyright (c) 2013-2021, xSky <guozhw at gmail dot com>
* All rights reserved.
* Distributed under GPL license.
* ----------------------------------------------------------------------------
*/
#include "xRedisClient.h"
#include <cstdio>
using namespace xrc;
enum {
CACHE_TYPE_1,
CACHE_TYPE_2,
CACHE_TYPE_MAX,
};
/* 配置一个3分片存储xRedis集群:共3个存储主节点 */
RedisNode RedisList1[3] = {
{ .dbindex = 0, .host = "127.0.0.1", .port = 6379, .passwd = "", .poolsize = 4, .timeout = 5, .role = MASTER },
{ .dbindex = 1, .host = "127.0.0.2", .port = 6379, .passwd = "", .poolsize = 4, .timeout = 5, .role = MASTER },
{ .dbindex = 2, .host = "127.0.0.3", .port = 6379, .passwd = "", .poolsize = 4, .timeout = 5, .role = MASTER }
};
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
xRedisClient xRedis;
xRedis.Init(CACHE_TYPE_MAX);
xRedis.ConnectRedisCache(RedisList1, sizeof(RedisList1) / sizeof(RedisNode),
3, CACHE_TYPE_1);
const char* key = "test";
const char* value = "test value";
SliceIndex index(&xRedis, CACHE_TYPE_1);
bool bRet = index.Create(key);
if (!bRet) {
return 0;
}
bRet = xRedis.set(index, key, value);
if (bRet) {
printf("success \r\n");
} else {
printf("error [%s] \r\n", index.GetErrInfo());
}
std::string strValue;
bRet = xRedis.get(index, key, strValue);
if (bRet) {
printf("%s \r\n", strValue.c_str());
} else {
printf("error [%s] \r\n", index.GetErrInfo());
}
return 0;
}

Generated on Mon Mar 21 2022 10:57:13 for xRedis API version 1.5.0.