sewenew / redis-plus-plus

Redis client written in C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide a way to iterate the nodes in a cluster shard so people can implement key scans on clusters

masariello opened this issue · comments

I would like to be able to SCAN keys in a cluster as in the below example code.

std::unordered_set<std::string> scan(std::string const& glob_expr)
{
  std::unordered_set<std::string> result;
  auto scan_node = [&](Redis& redis)
  {
    auto cursor = 0LL;
    do {
      cursor = redis.scan(cursor, glob_expr, [&](std::string const& k) { result.insert(k); });
    } while (cursor != 0);
  };

  ShardsPool shards_pool(...);
  for(auto const& slot : shards_pool->shards())
  {
    auto pool = shards_pool->fetch(slot.second());
    auto node = Redis(std::make_shared<GuardedConnection>(pool));
    scan_node(node));
  }
  return result;
}

The above uses the Redis::Redis(const GuardedConnectionSPtr &) which is private, so obviously not the cleanest approach.

A better way would be to add the following RedisCluster methods, please.

Shards RedisCluster::shards() const
{
  return pool_.shards();
}

ConnectionPoolSPtr RedisCluster::fetch(const Node & node)
{
  return pool_.fetch(node);
}

Thank you!

In fact, this feature is on the way. However, there's some problem with the async interface, and I'm considering how to do error handling elegantly. The interface is more or less like the following:

auto r = RedisCluster("redis://127.0.0.1:7000");
r.for_each([](Redis &r) {
                      // *r* is a Redis instance connecting a node, and you can do stuff with it for each node.
                 });

Regards

Looks lovely. The thinner the prettier. Thank you!

RedisCluster::for_each has been merged into the master branch. Please take a try. If you have any problem, feel free to let me know. for_each for AsyncRedisCluster is still on the way.

Regards