我正在尝试在我的应用程序和脚本之间共享一个对象(缓存对象)但没有成功。每天执行脚本以检索一些数据并更新缓存对象。应用程序使用此更新的共享对象后。
这是我每天执行的脚本代码
#!/usr/bin/env node
var async = require("async"),
_ = require("lodash"),
moment = require("moment"),
clone = require("clone");
var StoresManager = require("../app/lib/stores_manager"),
Logger = require("../app/lib/logger"),
TRAKT = require("../app/lib/trakt"),
CacheManager = require("../app/lib/cache_manager");
var WEB_LISTS = [],
MOBILE_LISTS = [];
async.waterfall(
[
...,
function (next) {
CacheManager.clear(function (err) { next(err); });
},
function (next) {
CacheManager.set("extended_web_browsing_list", WEB_LISTS, function (err) { next(err); });
},
function (next) {
CacheManager.set("extended_mobile_browsing_list", MOBILE_LISTS, function (err) { next(err); });
}
],
function (err) {
if ( err )
throw err;
Logger.info("DONE!");
process.exit(0);
}
);
我的缓存管理器模块
var NodeCache = require("node-cache");
var myCache = new NodeCache();
module.exports = myCache;
在需要时使用缓存的API端点。执行脚本时,在发出请求后,缓存对象为空。
...
var CacheManager = require("../../lib/cache_manager");
...
function retrieveWebBrowsingList(extended, response) {
var data = [];
async.waterfall(
[
function (next) {
if ( !extended )
return next();
Logger.debug(extended);
CacheManager.get("extended_web_browsing_list", function (err, value) {
Logger.debug(value);
if ( !value )
return next();
data = value;
next(true);
});
},
...
],
function (err) {
if ( err && typeof err !== "boolean" )
return Utils.handleError(err, response);
response.statusCode = httpStatus.OK;
response.data = data;
Logger.debug(response);
response.send();
}
);
}