Fix race condition caused by concurrent access to global cache

parent #437 
+1. This needs to be done as a ton of the code looks duplicated.