redis windows 简单实现

1.git 下载redis  
https://github.com/MicrosoftArchive/redis.git

2.将项目添加到reids项目

3.参考RedisCli添加/修改引用

4.编译运行

 

注意:
不可使用 <string> 等头文件    会有冲突,那就纯c编程吧

附csdn源码下载地址:https://download.csdn.net/download/glc22/10755059

#include "redisConn.h"
#include <stdio.h> 
#include <malloc.h>
const char* ip = "127.0.0.1";
int port = 6379;
const char* auth = "test123";
const char* subscribe = "cplusplus";
const char* publish = "webServer";

redisConn::redisConn()
{
}


redisConn::~redisConn()
{
	if (!m_isCon)
		return;
	redisFree(m_conServer);
	redisFree(m_conClient);
} 

redisContext* redisConn::getConClient(const char* ip, int port, const char* auth)
{

	redisContext *conn = redisConnect(ip, port);

	if (conn != NULL && conn->err)

	{

		printf("connection error: %s\n", conn->errstr);

		return NULL;

	} 
	redisReply *reply = (redisReply*)redisCommand(conn, "auth %s", auth);

	printf("%s\n", reply->str);
	freeReplyObject(reply);

	m_isCon = true;
	return conn;
} 

void redisConn::sendMsg(char* msg)
{ 
	redisReply *replys = (redisReply*)redisCommand(m_conClient, "publish %s %s", publish, msg);
	freeReplyObject(replys); 
}

static void cliFormatReplyRaw(redisReply *r) { 
	size_t i; 
	switch (r->type) {
	case REDIS_REPLY_NIL:
		/* Nothing... */
		break;
	case REDIS_REPLY_ERROR:
		printf(r->str, r->len); 
		break;
	case REDIS_REPLY_STATUS:
	case REDIS_REPLY_STRING:
		printf(r->str, r->len);
		break;
	case REDIS_REPLY_INTEGER:
		printf("%lld", r->integer);
		break;
	case REDIS_REPLY_ARRAY:
		for (i = 0; i < r->elements; i++) {
			printf("\n");
			cliFormatReplyRaw(r->element[i]); 
		}
		break;
	default:
		fprintf(stderr, "Unknown reply type: %d\n", r->type); 
		break;
	} 
}

void redisConn::Init()
{  
	m_conServer = getConClient(ip, port, auth);
	m_conClient = getConClient(ip, port, auth);
	if (!m_isCon)
		return; 
	redisReply *reply = (redisReply*)redisCommand(m_conServer, "SUBSCRIBE %s", subscribe);
	freeReplyObject(reply); 
	void *_reply; 
	while (redisGetReply(m_conServer, &_reply) == REDIS_OK)
	{ 
		redisReply* reply = (redisReply*)_reply;
		cliFormatReplyRaw(reply); 
		freeReplyObject(reply); 

		sendMsg("I receive");

	}
	 getchar();
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值