0

I have a project in which i have to create a global hashmap which has to be looked up frequently. This hashmap stores values from tables which won't be updated very frequently. The basic flow is to create a hashmap from the Postgres table values and during the other process in the project, this hashmap has to be looked up frequently for getting the values. I have used the node package Hashmap for the creation of hashmap. But the problem is, i have to create the hashmap every time i lookup for the value. This is taking too much time. Is there any way that i can store this global hashmap as a global const anywhere and just refer the variable during every lookup. Also i need to configure these constants as to be refreshed every day and startup of the project

I have tried an approach to call the hashmap creation function in the index.js file and store it in a constant. But every time i lookup the constant has to be reinitiated and the hashmap creation function called for every lookup. This is not the expected behaviour.

I expect the hashmap creation function has to be called once in a day or start up of the project and the same hashmap has to be referred for every lookup until next day/ next project startup so that the process is more memory and time efficient.

Can anyone suggest some solution for this

Thanks in advance.

1 Answers1

0

You can create a hash map from a Map object

You can put that map on a node global object

Then you can setup an interval to refresh that hash map once every 24 hours

global.myHashMap = new Map()

function populateHashMap(){
 //clear and populate the map
}

//Initial call
populateHashMap()

const oneDayInMs = 86400000

// set refresh every 24h
setInterval(populateHasMap,oneDayInMs )
Ivan V.
  • 7,593
  • 2
  • 36
  • 53