考虑以下:
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
结果承诺放入缓存中。
解析后,将使用确切结果更新缓存。