@@ -9,82 +9,145 @@ Boost.asio based Redis-client header-only library. Simple but powerfull.
99
1010Get/set example:
1111
12- #include "redisclient.h"
13-
14- static const std::string redisKey = "unique-redis-key-example";
15- static const std::string redisValue = "unique-redis-value";
12+ #include <redisclient/redissyncclient.h>
1613
1714 int main(int, char **)
1815 {
19- const char * address = "127.0.0.1";
20- const int port = 6379;
16+ boost::asio::ip::address address = boost::asio::ip::address::from_string( "127.0.0.1") ;
17+ const unsigned short port = 6379;
2118
2219 boost::asio::io_service ioService;
23- RedisClient redis(ioService);
24-
25- if( !redis.connect(address, port) )
20+ RedisSyncClient redis(ioService);
21+ std::string errmsg;
22+
23+ if( !redis.connect(address, port, errmsg) )
2624 {
27- std::cerr << "Can't connecto to redis" << std::endl;
25+ std::cerr << "Can't connect to redis: " << errmsg << std::endl;
2826 return EXIT_FAILURE;
2927 }
3028
31- redis.command("SET", redisKey, redisValue, [&](const Value &v) {
32- std::cerr << "SET: " << v.toString() << std::endl;
29+ const std::string key = "unique-redis-key-example";
30+ const char value [] = "unique-redis-value";
31+
32+ redis.command("SET", key, value);
33+ RedisValue result = redis.command("GET", key);
34+
35+ std::cout << "SET " << key << ": " << value << "\n";
36+ std::cout << "GET " << key << ": " << result.toString() << "\n";
37+
38+ return EXIT_SUCCESS;
39+ }
40+
41+ Async get/set example:
42+
43+ #include <redisclient/redisasyncclient.h>
44+
45+ static const std::string redisKey = "unique-redis-key-example";
46+ static const std::string redisValue = "unique-redis-value";
3347
34- redis.command("GET", redisKey, [&](const Value &v) {
35- std::cerr << "GET: " << v.toString() << std::endl;
48+ void handleConnected(boost::asio::io_service &ioService, RedisAsyncClient &redis,
49+ bool ok, const std::string &errmsg)
50+ {
51+ if( ok )
52+ {
53+ redis.command("SET", redisKey, redisValue, [&](const RedisValue &v) {
54+ std::cerr << "SET: " << v.toString() << std::endl;
55+
56+ redis.command("GET", redisKey, [&](const RedisValue &v) {
57+ std::cerr << "GET: " << v.toString() << std::endl;
3658
37- redis.command("DEL", redisKey, [&](const Value &v) {
38- ioService.stop();
59+ redis.command("DEL", redisKey, [&](const RedisValue &) {
60+ ioService.stop();
61+ });
3962 });
4063 });
41- });
64+ }
65+ else
66+ {
67+ std::cerr << "Can't connect to redis: " << errmsg << std::endl;
68+ }
69+ }
70+
71+ int main(int, char **)
72+ {
73+ boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1");
74+ const unsigned short port = 6379;
75+
76+ boost::asio::io_service ioService;
77+ RedisAsyncClient redis(ioService);
78+
79+ redis.asyncConnect(address, port,
80+ boost::bind(&handleConnected, boost::ref(ioService), boost::ref(redis), _1, _2));
4281
4382 ioService.run();
4483
4584 return 0;
4685 }
4786
87+
4888
4989Publish/subscribe example:
5090
51- #include " redisclient.h"
91+ #include < redisclient/redisasyncclient.h>
5292
5393 static const std::string channelName = "unique-redis-channel-name-example";
5494
55- int main(int, char ** )
95+ void subscribeHandler(boost::asio::io_service &ioService, const std::vector< char> &buf )
5696 {
57- const char *address = "127.0.0.1";
58- const int port = 6379;
97+ std::string msg(buf.begin(), buf.end());
5998
60- boost::asio::io_service ioService;
61- RedisClient publisher( ioService);
62- RedisClient subscriber(ioService);
99+ if( msg == "stop" )
100+ ioService.stop( );
101+ }
63102
64- if( !publisher.connect(address, port) || !subscriber.connect(address, port) )
65- {
66- std::cerr << "Can't connecto to redis" << std::endl;
67- return EXIT_FAILURE;
68- }
103+ void publishHandler(RedisAsyncClient &publisher, const RedisValue &)
104+ {
105+ publisher.publish(channelName, "First hello", [&](const RedisValue &) {
106+ publisher.publish(channelName, "Last hello", [&](const RedisValue &) {
107+ publisher.publish(channelName, "stop");
108+ });
109+ });
110+ }
69111
70- subscriber.subscribe(channelName, [&](const std::string &msg) {
71- std::cerr << "Message: " << msg << std::endl;
112+ int main(int, char **)
113+ {
114+ boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1");
115+ const unsigned short port = 6379;
72116
73- if( msg == "stop" )
74- ioService.stop( );
75- } );
117+ boost::asio::io_service ioService;
118+ RedisAsyncClient publisher(ioService );
119+ RedisAsyncClient subscriber(ioService );
76120
77- publisher.publish(channelName, "First hello", [&](const Value &) {
78- publisher.publish(channelName, "Last hello", [&](const Value &) {
79- publisher.publish(channelName, "stop");
80- });
121+ publisher.asyncConnect(address, port, [&](bool status, const std::string &err)
122+ {
123+ if( !status )
124+ {
125+ std::cerr << "Can't connect to to redis" << err << std::endl;
126+ }
127+ else
128+ {
129+ subscriber.asyncConnect(address, port, [&](bool status, const std::string &err)
130+ {
131+ if( !status )
132+ {
133+ std::cerr << "Can't connect to to redis" << err << std::endl;
134+ }
135+ else
136+ {
137+ subscriber.subscribe(channelName,
138+ boost::bind(&subscribeHandler, boost::ref(ioService), _1),
139+ boost::bind(&publishHandler, boost::ref(publisher), _1));
140+ }
141+ });
142+ }
81143 });
82144
83145 ioService.run();
84146
85147 return 0;
86148 }
87149
150+
88151Also you can build the library like a shared library. Just use
89152 -DREDIS_CLIENT_DYNLIB and -DREDIS_CLIENT_BUILD to build redisclient
90153and -DREDIS_CLIENT_DYNLIB to build your project.
0 commit comments