diff --git a/musikr/src/main/java/org/oxycblt/musikr/covers/Covers.kt b/musikr/src/main/java/org/oxycblt/musikr/covers/Covers.kt index 3514241a5..c9f17b580 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/covers/Covers.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/covers/Covers.kt @@ -20,6 +20,10 @@ package org.oxycblt.musikr.covers import android.os.ParcelFileDescriptor import java.io.InputStream +import java.util.concurrent.ConcurrentHashMap +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.async +import kotlinx.coroutines.coroutineScope import org.oxycblt.musikr.fs.File import org.oxycblt.musikr.metadata.Metadata @@ -41,6 +45,20 @@ interface Covers { suspend fun obtain(id: String): CoverResult } +/** A per-run successful-hit cache that shares in-flight lookups and retries misses. */ +internal class CachedCovers(private val delegate: Covers) : Covers { + private val lookups = ConcurrentHashMap>>() + + override suspend fun obtain(id: String): CoverResult = coroutineScope { + val lookup = lookups.computeIfAbsent(id) { async { delegate.obtain(id) } } + val result = lookup.await() + if (result is CoverResult.Miss) { + lookups.remove(id, lookup) + } + result + } +} + /** * An mutable repoistory for cover information. * diff --git a/musikr/src/main/java/org/oxycblt/musikr/pipeline/ExploreStep.kt b/musikr/src/main/java/org/oxycblt/musikr/pipeline/ExploreStep.kt index 52f401657..33212c061 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/pipeline/ExploreStep.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/pipeline/ExploreStep.kt @@ -27,6 +27,7 @@ import org.oxycblt.musikr.Config import org.oxycblt.musikr.Storage import org.oxycblt.musikr.cache.CacheResult import org.oxycblt.musikr.cache.CachedFile +import org.oxycblt.musikr.covers.CachedCovers import org.oxycblt.musikr.covers.CoverResult import org.oxycblt.musikr.fs.FS import org.oxycblt.musikr.fs.File @@ -59,6 +60,7 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage) explored: Channel, playlistFiles: MutableList, ): Deferred> { + val cachedCovers = CachedCovers(storage.covers) val files = Channel(Channel.UNLIMITED) val filesTask = fs.explore(files) @@ -94,7 +96,7 @@ private class ExploreStepImpl(private val fs: FS, private val storage: Storage) val audio = item.cachedFile.audio ?: return@mapParallel Finalized(NotAudio) val coverId = when ( - val result = audio.coverId?.let { id -> storage.covers.obtain(id) } + val result = audio.coverId?.let { id -> cachedCovers.obtain(id) } ) { is CoverResult.Hit -> result.cover is CoverResult.Miss -> diff --git a/musikr/src/test/java/org/oxycblt/musikr/covers/CachedCoversTest.kt b/musikr/src/test/java/org/oxycblt/musikr/covers/CachedCoversTest.kt new file mode 100644 index 000000000..29c07afa3 --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/covers/CachedCoversTest.kt @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2026 Auxio Project + * CachedCoversTest.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.covers + +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.mockk +import kotlinx.coroutines.test.runTest +import org.junit.Assert.assertSame +import org.junit.Test + +class CachedCoversTest { + @Test + fun obtain_sameId_reusesHit() = runTest { + val cover = mockk() + val delegate = mockk>() + coEvery { delegate.obtain("shared") } returns CoverResult.Hit(cover) + val cached = CachedCovers(delegate) + + val first = cached.obtain("shared") as CoverResult.Hit + val second = cached.obtain("shared") as CoverResult.Hit + + assertSame(cover, first.cover) + assertSame(cover, second.cover) + coVerify(exactly = 1) { delegate.obtain("shared") } + } +}