教程
https://blog.csdn.net/calmreason/article/details/54881014
cpp_redis采用延迟执行的方式,get set 都是把数据追加到client的成员buffer里,等commit的时候一起执行。
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())
});
############################ exists #######################################
vector<string> keys = { "hello"};//这里只能写一个key(如果写多余一个key会返回错误信息)
client.exists(keys, fun_call_back);
keys.push_back("key2");//keys = hello hello1
client.exists(keys, fun_call_back);//这时候发送的命令是错误的,所以返回错误信息,原因见上文
vector<pair<string, string>> vecPair = { { "key1", "value1" },{ "key2", "value2" },{ "key3", "value3" } };
client.hmset("MyHashSet", vecPair);
client.hexists("MyHashSet", "key1", fun_call_back);//查询hash中的一个key是否存在
client.sync_commit();
########################### 异常捕获 #####################################
try{
}
catch (cpp_redis::redis_error e)
{
std::cout << e.what() << endl;
}
######################### hash 增删改查 ################################
//新增:批量
vector<pair<string, string>> vecPair = { { "key1", "value1" },{ "key2", "value2" },{ "key3", "value3" } };
client.hmset("MyHashSet", vecPair, [](cpp_redis::reply& _reply) {
std::cout<<"新增批量:key1 key2 key3. " << _reply << endl;
});
//新增:单个
client.hset("MyHashSet", "key4", "value4", [](cpp_redis::reply& _reply) {
std::cout << "新增单个:key4. " << _reply << endl;
});
//查询:批量
client.hgetall("MyHashSet", [](cpp_redis::reply& _reply)//这种查询适合批量返回结果的情形,避免了来回的网络传输
{
std::cout << "查询批量:MyHashSet" << endl;
for each (auto& var in _reply.as_array())//这种方式返回的key 和value是挨着放的,因为是getall,所以不存在nill的情况(只要返回的vector的size>0)
{
std::cout << var << endl;
}
});
//修改:单个
client.hset("MyHashSet", "key4", "VALUE4", [](cpp_redis::reply& _reply) {
std::cout << "修改单个:MyHashSet key4 VALUE4. " << _reply << endl;
});
//查询:单个
client.hget("MyHashSet", "key4", [](cpp_redis::reply& _reply)
{
std::cout << "查询单个:MyHashSet key4. " << _reply << endl;
});
//删除:批量
vector<string> vecKeys = { "key1", "key2", "key_not_exist" };//删除不存在的key不会有任何影响,对客户端来说
client.hdel("MyHashSet", vecKeys, [](cpp_redis::reply& _reply) {
std::cout << "删除:批量:MyHashSet : key1, key2, key_not_exist. " << _reply << endl;
});
############################# send万能指令 ############################################
//zadd rank 11 baidu.com 8 jingdong.com 10 google.com.1
std::vector<std::string> my_command = {"zadd", "rank1", "11", "baidu.com", "8", "jingdong.com", "10", "google.com" };
client.send(my_command, [&](cpp_redis::reply& _reply)
{
std::cout << "zadd:finished!" << endl;
});
原文链接:https://blog.csdn.net/calmreason/article/details/82954780