each time the user entering a specific screen, it should be a trigger for creating a tree data type which based on a data that saved on the database. this tree calculation can take some time, and it's can be heavy on performance.
the following requests are sent from UI :
1. refreshTree - a trigger for building the tree
2. isTreeReady - indicating whether the tree is ready to use, and called every x amount of time
3. getTree - return the tree.
when building this I should take into consideration that multiple users can try to perform each one of those actions simultaneously. it's also true that 2 users share the same tree if any refresh hasn't been done.
I've thought about implementing it as a cache as follow :
the issue I have with that is that there can be a scenario in which :
first user refreshing the tree - and the tree is built.
then second user start to build the tree, and initialize the isReady flag to false, before the first user has noticed about the tree calculation - in this case the first user will need to wait to the calculation to be completed (even though he could have use the tree).
I'm trying to think a bout using a read lock in some way (instead of the isTreeReady flag), but I can't think about any that will fit my needs.