Refactor Cache code