C++ cppredis 使用

教程

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星点点-

请我喝杯咖啡呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值