Implement assignment 2 with a general cache function