使用C++操作Redis5.0新特性Stream实现发布订阅

关于Redis5.0新出的这个stream数据结构的用法与介绍,可以看我上一篇关于Python操作Redis的client的介绍

借助这个发送订阅平台,我重构了公司的摇号短信发送的平台。通过合作方提供格式化的摇号中签信息,一次抓取之后便可以实现对于中签者发布摇号成功消息,对于未中签者发布摇号失败通知。涉及公司隐私,这里并不展开讲摇号订阅发送的代码逻辑,只分享使用的Redis client。

同样的,写了一个C++的client,功能要更健全些。github上也有一些client,但我下载了stars数最多的两个项目,发现都不是很适用我的项目,一个client不支持接受一个map的消息,一条消息里只能包含一个key和一个value,不支持嵌套消息的读取,局限性很大,因为摇号的信息必须包含地域,中奖编码等,也不适用于我的工程项目。

这个client包括了:发布一个map的消息,创建消费组,使用消费组消费消息,ACK和删除一条消息,获取嵌套消息并存储到用户指定的map里等订阅发布需要实现的功能,对于一些需要尝试多次的操作,支持传入重试次数。

GitHub链接在这里

//put a meassage map into specific channel
int RedisTool::Publish(const std::string channel,std::map<std::string, std::string> field_string_map)
{
	if (!pRedisContext) return -1;
	std::string field_string;
	std::string command = "XADD";
	std::string format = "*";

	int command_length = field_string_map.size();
	const char **commandList = new const char*[3+2*command_length];
	commandList[0] = command.c_str();
	commandList[1] = channel.c_str();
	commandList[2] = format.c_str();

	size_t *lengthList = new size_t[3+2*command_length];
	lengthList[0] = command.length();
	lengthList[1] = channel.length();
	lengthList[2] = format.length();

	int index = 3;
	for(std::map<std::string,std::string>::const_iterator iter = field_string_map.begin(); iter != field_string_map.end(); ++iter)
	{
		commandList[index] = i
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值