This repository has been archived by the owner on Apr 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 554
Examples
Simon Ninon edited this page Oct 7, 2016
·
11 revisions
#include <cpp_redis/cpp_redis>
#include <signal.h>
#include <iostream>
volatile std::atomic_bool should_exit(false);
cpp_redis::redis_client client;
void
sigint_handler(int) {
std::cout << "disconnected (sigint handler)" << std::endl;
client.disconnect();
should_exit = true;
}
int
main(void) {
client.connect("127.0.0.1", 6379, [] (cpp_redis::redis_client&) {
std::cout << "client disconnected (disconnection handler)" << std::endl;
should_exit = true;
});
// same as client.send({ "SET", "hello", "42" }, ...)
client.set("hello", "42", [] (cpp_redis::reply& reply) {
std::cout << reply.as_string() << std::endl;
});
// same as client.send({ "DECRBY", "hello", 12 }, ...)
client.decrby("hello", 12, [] (cpp_redis::reply& reply) {
std::cout << reply.as_integer() << std::endl;
});
// same as client.send({ "GET", "hello" }, ...)
client.get("hello", [] (cpp_redis::reply& reply) {
std::cout << reply.as_string() << std::endl;
});
// commands are pipelined and only sent when client.commit() is called
client.commit();
// synchronous commit, no timeout
// client.sync_commit();
// synchronous commit, timeout
// client.sync_commit(std::chrono::milliseconds(100));
signal(SIGINT, &sigint_handler);
while (not should_exit);
return 0;
}
#include <cpp_redis/cpp_redis>
#include <signal.h>
#include <iostream>
volatile std::atomic_bool should_exit(false);
cpp_redis::redis_subscriber sub;
void
sigint_handler(int) {
std::cout << "disconnected (sigint handler)" << std::endl;
sub.disconnect();
should_exit = true;
}
int
main(void) {
sub.connect("127.0.0.1", 6379, [](cpp_redis::redis_subscriber&) {
std::cout << "sub disconnected (disconnection handler)" << std::endl;
should_exit = true;
});
sub.subscribe("some_chan", [] (const std::string& chan, const std::string& msg) {
std::cout << "MESSAGE " << chan << ": " << msg << std::endl;
});
sub.psubscribe("*", [] (const std::string& chan, const std::string& msg) {
std::cout << "PMESSAGE " << chan << ": " << msg << std::endl;
});
sub.commit();
signal(SIGINT, &sigint_handler);
while (not should_exit);
return 0;
}
#include <cpp_redis/cpp_redis>
#include <iostream>
class my_logger : public cpp_redis::logger_iface {
public:
//! ctor & dtor
my_logger(void) = default;
~my_logger(void) = default;
//! copy ctor & assignment operator
my_logger(const my_logger&) = default;
my_logger& operator=(const my_logger&) = default;
public:
void debug(const std::string& msg, const std::string& file, unsigned int line) {
std::cout << "debug: " << msg << " @ " << file << ":" << line << std::endl;
}
void info(const std::string& msg, const std::string& file, unsigned int line) {
std::cout << "info: " << msg << " @ " << file << ":" << line << std::endl;
}
void warn(const std::string& msg, const std::string& file, unsigned int line) {
std::cout << "warn: " << msg << " @ " << file << ":" << line << std::endl;
}
void error(const std::string& msg, const std::string& file, unsigned int line) {
std::cerr << "error: " << msg << " @ " << file << ":" << line << std::endl;
}
};
int
main(void) {
//! By default, no logging
//! Force logger call, just for the example (you will never have to do that by yourself)
std::cout << "By default: no logging" << std::endl;
_CPP_REDIS_LOG(debug, "This is a debug message");
_CPP_REDIS_LOG(info, "This is an info message");
_CPP_REDIS_LOG(warn, "This is a warn message");
_CPP_REDIS_LOG(error, "This is an error message");
std::cout << std::endl;
//! Use the default logger, provided with the library
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);
//! Force logger call, just for the example (you will never have to do that by yourself)
std::cout << "With the library provided logger" << std::endl;
_CPP_REDIS_LOG(debug, "This is a debug message");
_CPP_REDIS_LOG(info, "This is an info message");
_CPP_REDIS_LOG(warn, "This is a warn message");
_CPP_REDIS_LOG(error, "This is an error message");
std::cout << std::endl;
//! Use your custom logger
cpp_redis::active_logger = std::unique_ptr<my_logger>(new my_logger);
//! Force logger call, just for the example (you will never have to do that by yourself)
std::cout << "With an example of custom logger" << std::endl;
_CPP_REDIS_LOG(debug, "This is a debug message");
_CPP_REDIS_LOG(info, "This is an info message");
_CPP_REDIS_LOG(warn, "This is a warn message");
_CPP_REDIS_LOG(error, "This is an error message");
std::cout << std::endl;
return 0;
}
Need more information? Contact me.