Fix lookup of project by unknown ref when caching is enabled

Merged Douwe Maan requested to merge 24224-fix-project-ref-cache into master

I fixed the bug (.compact) and made sure caching worked consistently with project_from_ref_cached .

Closes #24224 (closed)