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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

In my example the relative paths are different, so there is 2 objects in cache
@igorissen: Wrong. The cache is keyed by resolved, absolute path, which will be the same.
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.
Run console.log(require.cache) and look at what is in the cache.
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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.