【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存在