acl/lib_acl_cpp/samples/redis/redis_key/redis_key.cpp

238 lines
4.9 KiB
C++
Raw Normal View History

#include "stdafx.h"
2015-01-14 23:35:23 +08:00
static acl::string __keypre("test_key");
static bool test_del(acl::redis_key& redis, int n)
2015-01-14 23:35:23 +08:00
{
acl::string key;
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
redis.clear();
int ret = redis.del_one(key.c_str());
2015-01-14 23:35:23 +08:00
if (ret < 0)
{
printf("del key: %s error: %s\r\n",
key.c_str(), redis.result_error());
2015-02-02 16:50:06 +08:00
return false;
2015-01-14 23:35:23 +08:00
}
else if (i < 10)
2016-04-01 09:36:18 +08:00
printf("del ok, key: %s, ret: %d\r\n", key.c_str(), ret);
2015-01-14 23:35:23 +08:00
}
2015-02-02 16:50:06 +08:00
return true;
2015-01-14 23:35:23 +08:00
}
static bool test_expire(acl::redis_key& redis, int n)
2015-01-14 23:35:23 +08:00
{
acl::string key;
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
redis.clear();
if (redis.expire(key.c_str(), 100) < 0)
2015-01-14 23:35:23 +08:00
{
printf("expire key: %s error: %s\r\n",
key.c_str(), redis.result_error());
2015-02-02 16:50:06 +08:00
return false;
2015-01-14 23:35:23 +08:00
}
else if (i < 10)
printf("expire ok, key: %s\r\n", key.c_str());
2015-01-14 23:35:23 +08:00
}
2015-02-02 16:50:06 +08:00
return true;
2015-01-14 23:35:23 +08:00
}
static bool test_ttl(acl::redis_key& redis, int n)
2015-01-14 23:35:23 +08:00
{
acl::string key;
int ttl;
2015-01-14 23:35:23 +08:00
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
redis.clear();
if ((ttl = redis.ttl(key.c_str())) < 0)
2015-01-14 23:35:23 +08:00
{
2021-08-17 11:00:51 +08:00
printf("get ttl key: %s error: %s, ttl=%d\r\n",
key.c_str(), redis.result_error(), ttl);
2015-02-02 16:50:06 +08:00
return false;
2015-01-14 23:35:23 +08:00
}
else if (i < 10)
printf("ttl ok, key: %s, ttl: %d\r\n",
key.c_str(), ttl);
2015-01-14 23:35:23 +08:00
}
2015-02-02 16:50:06 +08:00
return true;
2015-01-14 23:35:23 +08:00
}
static bool test_exists(acl::redis_key& redis, int n)
2015-01-14 23:35:23 +08:00
{
acl::string key;
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
redis.clear();
if (redis.exists(key.c_str()) == false)
2015-01-14 23:35:23 +08:00
printf("no exists key: %s\r\n", key.c_str());
else
printf("exists key: %s\r\n", key.c_str());
2015-01-14 23:35:23 +08:00
}
2015-02-02 16:50:06 +08:00
return true;
2015-01-14 23:35:23 +08:00
}
static bool test_type(acl::redis_key& redis, int n)
2015-01-14 23:35:23 +08:00
{
acl::string key;
for (int i = 0; i < n; i++)
{
key.format("%s_%d", __keypre.c_str(), i);
redis.clear();
acl::redis_key_t ret = redis.type(key.c_str());
2015-04-17 09:42:28 +08:00
if (ret == acl::REDIS_KEY_NONE)
2015-01-14 23:35:23 +08:00
{
printf("unknown type key: %s\r\n", key.c_str());
2015-02-02 16:50:06 +08:00
return false;
2015-01-14 23:35:23 +08:00
}
else
printf("type ok, key: %s, ret: %d\r\n",
key.c_str(), ret);
2015-01-14 23:35:23 +08:00
}
2015-02-02 16:50:06 +08:00
return true;
2015-01-14 23:35:23 +08:00
}
2022-12-16 22:32:12 +08:00
static bool test_keys_pattern(acl::redis_key& redis, const char* pattern)
{
std::vector<acl::string> values;
int ret = redis.keys_pattern(pattern, &values);
printf("ret=%d, pattern=%s\n", ret, pattern);
if (ret < 0) {
printf("keys_pattern error=%s, pattern=%s\r\n",
redis.result_error(), pattern);
return false;
} else if (ret == 0) {
printf("Not match pattern=%s\r\n", pattern);
return true;
}
printf("The matched values:\r\n");
for (std::vector<acl::string>::const_iterator cit = values.begin();
cit != values.end(); ++cit) {
printf(" %s\r\n", (*cit).c_str());
}
return true;
}
2015-01-14 23:35:23 +08:00
static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n"
"-s redis_addr[127.0.0.1:6379]\r\n"
2015-01-14 23:35:23 +08:00
"-n count\r\n"
"-C connect_timeout[default: 10]\r\n"
"-T rw_timeout[default: 10]\r\n"
2015-08-26 20:45:40 +08:00
"-c [use cluster mode]\r\n"
2022-12-16 22:32:12 +08:00
"-a cmd[del|expire|ttl|exists|type|all|keys_pattern]\r\n"
"-p pattern\r\n",
2015-01-14 23:35:23 +08:00
procname);
}
int main(int argc, char* argv[])
{
int ch, n = 1, conn_timeout = 10, rw_timeout = 10;
2022-12-16 22:32:12 +08:00
acl::string addr("127.0.0.1:6379"), cmd, pattern("none*");
2015-08-26 20:45:40 +08:00
bool cluster_mode = false;
2015-01-14 23:35:23 +08:00
2022-12-16 22:32:12 +08:00
while ((ch = getopt(argc, argv, "hs:n:C:T:a:cp:")) > 0)
2015-01-14 23:35:23 +08:00
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'n':
n = atoi(optarg);
break;
case 'C':
conn_timeout = atoi(optarg);
break;
case 'T':
rw_timeout = atoi(optarg);
break;
case 'a':
cmd = optarg;
break;
2022-12-16 22:32:12 +08:00
case 'p':
pattern = optarg;
break;
2015-08-26 20:45:40 +08:00
case 'c':
cluster_mode = true;
break;
2015-01-14 23:35:23 +08:00
default:
break;
}
}
acl::acl_cpp_init();
2015-08-26 20:45:40 +08:00
acl::redis_client_cluster cluster;
cluster.set(addr.c_str(), 100, conn_timeout, rw_timeout);
2015-08-26 20:45:40 +08:00
2015-01-14 23:35:23 +08:00
acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
2015-08-26 20:45:40 +08:00
acl::redis_key redis;
if (cluster_mode)
redis.set_cluster(&cluster);
2015-08-26 20:45:40 +08:00
else
redis.set_client(&client);
2015-01-14 23:35:23 +08:00
2015-02-02 16:50:06 +08:00
bool ret;
2015-01-14 23:35:23 +08:00
if (cmd == "del")
ret = test_del(redis, n);
2015-01-14 23:35:23 +08:00
else if (cmd == "expire")
ret = test_expire(redis, n);
2015-01-14 23:35:23 +08:00
else if (cmd == "ttl")
ret = test_ttl(redis, n);
2015-01-14 23:35:23 +08:00
else if (cmd == "exists")
ret = test_exists(redis, n);
2015-01-14 23:35:23 +08:00
else if (cmd == "type")
ret = test_type(redis, n);
2022-12-16 22:32:12 +08:00
else if (cmd == "keys_pattern")
ret = test_keys_pattern(redis, pattern);
else if (cmd == "all")
{
ret = test_expire(redis, n)
&& test_ttl(redis, n)
&& test_exists(redis, n)
&& test_type(redis, n)
&& test_del(redis, n);
}
2015-01-14 23:35:23 +08:00
else
2015-02-02 16:50:06 +08:00
{
ret = false;
2015-01-14 23:35:23 +08:00
printf("unknown cmd: %s\r\n", cmd.c_str());
2015-02-02 16:50:06 +08:00
}
if (ret == true)
printf("test OK!\r\n");
else
printf("test failed!\r\n");
2015-01-14 23:35:23 +08:00
#ifdef WIN32
printf("enter any key to exit\r\n");
getchar();
#endif
2015-01-14 23:35:23 +08:00
return 0;
}