Skip to content

Commit f06ab7a

Browse files
committed
add coroutine support for sentinel
1 parent a02afd3 commit f06ab7a

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2433,8 +2433,10 @@ auto async_redis = AsyncRedis(opts, pool_opts);
24332433
24342434
Future<string> ping_res = async_redis.ping();
24352435
2436+
// Async interface returning Future object.
24362437
Future<bool> set_res = async_redis.set("key", "val");
24372438
2439+
// Async interface with callback.
24382440
async_redis.set("key", "val",
24392441
[](Future<bool> &&fut) {
24402442
try {
@@ -2696,7 +2698,7 @@ fut.get();
26962698
26972699
### Coroutine Interface
26982700
2699-
*redis-plus-plus* also supports coroutine interface, however, coroutine support for Sentinel, Subscriber and Transaction is still on the way.
2701+
*redis-plus-plus* also supports coroutine interface, however, coroutine support for Subscriber and Transaction is still on the way.
27002702
27012703
#### Installation
27022704
@@ -2752,6 +2754,53 @@ cppcoro::sync_wait([&co_redis]() -> cppcoro::task<> {
27522754
}());
27532755
```
27542756
2757+
#### Redis Sentinel
2758+
2759+
Coroutine interface also supports Redis Sentinel.
2760+
2761+
```c++
2762+
#include <sw/redis++/co_redis++.h>
2763+
2764+
SentinelOptions sentinel_opts;
2765+
sentinel_opts.nodes = {
2766+
{"127.0.0.1", 8000},
2767+
{"127.0.0.1", 8001},
2768+
{"127.0.0.1", 8002}
2769+
};
2770+
2771+
sentinel_opts.connect_timeout = std::chrono::milliseconds(100);
2772+
sentinel_opts.socket_timeout = std::chrono::milliseconds(100);
2773+
2774+
auto sentinel = std::make_shared<CoSentinel>(sentinel_opts);
2775+
2776+
onnectionOptions connection_opts;
2777+
connection_opts.connect_timeout = std::chrono::milliseconds(100); // Required.
2778+
connection_opts.socket_timeout = std::chrono::milliseconds(100); // Required.
2779+
2780+
ConnectionPoolOptions pool_opts;
2781+
pool_opts.size = 3; // Optional. The default size is 1.
2782+
2783+
// Connect to master node.
2784+
CoRedis co_redis(sentinel, "mymaster", Role::MASTER, connection_opts, pool_opts);
2785+
2786+
// The following code randomly connects to one of the slave nodes.
2787+
// CoRedis co_redis(sentinel, "mymaster", Role::SLAVE, connection_opts, pool_opts);
2788+
2789+
cppcoro::sync_wait([&co_redis]() -> cppcoro::task<> {
2790+
try {
2791+
auto val = co_await co_redis.get("key");
2792+
if (val)
2793+
cout << *val << endl;
2794+
else
2795+
cout << "not exist" << endl;
2796+
} catch (const Error &e) {
2797+
cout << e.what() << endl;
2798+
}
2799+
}());
2800+
```
2801+
2802+
The coroutine support for sentinel is similar with the sync one, except that you need to create an `CoSentinel` object instead of a `Sentinel` object. Check [Redis Sentinel](#redis-sentinel) for more details on `SentinelOptions`, `ConnectionOptions` and `Role`.
2803+
27552804
## Redis Recipes
27562805
27572806
We can create many interesting data structures and algorithms based on Redis, such as [Redlock](https://redis.io/topics/distlock). We call these data structures and algorithms as **Redis Recipes**. *redis-plus-plus* will support some of these recipes.

src/sw/redis++/co_redis.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,26 @@
2121
#include "async_redis.h"
2222
#include "cxx_utils.h"
2323
#include "cmd_formatter.h"
24+
#include "async_sentinel.h"
2425

2526
namespace sw {
2627

2728
namespace redis {
2829

30+
using CoSentinel = AsyncSentinel;
31+
2932
class CoRedis {
3033
public:
3134
CoRedis(const ConnectionOptions &opts,
3235
const ConnectionPoolOptions &pool_opts = {}) : _async_redis(opts, pool_opts) {}
3336

37+
CoRedis(const std::shared_ptr<CoSentinel> &sentinel,
38+
const std::string &master_name,
39+
Role role,
40+
const ConnectionOptions &connection_opts,
41+
const ConnectionPoolOptions &pool_opts = {}) :
42+
_async_redis(sentinel, master_name, role, connection_opts, pool_opts) {}
43+
3444
CoRedis(const CoRedis &) = delete;
3545
CoRedis& operator=(const CoRedis &) = delete;
3646

0 commit comments

Comments
 (0)