refactor cache logic