Added locks for caching when running on multiple threads