HTTP Cache, where have you been all my life?

Yesterday I learned about the built-in support for HTTP cache in URLSession. How had I missed out on this? There are so many frameworks and libs out there that implement image caching systems, but this great functionality is already built into Foundation.

This post by Alexander Grebenyuk does a good job of laying it all out.

In my current project I need this to cache profile images.

Most of what I had to do to get HTTP cache working was to set the cache policy for my image requests. (Cache policy can be configured per session or per individual request.)

request.cachePolicy = .returnCacheDataElseLoad

(Note: This policy ignores validation in favor of always returning the cached value. We have a separate mechanism to signal profile image changes.)

Author: Mark

Mark is an American computer programmer living in Switzerland.