Polycentric persistent cache fixes for subscriptions

This commit is contained in:
Kelvin 2023-12-08 13:23:58 +01:00
commit 069a615193
3 changed files with 28 additions and 12 deletions

View file

@ -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) { if (!StatePolycentric.instance.enabled || id.claimType <= 0) {
return CachedPolycentricProfile(null); return CachedPolycentricProfile(null);
} }
@ -243,6 +243,8 @@ class PolycentricCache {
Logger.v(TAG, "getProfileAsync (id: $id) != null (with retrieved valid claims)") Logger.v(TAG, "getProfileAsync (id: $id) != null (with retrieved valid claims)")
return getProfileAsync(claims.ownedClaims.first().system).await() return getProfileAsync(claims.ownedClaims.first().system).await()
} else { } else {
if(url != null)
_profileUrlCache.setAndSave(url, PolycentricCache.CachedPolycentricProfile(null));
return null; return null;
} }
} }

View file

@ -170,17 +170,23 @@ class StatePolycentric {
} }
fun getChannelUrls(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false): List<String> { fun getChannelUrls(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false): List<String> {
if (!enabled) { return getChannelUrlsWithUpdateResult(url, channelId, cacheOnly).second;
return listOf(url); }
fun getChannelUrlsWithUpdateResult(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false): Pair<Boolean, List<String>> {
var didUpdate = false;
if (!enabled) {
return Pair(false, listOf(url));
} }
var polycentricProfile: PolycentricProfile? = null; var polycentricProfile: PolycentricProfile? = null;
try { try {
polycentricProfile = PolycentricCache.instance.getCachedProfile(url)?.profile; val polycentricCached = PolycentricCache.instance.getCachedProfile(url, cacheOnly)
if (polycentricProfile == null && channelId != null) { polycentricProfile = polycentricCached?.profile;
if (polycentricCached == null && channelId != null) {
Logger.i("StateSubscriptions", "Get polycentric profile not cached"); Logger.i("StateSubscriptions", "Get polycentric profile not cached");
if(!cacheOnly) if(!cacheOnly) {
polycentricProfile = runBlocking { PolycentricCache.instance.getProfileAsync(channelId) }?.profile; polycentricProfile = runBlocking { PolycentricCache.instance.getProfileAsync(channelId, url) }?.profile;
didUpdate = true;
}
} else { } else {
Logger.i("StateSubscriptions", "Get polycentric profile cached"); Logger.i("StateSubscriptions", "Get polycentric profile cached");
} }
@ -193,12 +199,12 @@ class StatePolycentric {
val urls = polycentricProfile.ownedClaims.groupBy { it.claim.claimType } val urls = polycentricProfile.ownedClaims.groupBy { it.claim.claimType }
.mapNotNull { it.value.firstOrNull()?.claim?.resolveChannelUrl() }.toMutableList(); .mapNotNull { it.value.firstOrNull()?.claim?.resolveChannelUrl() }.toMutableList();
if(urls.any { it.equals(url, true) }) if(urls.any { it.equals(url, true) })
return urls; return Pair(didUpdate, urls);
else else
return listOf(url) + urls; return Pair(didUpdate, listOf(url) + urls);
} }
else else
return listOf(url); return Pair(didUpdate, listOf(url));
} }
fun getChannelContent(scope: CoroutineScope, profile: PolycentricProfile, isSubscriptionOptimized: Boolean = false, channelConcurrency: Int = -1): IPager<IPlatformContent>? { fun getChannelContent(scope: CoroutineScope, profile: PolycentricProfile, isSubscriptionOptimized: Boolean = false, channelConcurrency: Int = -1): IPager<IPlatformContent>? {

View file

@ -254,9 +254,17 @@ class StateSubscriptions {
} }
val usePolycentric = true; val usePolycentric = true;
val lock = Object();
var polycentricBudget: Int = 10;
val subUrls = getSubscriptions().parallelStream().map { val subUrls = getSubscriptions().parallelStream().map {
if(usePolycentric) if(usePolycentric) {
Pair(it, StatePolycentric.instance.getChannelUrls(it.channel.url, it.channel.id)); val result = StatePolycentric.instance.getChannelUrlsWithUpdateResult(it.channel.url, it.channel.id, polycentricBudget <= 0);
if(result.first)
synchronized(lock) {
polycentricBudget--;
}
Pair(it, result.second);
}
else else
Pair(it, listOf(it.channel.url)); Pair(it, listOf(it.channel.url));
}.asSequence() }.asSequence()