refactor cache code