Build the cache key properly if there are multiple types

3.0-unstable version of #2406