From 08134b44275f6cbbaebf07b12440983c1b99df04 Mon Sep 17 00:00:00 2001 From: Koen Date: Mon, 4 Dec 2023 17:09:27 +0100 Subject: [PATCH] If cache entry is expired, load it from cache, then reload it from live. --- .../polycentric/PolycentricCache.kt | 20 +++++++++++-------- .../views/adapters/SubscriptionViewHolder.kt | 3 +++ .../adapters/feedtypes/PreviewVideoView.kt | 3 +++ .../adapters/viewholders/CreatorViewHolder.kt | 3 +++ .../viewholders/SubscriptionBarViewHolder.kt | 3 +++ 5 files changed, 24 insertions(+), 8 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 dc5ce5bb..2a4bb4b4 100644 --- a/app/src/main/java/com/futo/platformplayer/polycentric/PolycentricCache.kt +++ b/app/src/main/java/com/futo/platformplayer/polycentric/PolycentricCache.kt @@ -7,7 +7,6 @@ import com.futo.platformplayer.constructs.BatchedTaskHandler import com.futo.platformplayer.fragment.mainactivity.main.PolycentricProfile import com.futo.platformplayer.getNowDiffSeconds import com.futo.platformplayer.logging.Logger -import com.futo.platformplayer.resolveChannelUrl import com.futo.platformplayer.resolveChannelUrls import com.futo.platformplayer.serializers.OffsetDateTimeSerializer import com.futo.platformplayer.stores.CachedPolycentricProfileStorage @@ -19,17 +18,21 @@ import java.nio.ByteBuffer import java.time.OffsetDateTime class PolycentricCache { - data class CachedOwnedClaims(val ownedClaims: List?, val creationTime: OffsetDateTime = OffsetDateTime.now()); + data class CachedOwnedClaims(val ownedClaims: List?, val creationTime: OffsetDateTime = OffsetDateTime.now()) { + val expired get() = creationTime.getNowDiffSeconds() > CACHE_EXPIRATION_SECONDS + } @Serializable - data class CachedPolycentricProfile(val profile: PolycentricProfile?, @Serializable(with = OffsetDateTimeSerializer::class) val creationTime: OffsetDateTime = OffsetDateTime.now()); + data class CachedPolycentricProfile(val profile: PolycentricProfile?, @Serializable(with = OffsetDateTimeSerializer::class) val creationTime: OffsetDateTime = OffsetDateTime.now()) { + val expired get() = creationTime.getNowDiffSeconds() > CACHE_EXPIRATION_SECONDS + } - private val _cacheExpirationSeconds = 60 * 60 * 3; private val _cache = hashMapOf() private val _profileCache = hashMapOf() private val _profileUrlCache = FragmentedStorage.get("profileUrlCache") private val _scope = CoroutineScope(Dispatchers.IO); - private val _taskGetProfile = BatchedTaskHandler(_scope, { system -> + private val _taskGetProfile = BatchedTaskHandler(_scope, + { system -> val signedProfileEvents = ApiMethods.getQueryLatest( SERVER, system.toProto(), @@ -150,7 +153,7 @@ class PolycentricCache { return null } - if (!ignoreExpired && cached.creationTime.getNowDiffSeconds() > _cacheExpirationSeconds) { + if (!ignoreExpired && cached.expired) { return null; } @@ -188,7 +191,7 @@ class PolycentricCache { fun getCachedProfile(url: String, ignoreExpired: Boolean = false): CachedPolycentricProfile? { synchronized (_profileCache) { val cached = _profileUrlCache.get(url) ?: return null; - if (!ignoreExpired && cached.creationTime.getNowDiffSeconds() > _cacheExpirationSeconds) { + if (!ignoreExpired && cached.expired) { return null; } @@ -199,7 +202,7 @@ class PolycentricCache { fun getCachedProfile(system: PublicKey, ignoreExpired: Boolean = false): CachedPolycentricProfile? { synchronized(_profileCache) { val cached = _profileCache[system] ?: return null; - if (!ignoreExpired && cached.creationTime.getNowDiffSeconds() > _cacheExpirationSeconds) { + if (!ignoreExpired && cached.expired) { return null; } @@ -281,6 +284,7 @@ class PolycentricCache { private const val TAG = "PolycentricCache" const val SERVER = "https://srv1-stg.polycentric.io" private var _instance: PolycentricCache? = null; + private val CACHE_EXPIRATION_SECONDS = 60 * 60 * 3; @JvmStatic val instance: PolycentricCache diff --git a/app/src/main/java/com/futo/platformplayer/views/adapters/SubscriptionViewHolder.kt b/app/src/main/java/com/futo/platformplayer/views/adapters/SubscriptionViewHolder.kt index 80f10782..62918eed 100644 --- a/app/src/main/java/com/futo/platformplayer/views/adapters/SubscriptionViewHolder.kt +++ b/app/src/main/java/com/futo/platformplayer/views/adapters/SubscriptionViewHolder.kt @@ -84,6 +84,9 @@ class SubscriptionViewHolder : ViewHolder { val cachedProfile = PolycentricCache.instance.getCachedProfile(sub.channel.url, true); if (cachedProfile != null) { onProfileLoaded(sub, cachedProfile, false); + if (cachedProfile.expired) { + _taskLoadProfile.run(sub.channel.id); + } } else { _creatorThumbnail.setThumbnail(sub.channel.thumbnail, false); _taskLoadProfile.run(sub.channel.id); diff --git a/app/src/main/java/com/futo/platformplayer/views/adapters/feedtypes/PreviewVideoView.kt b/app/src/main/java/com/futo/platformplayer/views/adapters/feedtypes/PreviewVideoView.kt index 8bc3770e..6ddf89c4 100644 --- a/app/src/main/java/com/futo/platformplayer/views/adapters/feedtypes/PreviewVideoView.kt +++ b/app/src/main/java/com/futo/platformplayer/views/adapters/feedtypes/PreviewVideoView.kt @@ -178,6 +178,9 @@ open class PreviewVideoView : LinearLayout { val cachedProfile = PolycentricCache.instance.getCachedProfile(content.author.url, true); if (cachedProfile != null) { onProfileLoaded(cachedProfile, false); + if (cachedProfile.expired) { + _taskLoadProfile.run(content.author.id); + } } else { _imageNeopassChannel?.visibility = View.GONE; _creatorThumbnail?.setThumbnail(content.author.thumbnail, false); diff --git a/app/src/main/java/com/futo/platformplayer/views/adapters/viewholders/CreatorViewHolder.kt b/app/src/main/java/com/futo/platformplayer/views/adapters/viewholders/CreatorViewHolder.kt index 487ac8e7..9a59debb 100644 --- a/app/src/main/java/com/futo/platformplayer/views/adapters/viewholders/CreatorViewHolder.kt +++ b/app/src/main/java/com/futo/platformplayer/views/adapters/viewholders/CreatorViewHolder.kt @@ -68,6 +68,9 @@ class CreatorViewHolder(private val _viewGroup: ViewGroup, private val _tiny: Bo val cachedProfile = PolycentricCache.instance.getCachedProfile(authorLink.url, true); if (cachedProfile != null) { onProfileLoaded(cachedProfile, false); + if (cachedProfile.expired) { + _taskLoadProfile.run(authorLink.id); + } } else { _creatorThumbnail.setThumbnail(authorLink.thumbnail, false); _taskLoadProfile.run(authorLink.id); diff --git a/app/src/main/java/com/futo/platformplayer/views/adapters/viewholders/SubscriptionBarViewHolder.kt b/app/src/main/java/com/futo/platformplayer/views/adapters/viewholders/SubscriptionBarViewHolder.kt index 1b2d9c37..fe2cb7e0 100644 --- a/app/src/main/java/com/futo/platformplayer/views/adapters/viewholders/SubscriptionBarViewHolder.kt +++ b/app/src/main/java/com/futo/platformplayer/views/adapters/viewholders/SubscriptionBarViewHolder.kt @@ -54,6 +54,9 @@ class SubscriptionBarViewHolder(private val _viewGroup: ViewGroup) : AnyAdapter. val cachedProfile = PolycentricCache.instance.getCachedProfile(subscription.channel.url, true); if (cachedProfile != null) { onProfileLoaded(cachedProfile, false); + if (cachedProfile.expired) { + _taskLoadProfile.run(subscription.channel.id); + } } else { _creatorThumbnail.setThumbnail(subscription.channel.thumbnail, false); _taskLoadProfile.run(subscription.channel.id);