VS2017的redis客户端实现

VS2017下Redis服务器源码地址
https://download.csdn.net/download/qq_23350817/88541316

VS2017下Redis客户端源码地址(hiredis已完成windows下编译):
https://download.csdn.net/download/qq_23350817/88541242

C代码实现:

#include  
#include  
#include  
#include  
#include  
#include  
#include 

void doTest()
{
	//redis默认监听端口为6387 可以再配置文件中修改 
	redisContext* c = redisConnect("10.10.0.7", 6379);
	if (c->err)
	{
		redisFree(c);
		printf("Connect to redisServer faile\n");
		return;
	}

	// 进行身份验证(用户名和密码)
	redisReply *reply = (redisReply *)redisCommand(c, "AUTH test123");  // 替换为你的 Redis 密码

	if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
		printf("Authentication failed: %s\n", reply ? reply->str : "NULL");
		freeReplyObject(reply);
		return;
	}
	freeReplyObject(reply);

	reply = redisCommand(c, "SELECT 0");
	if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
		printf("Error selecting database: %s\n", reply ? reply->str : "NULL");
		freeReplyObject(reply);
		redisFree(c);
		return;
	}
	


	const char* command = "get auth:third-party:token";
	redisReply* r = (redisReply*)redisCommand(c, command);
	r = (redisReply*)redisCommand(c, command);
	if (r->type != REDIS_REPLY_STRING)
	{
		printf("Failed to execute command[%s]\n", command);
		freeReplyObject(r);
		redisFree(c);
		return;
	}
	printf("The value of 'sunx' is %s\n", r->str);
	freeReplyObject(r);
	printf("Succeed to execute command[%s]\n", command);

	redisFree(c);

}

int main()
{
	doTest();
	return 0;
}

#include  
#include  
#include  
#include  
#include  
#include  
#include  

void doTest()
{
	//redis默认监听端口为6387 可以再配置文件中修改 
	redisContext* c = redisConnect("10.100.0.127", 6379);
	if (c->err)
	{
		redisFree(c);
		printf("Connect to redisServer faile\n");
		return;
	}
	printf("Connect to redisServer Success\n");

	const char* command1 = "set sunx 123456789";
	redisReply* r = (redisReply*)redisCommand(c, command1);

	if (NULL == r)
	{
		printf("Execut command1 failure\n");
		redisFree(c);
		return;
	}
	if (!(r->type == REDIS_REPLY_STATUS && strcmp(r->str, "OK") == 0))
	{
		printf("Failed to execute command[%s]\n", command1);
		freeReplyObject(r);
		redisFree(c);
		return;
	}
	freeReplyObject(r);
	printf("Succeed to execute command[%s]\n", command1);

	const char* command2 = "strlen sunx";
	r = (redisReply*)redisCommand(c, command2);
	if (r->type != REDIS_REPLY_INTEGER)
	{
		printf("Failed to execute command[%s]\n", command2);
		freeReplyObject(r);
		redisFree(c);
		return;
	}
	int length = r->integer;
	freeReplyObject(r);
	printf("The length of 'sunx' is %d.\n", length);
	printf("Succeed to execute command[%s]\n", command2);


	const char* command3 = "get sunx";
	r = (redisReply*)redisCommand(c, command3);
	if (r->type != REDIS_REPLY_STRING)
	{
		printf("Failed to execute command[%s]\n", command3);
		freeReplyObject(r);
		redisFree(c);
		return;
	}
	printf("The value of 'sunx' is %s\n", r->str);
	freeReplyObject(r);
	printf("Succeed to execute command[%s]\n", command3);

	const char* command4 = "get test";
	r = (redisReply*)redisCommand(c, command4);
	if (r->type != REDIS_REPLY_NIL)
	{
		printf("Failed to execute command[%s]\n", command4);
		freeReplyObject(r);
		redisFree(c);
		return;
	}
	printf("The value of 'test' is %s\n", r->str);
	freeReplyObject(r);
	printf("Succeed to execute command[%s]\n", command4);


	redisFree(c);

}

int main()
{
	doTest();
	return 0;
}

C++代码实现:

#include 
#include 
#include 

extern "C" {
#include 
}

#pragma comment(lib, "hiredis.lib")
#pragma comment(lib, "Win32_Interop.lib")

std::string GetToken()
{
	std::string strToken = "";
	std::string ipaddr = "10.100.0.127";
	int port = 6379;
	struct timeval timeout = { 5, 0 }; // 设置超时时间为 5 秒
	std::string password = "hlxredis";
	std::string setPasswdCmd = "AUTH " + password;
	std::string tokenKey = "auth:third-party:token";

	redisContext* c = redisConnectWithTimeout(ipaddr.c_str(), port, timeout);
	if (c->err)
	{
		redisFree(c);
		printf("Connect to redisServer faile\n");
		return strToken;
	}

	redisReply *reply = (redisReply *)redisCommand(c, setPasswdCmd.c_str());
	if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
		printf("Authentication failed: %s\n", reply ? reply->str : "NULL");
		freeReplyObject(reply);
		return strToken;
	}
	freeReplyObject(reply);

	std::string selectDbCmd = "SELECT 0";
	reply = (redisReply *)redisCommand(c, selectDbCmd.c_str());
	if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
		printf("Error selecting database: %s\n", reply ? reply->str : "NULL");
		freeReplyObject(reply);
		redisFree(c);
		return strToken;
	}
	
	std::string getTokenCmd = "get " + tokenKey;
	redisReply* r = (redisReply*)redisCommand(c, getTokenCmd.c_str());
	if (r->type != REDIS_REPLY_STRING)
	{
		printf("Failed to execute command[%s]\n", getTokenCmd.c_str());
		freeReplyObject(r);
		redisFree(c);
		return strToken;
	}
	strToken = r->str;
	printf("The value of 'sunx' is %s\n", r->str);
	freeReplyObject(r);
	printf("Succeed to execute command[%s]\n", getTokenCmd.c_str());

	redisFree(c);

	return strToken;
}

int main()
{
	std::cout << "token:" << GetToken() << std::endl;;
	return 0;
}

注意:C++代码实现时,VS2017可能会报错检测到“RuntimeLibrary”的不匹配项: 值“MT_StaticRelease”不匹配值“MD_DynamicRelease”,此时需要如下修改:
VS2017的redis客户端实现_第1张图片

运行结果如下:
在这里插入图片描述

你可能感兴趣的:(redis,bootstrap,数据库)