Fix cache key collision and add test for cache key distinctness.

Fixes #7420.