From e8dce7f5e76431909f37e84772b315fb2202c6ff Mon Sep 17 00:00:00 2001 From: Solitai7e <60846439+Solitai7e@users.noreply.github.com> Date: Thu, 27 Jul 2023 16:53:56 +0000 Subject: [PATCH] Pixiv: LruCache fix (#17279) * Pixiv: LruCache fix * requested changes --- .../tachiyomi/extension/all/pixiv/Pixiv.kt | 21 +++++++++---------- .../tachiyomi/extension/all/pixiv/Util.kt | 10 +++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/all/pixiv/src/eu/kanade/tachiyomi/extension/all/pixiv/Pixiv.kt b/src/all/pixiv/src/eu/kanade/tachiyomi/extension/all/pixiv/Pixiv.kt index 35ca49e32..c0480bef7 100644 --- a/src/all/pixiv/src/eu/kanade/tachiyomi/extension/all/pixiv/Pixiv.kt +++ b/src/all/pixiv/src/eu/kanade/tachiyomi/extension/all/pixiv/Pixiv.kt @@ -1,6 +1,5 @@ package eu.kanade.tachiyomi.extension.all.pixiv -import android.util.LruCache import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.MangasPage import eu.kanade.tachiyomi.source.model.Page @@ -269,19 +268,19 @@ class Pixiv(override val lang: String) : HttpSource() { return Observable.just(MangasPage(mangas, hasNextPage = mangas.isNotEmpty())) } - private val illustsCache = object : LruCache(25) { - override fun create(illustId: String): PixivIllust { + private val getIllustCached by lazy { + lruCached(25) { illustId -> val call = ApiCall("/touch/ajax/illust/details?illust_id=$illustId") - return call.executeApi().illust_details!! + return@lruCached call.executeApi().illust_details!! } } - private val seriesIllustsCache = object : LruCache>(25) { - override fun create(seriesId: String): List { + private val getSeriesIllustsCached by lazy { + lruCached>(25) { seriesId -> val call = ApiCall("/touch/ajax/illust/series_content/$seriesId") var lastOrder = 0 - return buildList { + return@lruCached buildList { while (true) { call.url.setEncodedQueryParameter("last_order", lastOrder.toString()) @@ -300,7 +299,7 @@ class Pixiv(override val lang: String) : HttpSource() { if (isSeries) { val series = ApiCall("/touch/ajax/illust/series/$id").executeApi() - val illusts = seriesIllustsCache.get(id) + val illusts = getSeriesIllustsCached(id) if (series.id != null && series.userId != null) { manga.setUrlWithoutDomain("/user/${series.userId}/series/${series.id}") @@ -319,7 +318,7 @@ class Pixiv(override val lang: String) : HttpSource() { (series.coverImage ?: illusts.firstOrNull()?.url)?.let { manga.thumbnail_url = it } } else { - val illust = illustsCache.get(id) + val illust = getIllustCached(id) illust.id?.let { manga.setUrlWithoutDomain("/artworks/$it") } illust.title?.let { manga.title = it } @@ -341,8 +340,8 @@ class Pixiv(override val lang: String) : HttpSource() { val (id, isSeries) = parseSMangaUrl(manga.url) val illusts = when (isSeries) { - true -> seriesIllustsCache.get(id) - false -> listOf(illustsCache.get(id)) + true -> getSeriesIllustsCached(id) + false -> listOf(getIllustCached(id)) } val chapters = illusts.mapIndexed { i, illust -> diff --git a/src/all/pixiv/src/eu/kanade/tachiyomi/extension/all/pixiv/Util.kt b/src/all/pixiv/src/eu/kanade/tachiyomi/extension/all/pixiv/Util.kt index 86dcc1a94..1ca14c1c0 100644 --- a/src/all/pixiv/src/eu/kanade/tachiyomi/extension/all/pixiv/Util.kt +++ b/src/all/pixiv/src/eu/kanade/tachiyomi/extension/all/pixiv/Util.kt @@ -1,5 +1,7 @@ package eu.kanade.tachiyomi.extension.all.pixiv +import android.util.LruCache + internal fun countUp(start: Int = 0) = sequence { yieldAll(start..Int.MAX_VALUE) throw RuntimeException("Overflow") @@ -16,3 +18,11 @@ internal fun parseSMangaUrl(url: String): Pair { val isSeries = url.getOrNull(1) != 'a' return Pair(url.substringAfterLast('/'), isSeries) } + +internal fun lruCached(capacity: Int, compute: (K) -> V): (K) -> V { + val cache = object : LruCache(capacity) { + override fun create(key: K): V = compute(key) + } + + return { key -> synchronized(cache) { cache.get(key) } } +}