redis rank test
#include <hiredis\hiredis.h>
#include "json\json.h"
#include <vector>
#pragma comment(lib, "hiredis.lib")
#pragma comment(lib, "Win32_Interop.lib")
#pragma comment(lib, "lua.lib")
#pragma comment(lib, "json_vc71_libmtd.lib")
int _tmain(int argc, _TCHAR* argv[])
{
timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
Json::Reader jr;
redisContext * pRedis = redisConnectWithTimeout("127.0.0.1", 6379, tv);
void * rep = nullptr;
if (pRedis && pRedis->err == REDIS_OK)
{
rep = redisCommand(pRedis, "auth 1234");
freeReplyObject(rep);
for (int i = 0; i < 100; ++i)
{
Json::StyledWriter w;
char buff[128] = { 0, };
sprintf_s(buff, "Tobi_%03d", i);
Json::Value v;
v["Name"] = buff;
v["Score"] = i;
rep = redisCommand(pRedis, "ZADD pvp %d %s", i, w.write(v).c_str());
freeReplyObject(rep);
}
rep = redisCommand(pRedis, "ZREVRANGE pvp 0 100");
redisReply* r = (redisReply*) rep;
struct User
{
std::string name;
int score;
};
std::vector<User> vUser;
for (size_t i = 0; i < r->elements; ++i)
{
char * str = r->element[i]->str;
Json::Value v;
if (false == jr.parse(str, v))
continue;
Json::Value name = v["Name"];
Json::Value score = v["Score"];
User u;
u.name = name.asString();
u.score = score.asInt();
vUser.push_back(u);
}
freeReplyObject(rep);
for (auto user : vUser)
{
printf("name : %s , score : %d\n", user.name.c_str(), user.score);
if (user.name.length() == 0)
{
rep = redisCommand(pRedis, "ZDEL pvp %s", user.name.c_str());
}
}
}
else if (pRedis)
{
fprintf(stderr, "ERROR : %s", pRedis->errstr);
}
return 0;
}