cache function refactor