javascript - Kafka消费者onmessage - api调用对每条消息进行多次调用?
问题描述
考虑以下:
const cached = this.cache.get(somekey);
// console.log(!cached);
if (!cached) {
console.log(cached);
return requestRetry({
url: fullUri,
json: true,
maxAttempts: 3, // (default) try 5 times
retryDelay: 2000, // (default) wait for 5s before trying again
retryStrategy: requestRetry.RetryStrategies.HTTPOrNetworkError,
fullResponse: true
}).then((result) => {
//do something with the result and return result
this.cache.set(somekey, data);
return result;
}
} else {
//return cached result
}
上面的代码在 kafka 流消费者的 onmessage 方法中被调用。我面临的问题是,如果结果不是并且缓存未定义,它会为每个传入的 kafka 消息多次调用 url。
怎样才能做到只对第一个消息进行一次调用,它设置缓存,然后在下一条消息上它进入 else 部分并从缓存中检索数据。
解决方案
只需将requestRetry
结果承诺放入缓存中。
当它被解析时,缓存将更新为确切的结果。
推荐阅读
- docker - 超时异常 Flink
- excel - 将用户表单值插入特定列中的活动行
- python - 我的 Flask Container 和 Ganache Container 之间没有连接
- python - 了解 mdptoolbox 森林示例的参数值
- javascript - 从 url 获取(使用 javascript)json(使用 nodejs 发布)的问题
- javascript - Azure AppInsights 默认 TelemetryInitializer - Javascript SDK
- cmd - 在命令提示符下检查每个输出行的字符串中的子字符串
- meteor - 将数组元素迭代为集合文档(Meteor)中的单个值
- gcc - 如何限制 GCC 中的自动矢量化级别?
- django - Django:AttributeError:类型对象'GroupModel'没有属性'_meta'