Fixed #21012 -- Thread-local caches, like databases.

This continues the work from PR #1782.