Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.

Commit d5fa6f0

Browse files
MuggleWeiCylix
authored andcommitted
[3.5.4] add zadd command (#93)
* add zadd * clang-format * remove commented zadd unimplemented function.
1 parent 9de087a commit d5fa6f0

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

examples/redis_client.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ main(void) {
7171
// do_something_with_string(reply.as_string())
7272
});
7373

74+
client.zadd("zhello", {}, {{"1", "a"},
75+
{"2", "b"},
76+
{"3", "c"},
77+
{"4", "d"}},
78+
[](cpp_redis::reply& reply) {
79+
std::cout << "zadd zhello 1 a 2 b 3 c 4 d: " << reply << std::endl;
80+
});
81+
7482
// commands are pipelined and only sent when client.commit() is called
7583
// client.commit();
7684

includes/cpp_redis/network/tcp_client_iface.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
#pragma once
2424

25+
#include <cstdint>
2526
#include <functional>
2627
#include <string>
2728
#include <vector>
28-
#include <cstdint>
2929

3030
namespace cpp_redis {
3131

includes/cpp_redis/redis_client.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <atomic>
2626
#include <condition_variable>
2727
#include <functional>
28+
#include <map>
2829
#include <mutex>
2930
#include <queue>
3031
#include <string>
@@ -261,7 +262,7 @@ class redis_client {
261262
redis_client& unwatch(const reply_callback_t& reply_callback = nullptr);
262263
redis_client& wait(int numslaves, int timeout, const reply_callback_t& reply_callback = nullptr);
263264
redis_client& watch(const std::vector<std::string>& keys, const reply_callback_t& reply_callback = nullptr);
264-
// redis_client& zadd(const reply_callback_t& reply_callback = nullptr) key [nx|xx] [ch] [incr] score member [score member ...]
265+
redis_client& zadd(const std::string& key, const std::vector<std::string> options, const std::map<std::string, std::string> score_members, const reply_callback_t& reply_callback = nullptr);
265266
redis_client& zcard(const std::string& key, const reply_callback_t& reply_callback = nullptr);
266267
redis_client& zcount(const std::string& key, int min, int max, const reply_callback_t& reply_callback = nullptr);
267268
redis_client& zcount(const std::string& key, double min, double max, const reply_callback_t& reply_callback = nullptr);

sources/redis_client.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,23 @@ redis_client::watch(const std::vector<std::string>& keys, const reply_callback_t
13381338
return *this;
13391339
}
13401340

1341+
redis_client&
1342+
redis_client::zadd(const std::string& key, const std::vector<std::string> options, const std::map<std::string, std::string> score_members, const reply_callback_t& reply_callback) {
1343+
std::vector<std::string> cmd = {"ZADD", key};
1344+
1345+
//! options
1346+
cmd.insert(cmd.end(), options.begin(), options.end());
1347+
1348+
//! score members
1349+
for (auto& sm : score_members) {
1350+
cmd.push_back(sm.first);
1351+
cmd.push_back(sm.second);
1352+
}
1353+
1354+
send(cmd, reply_callback);
1355+
return *this;
1356+
}
1357+
13411358
redis_client&
13421359
redis_client::zcard(const std::string& key, const reply_callback_t& reply_callback) {
13431360
send({"ZCARD", key}, reply_callback);

0 commit comments

Comments
 (0)