Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to subscribe to keyspace changes and determine name of keys that changed? #63

Open
AlDanial opened this issue Aug 23, 2018 · 1 comment
Labels

Comments

@AlDanial
Copy link

I'm trying to write a program that subscribes to a keyspace change pattern. If it detects that a key has changed, I'd like to know which specific key it was. I began with redisclient-0.6.1/examples/async_pubsub2.cpp then simplified it to

#include <string>
#include <iostream>
#include <functional>
#include <boost/asio/ip/address.hpp>
#include <redisclient/redisasyncclient.h>

static const std::string channelName = "__keyspace@0__:x*";

class Client {
public:
    Client(boost::asio::io_service &ioService)
        : ioService(ioService) {}

    void onMessage(const std::vector<char> &buf) {
        std::string msg(buf.begin(), buf.end());
        std::cerr << "Message: " << msg << std::endl;
        if( msg == "stop" )
            ioService.stop();
    }

private:
    boost::asio::io_service &ioService;
};

int main(int, char **) {
    boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1");
    const unsigned short port = 6379;
    boost::asio::ip::tcp::endpoint endpoint(address, port);

    boost::asio::io_service ioService;
    redisclient::RedisAsyncClient subscriber(ioService);
    Client client(ioService);

    subscriber.connect(endpoint, [&](boost::system::error_code ec) {
        if( ec ) {
            std::cerr << "Can't connect to redis: " << ec.message() << std::endl;
        } else {
            subscriber.psubscribe(channelName,
                    std::bind(&Client::onMessage, &client, std::placeholders::_1)
                    );
        }
    });
    ioService.run();
    return 0;
}

which subscribes to changes to any key beginning with x. It half-works: when I change the value of key xy (manually, using redis-cli from another terminal), the onMessage() method in the program above is called, but msg just contains "set". But which key was set--xy or xz or .... ?

How should the code be changed so that it also captures the name(s) of the changed key(s)?

@nekipelov
Copy link
Owner

Currently there is no way. I'll do it later.

@nekipelov nekipelov added the todo label Sep 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants