From 069a615193949894f59cb66f2742305cfa462cc7 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Fri, 8 Dec 2023 13:23:58 +0100 Subject: [PATCH] Polycentric persistent cache fixes for subscriptions --- .../polycentric/PolycentricCache.kt | 4 +++- .../platformplayer/states/StatePolycentric.kt | 24 ++++++++++++------- .../states/StateSubscriptions.kt | 12 ++++++++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/futo/platformplayer/polycentric/PolycentricCache.kt b/app/src/main/java/com/futo/platformplayer/polycentric/PolycentricCache.kt index fa174bc7..6f887cb5 100644 --- a/app/src/main/java/com/futo/platformplayer/polycentric/PolycentricCache.kt +++ b/app/src/main/java/com/futo/platformplayer/polycentric/PolycentricCache.kt @@ -222,7 +222,7 @@ class PolycentricCache { } } - suspend fun getProfileAsync(id: PlatformID): CachedPolycentricProfile? { + suspend fun getProfileAsync(id: PlatformID, url: String? = null): CachedPolycentricProfile? { if (!StatePolycentric.instance.enabled || id.claimType <= 0) { return CachedPolycentricProfile(null); } @@ -243,6 +243,8 @@ class PolycentricCache { Logger.v(TAG, "getProfileAsync (id: $id) != null (with retrieved valid claims)") return getProfileAsync(claims.ownedClaims.first().system).await() } else { + if(url != null) + _profileUrlCache.setAndSave(url, PolycentricCache.CachedPolycentricProfile(null)); return null; } } diff --git a/app/src/main/java/com/futo/platformplayer/states/StatePolycentric.kt b/app/src/main/java/com/futo/platformplayer/states/StatePolycentric.kt index 6911d0f7..5c12771f 100644 --- a/app/src/main/java/com/futo/platformplayer/states/StatePolycentric.kt +++ b/app/src/main/java/com/futo/platformplayer/states/StatePolycentric.kt @@ -170,17 +170,23 @@ class StatePolycentric { } fun getChannelUrls(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false): List { + return getChannelUrlsWithUpdateResult(url, channelId, cacheOnly).second; + } + fun getChannelUrlsWithUpdateResult(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false): Pair> { + var didUpdate = false; if (!enabled) { - return listOf(url); + return Pair(false, listOf(url)); } - var polycentricProfile: PolycentricProfile? = null; try { - polycentricProfile = PolycentricCache.instance.getCachedProfile(url)?.profile; - if (polycentricProfile == null && channelId != null) { + val polycentricCached = PolycentricCache.instance.getCachedProfile(url, cacheOnly) + polycentricProfile = polycentricCached?.profile; + if (polycentricCached == null && channelId != null) { Logger.i("StateSubscriptions", "Get polycentric profile not cached"); - if(!cacheOnly) - polycentricProfile = runBlocking { PolycentricCache.instance.getProfileAsync(channelId) }?.profile; + if(!cacheOnly) { + polycentricProfile = runBlocking { PolycentricCache.instance.getProfileAsync(channelId, url) }?.profile; + didUpdate = true; + } } else { Logger.i("StateSubscriptions", "Get polycentric profile cached"); } @@ -193,12 +199,12 @@ class StatePolycentric { val urls = polycentricProfile.ownedClaims.groupBy { it.claim.claimType } .mapNotNull { it.value.firstOrNull()?.claim?.resolveChannelUrl() }.toMutableList(); if(urls.any { it.equals(url, true) }) - return urls; + return Pair(didUpdate, urls); else - return listOf(url) + urls; + return Pair(didUpdate, listOf(url) + urls); } else - return listOf(url); + return Pair(didUpdate, listOf(url)); } fun getChannelContent(scope: CoroutineScope, profile: PolycentricProfile, isSubscriptionOptimized: Boolean = false, channelConcurrency: Int = -1): IPager? { diff --git a/app/src/main/java/com/futo/platformplayer/states/StateSubscriptions.kt b/app/src/main/java/com/futo/platformplayer/states/StateSubscriptions.kt index 351b3a60..1f28b707 100644 --- a/app/src/main/java/com/futo/platformplayer/states/StateSubscriptions.kt +++ b/app/src/main/java/com/futo/platformplayer/states/StateSubscriptions.kt @@ -254,9 +254,17 @@ class StateSubscriptions { } val usePolycentric = true; + val lock = Object(); + var polycentricBudget: Int = 10; val subUrls = getSubscriptions().parallelStream().map { - if(usePolycentric) - Pair(it, StatePolycentric.instance.getChannelUrls(it.channel.url, it.channel.id)); + if(usePolycentric) { + val result = StatePolycentric.instance.getChannelUrlsWithUpdateResult(it.channel.url, it.channel.id, polycentricBudget <= 0); + if(result.first) + synchronized(lock) { + polycentricBudget--; + } + Pair(it, result.second); + } else Pair(it, listOf(it.channel.url)); }.asSequence()