[1.7.x] Ensured cache tests do not leak temp dirs

Refs #17215.
Backport of 1d9596025e from master.