Refactor the cache code... again.