【6】cpp_redis exists

【1】cpp_redis (Windows C++ Redis客户端)(C++11实现)官方最新源码编译
正文如下

该函数检查key是否存在

如果存在:返回的reply为整数类型,值为1,否则为0

代码:

#include <cpp_redis/cpp_redis>
#include <iostream>
#include "TestRedis.h"
using namespace std;

#ifdef _WIN32
#include <Winsock2.h>
#endif /* _WIN32 */
#pragma comment( lib, "ws2_32.lib")//最好的方法是包含在项目属性中,因为那样可以根据Debug、Release、x86、x64来区分。这里仅仅是为了突出引用了这个库写在这里 

bool StartWSA(void)
{
	//! Windows netword DLL init
	WORD version = MAKEWORD(2, 2);
	WSADATA data;

	if (WSAStartup(version, &data) != 0)
	{
		std::cerr << "WSAStartup() failure" << std::endl;
		return false;
	}
	return true;
}
void StopWSA(void)
{
	WSACleanup();
}

typedef std::function<void(cpp_redis::reply&)> reply_callback_t;
void fun_call_back(cpp_redis::reply& _reply)
{
	std::cout << _reply << endl;
}

int main(void)
{
	//启动Windows网络通信库
	if (!StartWSA())
	{
		return -1;
	}
	try
	{
		//! 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;
		});

		client.set("hello", "Hello World!");
		client.set("hello1", "Hello World!");
		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是否存在

		// synchronous commit, no timeout
		client.sync_commit();
	}
	catch (cpp_redis::redis_error e)
	{
		std::cout << e.what() << endl;
	}


	//关闭Windows网络通信库
	StopWSA();

	return 0;
}

输出:hello存在,hash中的key1存在


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值