8

In Effective Android HTTP Jesse Wilson mentioned the following:

Serving static resources like images? Use a permanent URL and let it cache forever

I am using Picasso with a specific OkHttp instance for fetching and caching images. I am wondering how to setup the let it cache forever strategy?

I can only think about overriding the HTTP cache control header max-stale (Cache-Control: max-stale=)with the highest possible value, is there another (better) way?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Niqo
  • 1,072
  • 10
  • 20

1 Answers1

14

Best way is to configure your server to include a long max-age. For example, specify 365 days with this header:

Cache-Control: max-age=31536000

If you want to enable unlimited caching on the client, you can use a long max-stale:

Cache-Control: max-stale=31536000
Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Is there a reason this is considered better than a long "Expires"? – Umopepisdn Jan 08 '16 at 02:35
  • 1
    It’s preferred by RFC 7234, the HTTP caching spec. “If a response includes a Cache-Control field with the max-age directive (Section 5.2.2.8), a recipient MUST ignore the Expires field.” – Jesse Wilson Jan 09 '16 at 05:45