简体   繁体   中英

Best way to cache data on node js

I have Json file I need to look for specific value (file is pretty big) and I want to turn this json into array so doing so would be easier and faster. BUT what is the best way to save this array? so I wont need to run over this json every time and it will be saved until recycle or service restart? (node js project)

1) Use Redis recommended

Pros:

  1. Access objects super fast.
  2. Isolated from node process.
    • Will not affect heap memory.
    • Can be deployed at separated server.
    • Data persistence if your application crashed.
  3. Support compression.
    • low memory consumption.

Cons:

  1. You may face some limitations if you have nested objects, but there is a workaround which requires extra work to handle.

2) Use database preferred MongoDB ,

Pros:

  1. Save/Load objects easily since MongoDB supports JSON.
  2. The same as number 2 of Redis pros

Cons:

  1. You have to query every time to access objects.

3) Use Files not recommended , When your application start/restart , load your objects form file into global array, and when close/shutdown your application dump your objects from global array back into file.

Pros:

  1. Access objects fast.

Cons:

  1. Heap memory leakage if your objects size are Huge.
  2. Data loss if your application crashed.

At the end it's your choice, if the speed matters choose Redis , if you want the easy way choose mongoDB . If it's not a problem losing some of your data go for Files . Also you can mix between number 2 and 3 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM