Skip to content

Commit

Permalink
add coroutine support for sentinel
Browse files Browse the repository at this point in the history
  • Loading branch information
sewenew committed Jul 12, 2022
1 parent a02afd3 commit f06ab7a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2433,8 +2433,10 @@ auto async_redis = AsyncRedis(opts, pool_opts);
Future<string> ping_res = async_redis.ping();
// Async interface returning Future object.
Future<bool> set_res = async_redis.set("key", "val");
// Async interface with callback.
async_redis.set("key", "val",
[](Future<bool> &&fut) {
try {
Expand Down Expand Up @@ -2696,7 +2698,7 @@ fut.get();
### Coroutine Interface
*redis-plus-plus* also supports coroutine interface, however, coroutine support for Sentinel, Subscriber and Transaction is still on the way.
*redis-plus-plus* also supports coroutine interface, however, coroutine support for Subscriber and Transaction is still on the way.
#### Installation
Expand Down Expand Up @@ -2752,6 +2754,53 @@ cppcoro::sync_wait([&co_redis]() -> cppcoro::task<> {
}());
```
#### Redis Sentinel
Coroutine interface also supports Redis Sentinel.
```c++
#include <sw/redis++/co_redis++.h>
SentinelOptions sentinel_opts;
sentinel_opts.nodes = {
{"127.0.0.1", 8000},
{"127.0.0.1", 8001},
{"127.0.0.1", 8002}
};
sentinel_opts.connect_timeout = std::chrono::milliseconds(100);
sentinel_opts.socket_timeout = std::chrono::milliseconds(100);
auto sentinel = std::make_shared<CoSentinel>(sentinel_opts);
onnectionOptions connection_opts;
connection_opts.connect_timeout = std::chrono::milliseconds(100); // Required.
connection_opts.socket_timeout = std::chrono::milliseconds(100); // Required.
ConnectionPoolOptions pool_opts;
pool_opts.size = 3; // Optional. The default size is 1.
// Connect to master node.
CoRedis co_redis(sentinel, "mymaster", Role::MASTER, connection_opts, pool_opts);
// The following code randomly connects to one of the slave nodes.
// CoRedis co_redis(sentinel, "mymaster", Role::SLAVE, connection_opts, pool_opts);
cppcoro::sync_wait([&co_redis]() -> cppcoro::task<> {
try {
auto val = co_await co_redis.get("key");
if (val)
cout << *val << endl;
else
cout << "not exist" << endl;
} catch (const Error &e) {
cout << e.what() << endl;
}
}());
```
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`.
## Redis Recipes
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.
Expand Down
10 changes: 10 additions & 0 deletions src/sw/redis++/co_redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@
#include "async_redis.h"
#include "cxx_utils.h"
#include "cmd_formatter.h"
#include "async_sentinel.h"

namespace sw {

namespace redis {

using CoSentinel = AsyncSentinel;

class CoRedis {
public:
CoRedis(const ConnectionOptions &opts,
const ConnectionPoolOptions &pool_opts = {}) : _async_redis(opts, pool_opts) {}

CoRedis(const std::shared_ptr<CoSentinel> &sentinel,
const std::string &master_name,
Role role,
const ConnectionOptions &connection_opts,
const ConnectionPoolOptions &pool_opts = {}) :
_async_redis(sentinel, master_name, role, connection_opts, pool_opts) {}

CoRedis(const CoRedis &) = delete;
CoRedis& operator=(const CoRedis &) = delete;

Expand Down

0 comments on commit f06ab7a

Please sign in to comment.