This is a follow up to 435e220 with a small refactor to make the function easier to test, and adding unit tests for CacheKey.
435e220