Open
Description
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)?