Skip to content

Moa image caching

Evgenii Neumerzhitckii edited this page Oct 24, 2016 · 7 revisions

Caching

Moa uses the built-in URLSession caching methods. It creates a dedicated cache storage that is separate from the app's shared url cache. You can change the cache settings at any moment by setting the Moa.settings.cache property.

Caching controlled by the server

By default images are cached locally according to their response HTTP headers: Cache-Control, Expires and ETag. This is useful when you control the web server and can configure its image caching responses. It is similar to how the image caching is done in the web browsers where the caching decisions are made on the server side. Ilya Grigorik did an excellent overview of HTTP caching in this article.

// By default images are cached according to their response HTTP headers.
Moa.settings.cache.requestCachePolicy = .useProtocolCachePolicy

Force caching on client side

If you don't control the server you can set the requestCachePolicy setting to .returnCacheDataElseLoad. It will load the images from local cache regardless of their age and expiration date.

// Use local cache regardless of response HTTP headers.
Moa.settings.cache.requestCachePolicy = .returnCacheDataElseLoad

Other cache settings

// Change memory cache size. Default is 20 MB.
Moa.settings.cache.memoryCapacityBytes = 20 * 1024 * 1024

// Change disk cache size. Default is 100 MB.
Moa.settings.cache.diskCapacityBytes = 100 * 1024 * 1024

// Change the name of the cache directory.
// Useful for sharing cache with the rest of the app.
// Default is "moaImageDownloader".
Moa.settings.cache.diskPath = "MyAppSharedCache"