Search code examples
databasedata-structuresdata-stream

Best way to manage a queue or stack in java for DB Seq objects


In my application there are some data which always reads from Seq. I am thinking of having a data structure which would start getting loaded with DB Seq and whenever I need DB Seq, I can directly read from there. Once the data is read from there it needs to be removed so that no one else can read the same data again. Also this data structure would automatically hit DB and get more seq objects once it is left with only 0.25 % of total capacity something like HashMap.

This would enable to not hit DB when I need but directly read from this structure and save DB hit time in my flow.

I am thinking of maintaining a Stack kind of structure but how to reload it automatically because if I use some kind of Scheduler I am not sure how to configure it appropriately.

Any help would be great.


Solution

  • Lets, consider below example

    Step 1 : You can create stack of size 16 and push 16 sequence which is loaded from DB sequence.

    Step 2 : check size of stack while poping/removing elements from it. if it is 4, then again loads sequence from db if not leave it.