1

I am really stuck by nodejs cache system. I have this structure for my project :

Project/
    apps/
        jobs_processor/
            app.js
            processors.js
            processors/
    libs/
        queue_manager.js

queue_manager.js require processors.js

var processors = require("../apps/jobs_processor/processors.js");

app.js require also processor.js

var processors = require("./processors.js");

If I take into account the documentation, I must have the same path may be to obtain the same object, is that right ? If so, how can I achieve that (have the same path) ?

Thanks.

EDIT:

If found a solution to my problem. Here is the first version of queue_manager.js file

var _ = require("lodash");

var Utils = require("./utilities");
var Processors = require("../apps/jobs_processor/processors");
var Logger = require("./logger");

var QUEUES_CACHE = {};

exports.createJob = createJob;
exports.getCacheObject = getCacheObject;

function createJob(name, data) {
  var cacheId = name.replace(/ /g, "_");

  Logger.info("Cache ID: " + cacheId);

  if (!QUEUES_CACHE[ cacheId ]) {
    _.each(Processors, function (processor) {
      Logger.debug("PROCESSOR NAME: " + processor.name);
      Logger.debug("JOB NAME: " + name);

      if (processor.name === name)
        QUEUES_CACHE[ cacheId ] = processor;
    });

    if (!QUEUES_CACHE[ cacheId ])
      throw new Error("Processor for job \"" + name + "\" not found.");
  }

  Logger.debug(Object.keys(QUEUES_CACHE));

  return QUEUES_CACHE[ cacheId ].queue.add(data);
}

function getCacheObject() {
  return QUEUES_CACHE;
}

And now the last version of the same file

var _ = require("lodash");

var Utils = require("./utilities");
var Logger = require("./logger");

exports.createJob = createJob;

function createJob(name, data) {
  var Processors = require("../apps/jobs_processor/processors");
  var processor;

  _.each(Processors, function (element) {
    Logger.debug("Processor name: " + element.name);

    if (element.name === name)
      processor = element;
  });

  return processor.queue.add(data);
}

Each time that i called createJob method, I require the processors module which is an array of each job processor that I have created.

Ismael
  • 320
  • 4
  • 15

1 Answers1

3

Node.js will resolve the path before caching the module.
As long as your relative paths resolve to the same absolute path on disk, you're fine.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • In my example the relative paths are different, so there is 2 objects in cache – Ismael Dec 23 '14 at 14:51
  • @igorissen: Wrong. The cache is keyed by resolved, absolute path, which will be the same. – SLaks Dec 23 '14 at 14:53
  • it's very strange because I'm logging an object in the `queue_manager.js` and when the `createJob` method is called I have 2 different objects. – Ismael Dec 23 '14 at 15:22
  • Run `console.log(require.cache)` and look at what is in the cache. – SLaks Dec 23 '14 at 15:25
  • I just log `require.cache` and like you said there is only one `/Users/fluxb0x/Projects/my-project/libs/queue_manager.js` file. But I found the same module as `parent` in other modules (`{"/Users/fluxb0x/Projects/my-project/module.js": {"parent": {"id":"/Users/fluxb0x/Projects/my-project/libs/queue_manager.js", ...} } }`). Is there a difference between the main module and the parent module? or the parent module is just a link to the main module? – Ismael Dec 23 '14 at 15:31
  • `module.parent` is the module info object that first called `require()`. It's a reference to the cached object. – SLaks Dec 23 '14 at 15:35