mirror of
https://gitlab.futo.org/videostreaming/grayjay.git
synced 2025-08-05 07:41:23 +00:00
If cache entry is expired, load it from cache, then reload it from live.
This commit is contained in:
parent
f90290c4ec
commit
08134b4427
5 changed files with 24 additions and 8 deletions
|
@ -7,7 +7,6 @@ import com.futo.platformplayer.constructs.BatchedTaskHandler
|
||||||
import com.futo.platformplayer.fragment.mainactivity.main.PolycentricProfile
|
import com.futo.platformplayer.fragment.mainactivity.main.PolycentricProfile
|
||||||
import com.futo.platformplayer.getNowDiffSeconds
|
import com.futo.platformplayer.getNowDiffSeconds
|
||||||
import com.futo.platformplayer.logging.Logger
|
import com.futo.platformplayer.logging.Logger
|
||||||
import com.futo.platformplayer.resolveChannelUrl
|
|
||||||
import com.futo.platformplayer.resolveChannelUrls
|
import com.futo.platformplayer.resolveChannelUrls
|
||||||
import com.futo.platformplayer.serializers.OffsetDateTimeSerializer
|
import com.futo.platformplayer.serializers.OffsetDateTimeSerializer
|
||||||
import com.futo.platformplayer.stores.CachedPolycentricProfileStorage
|
import com.futo.platformplayer.stores.CachedPolycentricProfileStorage
|
||||||
|
@ -19,17 +18,21 @@ import java.nio.ByteBuffer
|
||||||
import java.time.OffsetDateTime
|
import java.time.OffsetDateTime
|
||||||
|
|
||||||
class PolycentricCache {
|
class PolycentricCache {
|
||||||
data class CachedOwnedClaims(val ownedClaims: List<OwnedClaim>?, val creationTime: OffsetDateTime = OffsetDateTime.now());
|
data class CachedOwnedClaims(val ownedClaims: List<OwnedClaim>?, val creationTime: OffsetDateTime = OffsetDateTime.now()) {
|
||||||
|
val expired get() = creationTime.getNowDiffSeconds() > CACHE_EXPIRATION_SECONDS
|
||||||
|
}
|
||||||
@Serializable
|
@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<PlatformID, CachedOwnedClaims>()
|
private val _cache = hashMapOf<PlatformID, CachedOwnedClaims>()
|
||||||
private val _profileCache = hashMapOf<PublicKey, CachedPolycentricProfile>()
|
private val _profileCache = hashMapOf<PublicKey, CachedPolycentricProfile>()
|
||||||
private val _profileUrlCache = FragmentedStorage.get<CachedPolycentricProfileStorage>("profileUrlCache")
|
private val _profileUrlCache = FragmentedStorage.get<CachedPolycentricProfileStorage>("profileUrlCache")
|
||||||
private val _scope = CoroutineScope(Dispatchers.IO);
|
private val _scope = CoroutineScope(Dispatchers.IO);
|
||||||
|
|
||||||
private val _taskGetProfile = BatchedTaskHandler<PublicKey, CachedPolycentricProfile>(_scope, { system ->
|
private val _taskGetProfile = BatchedTaskHandler<PublicKey, CachedPolycentricProfile>(_scope,
|
||||||
|
{ system ->
|
||||||
val signedProfileEvents = ApiMethods.getQueryLatest(
|
val signedProfileEvents = ApiMethods.getQueryLatest(
|
||||||
SERVER,
|
SERVER,
|
||||||
system.toProto(),
|
system.toProto(),
|
||||||
|
@ -150,7 +153,7 @@ class PolycentricCache {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ignoreExpired && cached.creationTime.getNowDiffSeconds() > _cacheExpirationSeconds) {
|
if (!ignoreExpired && cached.expired) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,7 +191,7 @@ class PolycentricCache {
|
||||||
fun getCachedProfile(url: String, ignoreExpired: Boolean = false): CachedPolycentricProfile? {
|
fun getCachedProfile(url: String, ignoreExpired: Boolean = false): CachedPolycentricProfile? {
|
||||||
synchronized (_profileCache) {
|
synchronized (_profileCache) {
|
||||||
val cached = _profileUrlCache.get(url) ?: return null;
|
val cached = _profileUrlCache.get(url) ?: return null;
|
||||||
if (!ignoreExpired && cached.creationTime.getNowDiffSeconds() > _cacheExpirationSeconds) {
|
if (!ignoreExpired && cached.expired) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +202,7 @@ class PolycentricCache {
|
||||||
fun getCachedProfile(system: PublicKey, ignoreExpired: Boolean = false): CachedPolycentricProfile? {
|
fun getCachedProfile(system: PublicKey, ignoreExpired: Boolean = false): CachedPolycentricProfile? {
|
||||||
synchronized(_profileCache) {
|
synchronized(_profileCache) {
|
||||||
val cached = _profileCache[system] ?: return null;
|
val cached = _profileCache[system] ?: return null;
|
||||||
if (!ignoreExpired && cached.creationTime.getNowDiffSeconds() > _cacheExpirationSeconds) {
|
if (!ignoreExpired && cached.expired) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,6 +284,7 @@ class PolycentricCache {
|
||||||
private const val TAG = "PolycentricCache"
|
private const val TAG = "PolycentricCache"
|
||||||
const val SERVER = "https://srv1-stg.polycentric.io"
|
const val SERVER = "https://srv1-stg.polycentric.io"
|
||||||
private var _instance: PolycentricCache? = null;
|
private var _instance: PolycentricCache? = null;
|
||||||
|
private val CACHE_EXPIRATION_SECONDS = 60 * 60 * 3;
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val instance: PolycentricCache
|
val instance: PolycentricCache
|
||||||
|
|
|
@ -84,6 +84,9 @@ class SubscriptionViewHolder : ViewHolder {
|
||||||
val cachedProfile = PolycentricCache.instance.getCachedProfile(sub.channel.url, true);
|
val cachedProfile = PolycentricCache.instance.getCachedProfile(sub.channel.url, true);
|
||||||
if (cachedProfile != null) {
|
if (cachedProfile != null) {
|
||||||
onProfileLoaded(sub, cachedProfile, false);
|
onProfileLoaded(sub, cachedProfile, false);
|
||||||
|
if (cachedProfile.expired) {
|
||||||
|
_taskLoadProfile.run(sub.channel.id);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_creatorThumbnail.setThumbnail(sub.channel.thumbnail, false);
|
_creatorThumbnail.setThumbnail(sub.channel.thumbnail, false);
|
||||||
_taskLoadProfile.run(sub.channel.id);
|
_taskLoadProfile.run(sub.channel.id);
|
||||||
|
|
|
@ -178,6 +178,9 @@ open class PreviewVideoView : LinearLayout {
|
||||||
val cachedProfile = PolycentricCache.instance.getCachedProfile(content.author.url, true);
|
val cachedProfile = PolycentricCache.instance.getCachedProfile(content.author.url, true);
|
||||||
if (cachedProfile != null) {
|
if (cachedProfile != null) {
|
||||||
onProfileLoaded(cachedProfile, false);
|
onProfileLoaded(cachedProfile, false);
|
||||||
|
if (cachedProfile.expired) {
|
||||||
|
_taskLoadProfile.run(content.author.id);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_imageNeopassChannel?.visibility = View.GONE;
|
_imageNeopassChannel?.visibility = View.GONE;
|
||||||
_creatorThumbnail?.setThumbnail(content.author.thumbnail, false);
|
_creatorThumbnail?.setThumbnail(content.author.thumbnail, false);
|
||||||
|
|
|
@ -68,6 +68,9 @@ class CreatorViewHolder(private val _viewGroup: ViewGroup, private val _tiny: Bo
|
||||||
val cachedProfile = PolycentricCache.instance.getCachedProfile(authorLink.url, true);
|
val cachedProfile = PolycentricCache.instance.getCachedProfile(authorLink.url, true);
|
||||||
if (cachedProfile != null) {
|
if (cachedProfile != null) {
|
||||||
onProfileLoaded(cachedProfile, false);
|
onProfileLoaded(cachedProfile, false);
|
||||||
|
if (cachedProfile.expired) {
|
||||||
|
_taskLoadProfile.run(authorLink.id);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_creatorThumbnail.setThumbnail(authorLink.thumbnail, false);
|
_creatorThumbnail.setThumbnail(authorLink.thumbnail, false);
|
||||||
_taskLoadProfile.run(authorLink.id);
|
_taskLoadProfile.run(authorLink.id);
|
||||||
|
|
|
@ -54,6 +54,9 @@ class SubscriptionBarViewHolder(private val _viewGroup: ViewGroup) : AnyAdapter.
|
||||||
val cachedProfile = PolycentricCache.instance.getCachedProfile(subscription.channel.url, true);
|
val cachedProfile = PolycentricCache.instance.getCachedProfile(subscription.channel.url, true);
|
||||||
if (cachedProfile != null) {
|
if (cachedProfile != null) {
|
||||||
onProfileLoaded(cachedProfile, false);
|
onProfileLoaded(cachedProfile, false);
|
||||||
|
if (cachedProfile.expired) {
|
||||||
|
_taskLoadProfile.run(subscription.channel.id);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_creatorThumbnail.setThumbnail(subscription.channel.thumbnail, false);
|
_creatorThumbnail.setThumbnail(subscription.channel.thumbnail, false);
|
||||||
_taskLoadProfile.run(subscription.channel.id);
|
_taskLoadProfile.run(subscription.channel.id);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue