Refactors rebuildCache method.