Javascript node.js用于缓存的同步请求
我需要在代码中解决一个与缓存API结果有关的问题。我有一些async.map,如下所示:Javascript node.js用于缓存的同步请求,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我需要在代码中解决一个与缓存API结果有关的问题。我有一些async.map,如下所示: for(var user of allUsers) { var requestList = fill(cache, locations, apiUrl, cacheMap); async.map(requestList, function(obj, callback) { // ... }, function(err, results) { // P
for(var user of allUsers) {
var requestList = fill(cache, locations, apiUrl, cacheMap);
async.map(requestList, function(obj, callback) {
// ...
}, function(err, results) {
// PUT RESULTS INTO A CACHE
});
}
函数fill
只是查看缓存中是否存在locations
中的位置,而不是为要运行的API创建请求URL
然而,我意识到缓存在我的方法中根本没有多大用处,因为我的代码将分派async.map,并在下一个循环迭代fill
时立即开始,这意味着缓存不会在用户的每次迭代中同步
如何确保用户的每个迭代都有上一个用户的缓存更新版本?我需要非常巧妙地利用有限的API调用,因此如果有重复的请求,我希望请求一次,然后在以后的请求中从缓存中提取该结果
我现在唯一的想法是发送一个同步请求,而不是一个async.map,但我知道这违背了node.js的设计
for(var user of allUsers) {
var requestList = fill(cache, locations, apiUrl, cacheMap);
// sync map instead
requestList.map(function(obj) {
var res = sync-request(obj)
// put request result into cache
});
// cont...
}
使用承诺代理和缓存API调用。批处理API请求和缓存结果非常简单。下面的代码模块将一个现有的
expensiveAPI
调用封装在一个Promise中,并将解析结果缓存60秒
// Existing module to call the expensive API
// using the standard callback pattern
var expensiveApi = require("./expensiveApi");
// Using bluebird promise library
// See http://bluebirdjs.com/docs/api-reference.html
var Promise = require("bluebird");
// Promisify the existing callback
expensiveApi = Promise.promisify(expensiveApi);
// Calls to the API will now return a Promise
// A cache for the Promises
var cache = {};
module.exports = function expensiveApiWithPromises(item) {
// Check whether a cached Promise already exists
if (cache[item]) {
// Return it to the caller
return cache[item];
}
// Promise is not in the cache
cache[item] = expensiveApi(item)
.then(function(result) {
// Promise has resolved
// set the result to expire
setTimeout(function() {
delete cache[item];
}, 60 * 1000); // 60 seconds expiry
// Return the result
return result;
})
.catch(function(err) {
// Promise rejected with an error
// Reset the cache item
delete cache[item];
// Propagate the error
throw err;
});
// Return the newly created cached Promise
return cache[item];
}
使用承诺代理和缓存API调用。批处理API请求和缓存结果非常简单。下面的代码模块将一个现有的
expensiveAPI
调用封装在一个Promise中,并将解析结果缓存60秒
// Existing module to call the expensive API
// using the standard callback pattern
var expensiveApi = require("./expensiveApi");
// Using bluebird promise library
// See http://bluebirdjs.com/docs/api-reference.html
var Promise = require("bluebird");
// Promisify the existing callback
expensiveApi = Promise.promisify(expensiveApi);
// Calls to the API will now return a Promise
// A cache for the Promises
var cache = {};
module.exports = function expensiveApiWithPromises(item) {
// Check whether a cached Promise already exists
if (cache[item]) {
// Return it to the caller
return cache[item];
}
// Promise is not in the cache
cache[item] = expensiveApi(item)
.then(function(result) {
// Promise has resolved
// set the result to expire
setTimeout(function() {
delete cache[item];
}, 60 * 1000); // 60 seconds expiry
// Return the result
return result;
})
.catch(function(err) {
// Promise rejected with an error
// Reset the cache item
delete cache[item];
// Propagate the error
throw err;
});
// Return the newly created cached Promise
return cache[item];
}
您可以使用来迭代
alluser
。这将按顺序逐步执行并保持异步
async.eachSeries(allUsers, (user, done) => {
const requestList = fill(cache, locations, apiUrl, cacheMap);
async.map(requestList, (obj, callback) => {
// ..
callback(null, requestResult);
}, (err, results) => {
// PUT RESULTS INTO A CACHE
done(null, results);
});
}, cb);
您可以使用来迭代
alluser
。这将按顺序逐步执行并保持异步
async.eachSeries(allUsers, (user, done) => {
const requestList = fill(cache, locations, apiUrl, cacheMap);
async.map(requestList, (obj, callback) => {
// ..
callback(null, requestResult);
}, (err, results) => {
// PUT RESULTS INTO A CACHE
done(null, results);
});
}, cb);
谢谢你的回答!提示一个
request()
调用是否也适用于此代码?在这一点上,我不是只想使用request-promise()
库并将与之链接起来吗?是的。如果您的API请求正在使用请求承诺
,那么您不需要承诺API请求,因为它已经将返回承诺。感谢您的回答!提示一个request()
调用是否也适用于此代码?在这一点上,我不是只想使用request-promise()
库并将与之链接起来吗?是的。如果您的API请求正在使用请求承诺
,那么您不需要承诺API请求,因为它已经将返回承诺。它是否保证按顺序运行该系列?谢谢是的,我们保证订货。如果您仅使用。每个,则不能保证它们将并行运行。是否保证按顺序运行系列?谢谢是的,我们保证订货。如果只使用。每个,则不能保证,因为它们将并行运行