Updated middleware to add caching