Commit c75df219798da3859274fb5f7b8fedbec19daffa

Authored by Hayk Martirosyan
1 parent 518a29c1

Start of looped command implementation

examples/basic.cpp
... ... @@ -18,7 +18,7 @@ unsigned long time_ms() {
18 18  
19 19 int main(int argc, char* argv[]) {
20 20  
21   - redisx::Redis r(REDIS_HOST, REDIS_PORT);
  21 + redisx::Redis r = {REDIS_HOST, REDIS_PORT};
22 22  
23 23 r.command<const string&>("GET blah", [](const string& cmd, const string& value) {
24 24 cout << "[COMMAND] " << cmd << ": " << value << endl;
... ... @@ -45,28 +45,31 @@ int main(int argc, char* argv[]) {
45 45 cout << "num deleted: " << num_deleted << endl;
46 46 });
47 47  
48   - unsigned long t0 = time_ms();
49   - unsigned long t1 = t0;
50   -
51   - int len = 1000000;
52   - int count = 0;
53   -
54   - for(int i = 0; i < len; i++) {
55   - r.command<const string&>("set blah wefoiwef", [&t0, &t1, &count, len](const string& cmd, const string& reply) {
56   -
57   - count++;
58   - if(count == len) {
59   - cout << cmd << ": " << reply << endl;
60   - cout << "Time to queue async commands: " << t1 - t0 << "ms" << endl;
61   - cout << "Time to receive all: " << time_ms() - t1 << "ms" << endl;
62   - cout << "Total time: " << time_ms() - t0 << "ms" << endl;
63   - }
64   - });
65   - }
66   - t1 = time_ms();
  48 + r.command_loop("LPUSH count 1", 0, 1);
  49 + r.command_loop("LPUSH count2 1", 0, 1);
  50 +
  51 +// unsigned long t0 = time_ms();
  52 +// unsigned long t1 = t0;
  53 +//
  54 +// int len = 1000000;
  55 +// int count = 0;
  56 +//
  57 +// for(int i = 0; i < len; i++) {
  58 +// r.command<const string&>("set blah wefoiwef", [&t0, &t1, &count, len](const string& cmd, const string& reply) {
  59 +//
  60 +// count++;
  61 +// if(count == len) {
  62 +// cout << cmd << ": " << reply << endl;
  63 +// cout << "Time to queue async commands: " << t1 - t0 << "ms" << endl;
  64 +// cout << "Time to receive all: " << time_ms() - t1 << "ms" << endl;
  65 +// cout << "Total time: " << time_ms() - t0 << "ms" << endl;
  66 +// }
  67 +// });
  68 +// }
  69 +// t1 = time_ms();
67 70  
68 71 thread loop([&r] { r.start(); });
69 72 loop.join();
70 73  
71 74 return 0;
72   -}
  75 +};
... ...
src/redisx.cpp
... ... @@ -54,6 +54,9 @@ void Redis::start() {
54 54 event_base_dispatch(base);
55 55 }
56 56  
  57 +
  58 +// ----------------------------
  59 +
57 60 void Redis::command(const char* cmd) {
58 61 int status = redisAsyncCommand(c, NULL, NULL, cmd);
59 62 if (status != REDIS_OK) {
... ... @@ -64,6 +67,36 @@ void Redis::command(const char* cmd) {
64 67  
65 68 // ----------------------------
66 69  
  70 +void e_callback(evutil_socket_t fd, short what, void *arg) {
  71 +
  72 + const char* cmd = "LPUSH count 1";
  73 +
  74 + redisAsyncContext* c = (redisAsyncContext*)arg;
  75 +
  76 + int status = redisAsyncCommand(c, NULL, NULL, cmd);
  77 + if (status != REDIS_OK) {
  78 + cerr << "[ERROR] Async command \"" << cmd << "\": " << c->errstr << endl;
  79 + return;
  80 + }
  81 +}
  82 +
  83 +struct event* Redis::command_loop(const char* cmd, long interval_s, long interval_us) {
  84 +
  85 + struct event* e;
  86 + if(interval_s == 0 && interval_us == 0) {
  87 + e = event_new(base, -1, EV_PERSIST, e_callback, c);
  88 + event_add(e, NULL);
  89 + } else {
  90 + struct timeval e_time = {interval_s, interval_us};
  91 + e = event_new(base, -1, EV_TIMEOUT | EV_PERSIST, e_callback, c);
  92 + event_add(e, &e_time);
  93 + }
  94 +
  95 + return e;
  96 +}
  97 +
  98 +// ----------------------------
  99 +
67 100 template<>
68 101 void invoke_callback(const CommandAsync<const redisReply*>* cmd_obj, redisReply* reply) {
69 102 cmd_obj->invoke(reply);
... ...
src/redisx.hpp
... ... @@ -29,6 +29,8 @@ public:
29 29  
30 30 void command(const char* command);
31 31  
  32 + struct event* command_loop(const char* command, long interval_s, long interval_us);
  33 +
32 34 void get(const char* key, std::function<void(const std::string&, const char*)> callback);
33 35  
34 36 void set(const char* key, const char* value);
... ... @@ -105,4 +107,5 @@ void Redis::command(const std::string&amp; cmd, const std::function&lt;void(const std::
105 107 }
106 108 }
107 109  
  110 +
108 111 } // End namespace redis
... ...