Cache threading fix

This fixes a longstanding thread-safety issue with the caching code. It would manifest itself as exceptions or hangs from getValue() calls, when one thread was removing a value from the cache while another was preparing to use it.