New cache fixes: add safeguard before looking in folders
# What does this PR do?

This PR adds a few fixes in the new cache functions, mainly to not call `os.listdir` on a folder that does not exist

Fixes #18517 
_The documentation is not available anymore as the PR was closed or merged._