Hi all, while using coil with to load images with...
# squarelibraries
a
Hi all, while using coil with to load images with
Cache-Control
response headers, I noticed that if memory cache is enabled and there is an image in the cache for the requested key, then the cache control header is not respected because cached value is returned before
CacheControlCacheStrategy
is queried. Is it an expected behavior or there is a way to configure coil to respect cache control with the memory cache enabled?
j
I can't speak to Coil, but this was also the behavior of Picasso. Our assertion was that the process would only be alive for a short period of time, and best practices are to change your URL when the image changes.
🤔 1
yes black 2
a
I see, in my case the image urls are stable in a sense that they don’t change when the content changes so I want to rely on Cache-Control headers to manage the image lifecycle. I also don’t want to lose the fast in-memory storage so thinking about how to work around this
c
I wouldn’t say this is 100% intentional, but it is a side effect of Coil’s design. Unfortunately I can’t think of a great way to change this behaviour since cache-control info isn’t stored in the memory cache. That info is only stored in the disk cache, which can’t/shouldn’t be read on the main thread. The memory cache is set up to be as fast as possible since it’s checked on the main thread and checking cache headers would slow it down.
You could possibly set
memoryCachePolicy(CachePolicy.DISABLED)
and manually check the memory cache yourself inside a custom interceptor after verifying the cache headers, though there are likely pitfalls there
a
got it, I tried to hack around by essentially storing the
NetworkResponse
in memory cache for a memory key and querying the
CacheControlStrategy
every time a memory entry is queried. I figured that
Computation
from the cache control is fast and doesn’t do any i/o so it should be fine to query in the memory cache. It didn’t work as expected because I couldn’t get access to the current
NetworkRequest
inside the memory cache so I stopped the research because it that felt too hacky to be used in prod anyway.
You could possibly set
memoryCachePolicy(CachePolicy.DISABLED)
and manually check the memory cache yourself inside a custom interceptor after verifying the cache headers, though there are likely pitfalls there
will it require to basically copy the
Computation
logic? That’s smth I wanted to avoid