6

I am using NodeJS by express framework, now I want to use the memory cache to save objects(almost 3000).

I have thought create a file like

cache.js:

var cache={};
module.exports=cache;

Then in any module I need the cache I can require it:

require('cache')
cache.xx=xxx

However it seems that I can not make sure that the cache object will be create and will be only one copy during the app running. Since the require may use the module cache or not.(from this link:https://stackoverflow.com/a/9210901/306719)

Any suggestion?

2
  • 1
    The link you've included says that the module cache will always work. The module will NOT be invalidated and reloaded. It should work just fine to do what you want just like you planned. Commented Nov 12, 2014 at 3:02
  • 1
    Ok, misunderstand it since I read the "...may not cause..."... Commented Nov 12, 2014 at 3:10

1 Answer 1

4

That will work just fine. Node caches the module the first time it's loaded, so any additional requires will get the same exported object.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.