docs/content/stable/yedis/develop/client-drivers/yedis/cpp.md
The tutorial assumes that you have:
We use the cpp_redis driver. To install the library do the following:
cpp_redis repository$ git clone https://github.com/Cylix/cpp_redis.git
$ cd cpp_redis
$ git submodule init && git submodule update
$ mkdir build && cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Release
$ make
$ make install
Create a file ybredis_hello_world.cpp and copy the contents below:
#include <cpp_redis/cpp_redis>
#include<iostream>
#include<vector>
#include<string>
#include<utility>
using namespace std;
int main() {
cpp_redis::client client;
client.connect("127.0.0.1", 6379, [](const std::string& host, std::size_t port, cpp_redis::client::connect_state status) {
if (status == cpp_redis::client::connect_state::dropped) {
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
});
string userid = "1";
vector<pair<string, string>> userProfile;
userProfile.push_back(make_pair("name", "John"));
userProfile.push_back(make_pair("age", "35"));
userProfile.push_back(make_pair("language", "Redis"));
// Insert the data
client.hmset(userid, userProfile, [](cpp_redis::reply& reply) {
cout<< "HMSET returned " << reply << ": id=1, name=John, age=35, language=Redis" << endl;
});
// Query the data
client.hgetall(userid, [](cpp_redis::reply& reply) {
std::vector<cpp_redis::reply> retVal;
if (reply.is_array()) {
retVal = reply.as_array();
}
cout << "Query result:" <<endl;
for (int i = 0; i < retVal.size(); i=i+2) {
cout << retVal[i] << "=" <<retVal[i+1] << endl;
}
});
// synchronous commit, no timeout
client.sync_commit();
return 0;
}
To compile the file, run the following command.
$ g++ -ltacopie -lcpp_redis -std=c++11 -o ybredis_hello_world ybredis_hello_world.cpp
To run the application, run the following command.
$ ./ybredis_hello_world
You should see the following output.
HMSET returned OK: id=1, name=John, age=35, language=Redis
Query result:
age=35
language=Redis
name=John