从 diff 模块访问模块数据
Access to module data from diff module
我有节点模块,我需要解析数据,我想在不同的modules.the第一个模块中共享这个解析的属性,调用这个模块负责传递数据,而其他模块不负责需要发送数据,因为我已经存储了 parsedData(在 cacheObj 中)并且可以使用任何 属性,问题是当我从 1 个模块访问并提供数据然后尝试从 diff 模块访问时,缓存对象不包含我 "store" 的数据,知道如何做对吗?
"use strict";
var Parser = require('myParser'),
_ = require('lodash');
function myParser(data) {
if (!(this instanceof myParser)) return new myParser(data);
if (!_.isEmpty(this.cacheObj)) {
this.parsedData = this.cacheObj;
} else {
this.parsedData = Parser.parse(data);
this.cacheObj = this.parsedData;
}
}
myParser.prototype = {
cacheObj: {},
getPropOne: function () {
return this.parsedData.propOne;
},
getPropTwo: function () {
return this.parsedData.propTwo;
}
};
module.exports = myParser;
我的节点应用程序的数据应该相同,所以我不需要每次都传递它...只是为了 "init"...
使用单例对象,下面是基本示例
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;
我有节点模块,我需要解析数据,我想在不同的modules.the第一个模块中共享这个解析的属性,调用这个模块负责传递数据,而其他模块不负责需要发送数据,因为我已经存储了 parsedData(在 cacheObj 中)并且可以使用任何 属性,问题是当我从 1 个模块访问并提供数据然后尝试从 diff 模块访问时,缓存对象不包含我 "store" 的数据,知道如何做对吗?
"use strict";
var Parser = require('myParser'),
_ = require('lodash');
function myParser(data) {
if (!(this instanceof myParser)) return new myParser(data);
if (!_.isEmpty(this.cacheObj)) {
this.parsedData = this.cacheObj;
} else {
this.parsedData = Parser.parse(data);
this.cacheObj = this.parsedData;
}
}
myParser.prototype = {
cacheObj: {},
getPropOne: function () {
return this.parsedData.propOne;
},
getPropTwo: function () {
return this.parsedData.propTwo;
}
};
module.exports = myParser;
我的节点应用程序的数据应该相同,所以我不需要每次都传递它...只是为了 "init"...
使用单例对象,下面是基本示例
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;