coil-kt / coil

Image loading for Android and Compose Multiplatform.
https://coil-kt.github.io/coil/
Apache License 2.0
10.78k stars 660 forks source link

API for atomically accessing an image request's disk cache entry #2630

Open saket opened 4 days ago

saket commented 4 days ago

image

Is your feature request related to a problem? Please describe. Telephoto uses coil's disk cache for streaming large images directly from the disk. This is working well for most users, but some users are reporting crashes where coil is returning a null cache entry, possibly because the cache entry was either cleared or opened somewhere else before telephoto could access it.

Describe the solution you'd like

an atomic API that writes the image data to disk then immediately opens a source for the file. That way we avoid the race condition between writing the image data and calling openSnapshot

Additional context https://github.com/saket/telephoto/issues/106#issuecomment-2436547617

revonateB0T commented 4 days ago

Isn't there already a commitAndOpenSnapshot? https://github.com/coil-kt/coil/blob/0582d7587e946c1d19340aee15227bf6aefc7770/coil-core/src/commonMain/kotlin/coil3/disk/RealDiskCache.kt#L66

colinrtwhite commented 3 days ago

@revonateB0T That API's only useful if you're also writing the data. Saket's looking for an API that will do URL -> DiskCache.Snapshot. The current way of URL -> disk cache key -> DiskCache.Snapshot allows some cases where disk cache key -> DiskCache.Snapshot will return null (race conditions around simultaneous read/write, cache getting cleared).