sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

parse async scan command reply #407

Closed dkd1111 closed 2 years ago

dkd1111 commented 2 years ago

AsyncRedis doesn't support "scan" command. Its redisReply should be checked recusively if array type.

sewenew commented 2 years ago

@dkd1111 There's no need to do the change, instead, reply::parse can handle scan result with a proper template type. You can try the following code for how to do scan with async interface.

void scan(AsyncRedis &r, long long cursor = 0) {
    using ScanResult = std::pair<std::string, std::vector<std::string>>;
    r.command<ScanResult>("scan", cursor, [&r](Future<ScanResult> &&fut) {
                try {
                    auto res = fut.get();
                    const auto &keys = res.second;
                    // process scan results.

                    auto next_cursor = std::stoll(res.first);    // Redis return cursor as a string, instead of an integer.
                    if (next_cursor != 0) {
                        scan(r, next_cursor);
                    }
                } catch (const Error &e) {
                    // handle error
                }
            });
}

B.T.W. Thanks for the pull request :)

Regards

dkd1111 commented 2 years ago

I see, thanks for comments. :)