我是节点模块,我需要解析数据,我想在不同的模块中共享这个解析的属性。第一个调用它的模块负责传递数据,其他模块不需要发送数据,因为我已经存储了parsedData(在cacheObj中)并且可以使用刚刚获取的任何属性,问题是当我从1个模块访问并提供数据然后尝试从diff模块访问时,缓存对象不包含我“存储”的数据,任何想法如何正确吗?
var _ = require('lodash');
var Promise = require('bluebird');
var actionUtil = require('sails/lib/hooks/blueprints/actionUtil');
var takeAliases = _.partial(_.pluck, _, 'alias');
var populateAliases = function (model, alias) {
return model.populate(alias);
};
module.exports = function (req, res) {
_.set(req.options, 'criteria.blacklist', ['limit', 'skip', 'sort', 'populate', 'fields']);
var fields = req.param('fields') ? req.param('fields').replace(/ /g, '').split(',') : [];
var populate = req.param('populate') ? req.param('populate').replace(/ /g, '').split(',') : [];
var Model = actionUtil.parseModel(req);
var where = actionUtil.parseCriteria(req);
var limit = actionUtil.parseLimit(req);
var skip = actionUtil.parseSkip(req);
var sort = actionUtil.parseSort(req);
var findQuery = _.reduce(_.intersection(populate, takeAliases(Model.associations)), populateAliases, Model.find().where(where).limit(limit).skip(skip).sort(sort));
var countQuery = Model.count(where);
Promise.all([findQuery, countQuery])
.spread(function (_records, _count) {
var records = fields.length > 0 ? _.map(_records, _.partial(_.pick, _, fields)) : _records;
return [records, null, null, {
criteria: where,
limit: limit,
start: skip,
end: skip + limit,
total: _count
}];
})
.spread(res.ok)
.catch(res.serverError);
};
我的节点应用程序的数据应该相同,所以我不需要每次都传递它...只是为了“init”......
答案 0 :(得分:1)
使用单例对象,
下面的基本示例var Singleton = (function () {
var instance;
function createInstance() {
var object = new Object("I am the instance");
return object;
}
return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
在您的情况下,使用相同的方法
"use strict";
var Parser = require('myParser'),
_ = require('lodash');
var cacheObj; // <-- singleton, will hold value and will not be reinitialized on myParser function call
function myParser(data) {
if (!(this instanceof myParser)) return new myParser(data);
if (!_.isEmpty(cacheObj)) { //remove `this`
this.parsedData = cacheObj; //remove `this`
} else {
this.parsedData = Parser.parse(data);
cacheObj = this.parsedData; //remove `this`
}
}
myParser.prototype = {
//remove `this.cacheObj`
getPropOne: function () {
return this.parsedData.propOne;
},
getPropTwo: function () {
return this.parsedData.propTwo;
}
};
module.exports = myParser;
使用memory-cache,不要忘记安装
"use strict";
var Parser = require('myParser'),
_ = require('lodash');
var cache = require('memory-cache');
function myParser(data) {
if (!(this instanceof myParser)) return new myParser(data);
var cache_data = cache.get('foo');
if (!_.isEmpty(cache_data)) {
this.parsedData = JSON.parse(cache_data);
} else {
this.parsedData = Parser.parse(data);
cache.put('foo', JSON.stringify(this.parsedData));
}
}
myParser.prototype = {
getPropOne: function () {
return this.parsedData.propOne;
},
getPropTwo: function () {
return this.parsedData.propTwo;
}
};
module.exports = myParser;