关于Redis5.0新出的这个stream数据结构的用法与介绍,可以看我上一篇关于Python操作Redis的client的介绍。
借助这个发送订阅平台,我重构了公司的摇号短信发送的平台。通过合作方提供格式化的摇号中签信息,一次抓取之后便可以实现对于中签者发布摇号成功消息,对于未中签者发布摇号失败通知。涉及公司隐私,这里并不展开讲摇号订阅发送的代码逻辑,只分享使用的Redis client。
同样的,写了一个C++的client,功能要更健全些。github上也有一些client,但我下载了stars数最多的两个项目,发现都不是很适用我的项目,一个client不支持接受一个map的消息,一条消息里只能包含一个key和一个value,不支持嵌套消息的读取,局限性很大,因为摇号的信息必须包含地域,中奖编码等,也不适用于我的工程项目。
这个client包括了:发布一个map的消息,创建消费组,使用消费组消费消息,ACK和删除一条消息,获取嵌套消息并存储到用户指定的map里等订阅发布需要实现的功能,对于一些需要尝试多次的操作,支持传入重试次数。
//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