javascript - Access to module data from diff module -


i've node module , need parse data , want share parsed properties in different modules.the first module call responsible pass data , other modules doesn't need send data since store parseddata(in cacheobj) , can use of property,the problem when access 1 module , provide data , try access diff module,the cache object not contain data "store",any idea how right?

"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;  

the data should same node app dont need pass every time...just "init"...

use singleton objects, basic sample below

var singleton = (function () {     var instance;      function createinstance() {         var object = new object("i instance");         return object;     }      return {         getinstance: function () {             if (!instance) {                 instance = createinstance();             }             return instance;         }     }; })(); 

in case, use same approach

"use strict"; var parser = require('myparser'),     _ = require('lodash');  var cacheobj; // <-- singleton, hold value , not 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;  

using memory-cache, don't forget install

"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;  

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -