MDL-19702 rewritten context caching by Sam Marshall + tweaks for potential problems...