Kotlin, Kotlin Design Patterns, Kotlin Flyweight, Kotlin Flyweight Pattern.
get(url: String): Image { val imageMutex = lock.withLock { locks.getOrPut(url) { Mutex() } } val image = imageMutex.withLock { getImage(url) } locks.remove(url) return image } private suspend fun getImage(url: String): Image = cache[url] ?: fetchImage(url).also { image -> cache[url] = image } }