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 555
Examples
Simon Ninon edited this page Oct 16, 2016
·
11 revisions
#include <cpp_redis/cpp_redis>
#include <iostream>
int
main(void) {
//! Enable logging
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);
cpp_redis::redis_client client;
client.connect("127.0.0.1", 6379, [](cpp_redis::redis_client&) {
std::cout << "client disconnected (disconnection handler)" << std::endl;
});
// same as client.send({ "SET", "hello", "42" }, ...)
client.set("hello", "42", [](cpp_redis::reply& reply) {
std::cout << "set hello 42: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});
// same as client.send({ "DECRBY", "hello", 12 }, ...)
client.decrby("hello", 12, [](cpp_redis::reply& reply) {
std::cout << "decrby hello 12: " << reply << std::endl;
// if (reply.is_integer())
// do_something_with_integer(reply.as_integer())
});
// same as client.send({ "GET", "hello" }, ...)
client.get("hello", [](cpp_redis::reply& reply) {
std::cout << "get hello: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});
// 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));
return 0;
}
#include <cpp_redis/cpp_redis>
#include <iostream>
#include <signal.h>
volatile std::atomic_bool should_exit(false);
void
sigint_handler(int) {
should_exit = true;
}
int
main(void) {
//! Enable logging
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);
cpp_redis::redis_subscriber sub;
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 (!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.