Created
July 15, 2015 08:35
-
-
Save sysroad/7c8adccb7d25c160b3eb to your computer and use it in GitHub Desktop.
redis rank test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment