redis ,hiredis, XREAD 使用代码实例

该代码示例展示了如何使用 Redis C 客户端库 hiredis 和 redisCli 处理 Redis 流(stream)消息,包括接收、删除和控制消息队列的大小。主要操作包括 `XDEL` 命令删除指定 ID 的消息,`XREAD` 命令获取消息,以及 `XTRIM` 命令限制消息队列的最大长度。
摘要由CSDN通过智能技术生成
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "hiredis.h"
#include "redisCli.h"


#define REDIS_STREAM_KEY       "setMessage" 
#define REDIS_STREAM_MAX_LEN   1024

static char redisSerIp[64] = {"127.0.0.1"};
static unsigned short redisSerPort = 6379;

static redisReply *redisStreamRceiveMsg(redisReply *reply)
{
	int i = 0;
	redisReply *childReply = NULL;
	redisReply *retReply = NULL;
	
	while(reply)
	{
		if(reply->type == REDIS_REPLY_ARRAY)
		{
			childReply = NULL;
			for (i = 0; i < reply->elements; i++)
			{
				childReply = reply->element[i];
			}
			
			if(childReply && childReply->element)
			{
				retReply = reply;
				reply = childReply;
			}	
			else
			{
				return retReply;
			}
		}
		else
		{
			return NULL;
		}
	}
	return NULL;
}




static void redisStreamDel(redisContext *context,char *cpId)
{
	redisReply *reply = NULL;
	
	if(context == NULL || cpId == NULL)
	{
		return ;
	}
	
	reply = redisCommand(context, "XDEL %s %s",REDIS_STREAM_KEY,cpId);
	if((reply == NULL) || (context->err))
	{
		redisPublishZlogPirnt(ZLOG_ERROR,"[ctl] redisCommand reply is error,XDEL %s %s",
				REDIS_STREAM_KEY,cpId);
		if(reply)
		{
			freeReplyObject(reply);	
		}
		return;
	}	
	freeReplyObject(reply);	
}



static redisReply *redisStreamRceiveMsgFormat(redisContext *context,redisReply *reply)
{
	int i = 0;
	redisReply *retReply = NULL;
	redisReply *childReply = NULL;
	
	retReply = redisStreamRceiveMsg(reply);
	if(retReply)
	{
		for (i = 0; i < retReply->elements; i++)
		{
			childReply = retReply->element[i];
			if ((childReply->type == REDIS_REPLY_STRING)
					&& (childReply->str))	
			{
				redisPublishZlogPirnt(ZLOG_DEBUG,"[ctl] redisStreamRceiveMsgFormat DEL STR = %s",
						childReply->str);
				redisStreamDel(context,childReply->str);
			}
		}
		
		if(childReply)
		{
			return childReply;
		}
	}
	return NULL;
}



static redisContext *redisConnInit(void )
{
	redisContext *context = NULL;
	redisReply *reply = NULL;
	
	context = redisConnect(redisSerIp, redisSerPort);  //链接本地127.0.0.1,端口为6379的redis
	if(context != NULL && context->err)
	{
		printf("[zlog] redisConnect failed,server : %s, port:%d , SUBSCRIBE channel:zlog\n",redisSerIp,redisSerPort);
		redisFree(context);
		return NULL;
	}	
	/*控制消息队列的大小*/
	reply = redisCommand(context, "XTRIM %s MAXLEN %d",REDIS_STREAM_KEY,REDIS_STREAM_MAX_LEN);
	if((reply == NULL) || (context->err))
	{
		redisPublishZlogPirnt(ZLOG_ERROR,"[ctl] redisCommand reply is error,XTRIM %s MAXLEN %d ",
				REDIS_STREAM_KEY,REDIS_STREAM_MAX_LEN);
		if(reply)
		{
			freeReplyObject(reply);	
		}				
		return NULL;
	}	
	freeReplyObject(reply);	
	return context;
}
			
			
			
int main(void )
{
	int i = 0;
	redisContext *context = NULL;
	redisReply *reply = NULL;
	redisReply *retReply = NULL;
	redisReply *childReply = NULL;
	
	redisPublishZlogPirnt(ZLOG_DEBUG,"[ctl] ctl main start ... ");	
	context = redisConnInit();
	if(!context)
	{
		return -1;
	}
	redisPublishZlogPirnt(ZLOG_DEBUG,"[ctl] redisConnInit ok ... ");	

	while(1)
	{
		reply = redisCommand(context, "XREAD block 1000 streams %s 0-0",REDIS_STREAM_KEY);
		if((reply == NULL) || (context->err))
		{
			redisPublishZlogPirnt(ZLOG_ERROR,"[ctl] redisCommand reply is error,XREAD block 1000 streams %s 0-0",
					REDIS_STREAM_KEY);
			if(reply)
			{
				freeReplyObject(reply);	
			}					
			continue;
		}

		redisPublishZlogPirnt(ZLOG_DEBUG,"[ctl] XREAD block 1000 streams %s $ ok ... ",REDIS_STREAM_KEY);
		retReply = redisStreamRceiveMsgFormat(context,reply);
		if(retReply)
		{
			for (i = 0; i < retReply->elements; i++)
			{ 
				childReply = retReply->element[i];
				if ((childReply->type == REDIS_REPLY_STRING)
						&& (childReply->str))	
				{
					printf("str = %s\r\n",childReply->str);
				}
			}				
		}
		freeReplyObject(reply);			
	}
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值