mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2025-12-08 05:55:40 +00:00
add caches
This commit is contained in:
49
posts/src/jvmMain/kotlin/cached/CachedPostsRepo.kt
Normal file
49
posts/src/jvmMain/kotlin/cached/CachedPostsRepo.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
package dev.inmo.plaguposter.posts.cached
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import dev.inmo.micro_utils.pagination.FirstPagePagination
|
||||
import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination
|
||||
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||
import dev.inmo.micro_utils.repos.CRUDRepo
|
||||
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
||||
import dev.inmo.micro_utils.repos.cache.full.FullCRUDCacheRepo
|
||||
import dev.inmo.plaguposter.posts.models.NewPost
|
||||
import dev.inmo.plaguposter.posts.models.PostContentInfo
|
||||
import dev.inmo.plaguposter.posts.models.PostId
|
||||
import dev.inmo.plaguposter.posts.models.RegisteredPost
|
||||
import dev.inmo.plaguposter.posts.repo.PostsRepo
|
||||
import dev.inmo.tgbotapi.types.IdChatIdentifier
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class CachedPostsRepo(
|
||||
private val parentRepo: PostsRepo,
|
||||
private val scope: CoroutineScope,
|
||||
private val kvCache: FullKVCache<PostId, RegisteredPost> = FullKVCache()
|
||||
) : PostsRepo, CRUDRepo<RegisteredPost, PostId, NewPost> by FullCRUDCacheRepo(
|
||||
parentRepo,
|
||||
kvCache,
|
||||
scope,
|
||||
{ it.id }
|
||||
) {
|
||||
override val removedPostsFlow: Flow<RegisteredPost> by parentRepo::removedPostsFlow
|
||||
|
||||
override suspend fun getIdByChatAndMessage(chatId: IdChatIdentifier, messageId: MessageIdentifier): PostId? {
|
||||
doForAllWithNextPaging(firstPageWithOneElementPagination) {
|
||||
kvCache.values(it).also {
|
||||
it.results.forEach {
|
||||
return it.takeIf {
|
||||
it.content.any { it.chatId == chatId && it.messageId == messageId }
|
||||
} ?.id ?: return@forEach
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
override suspend fun getPostCreationTime(postId: PostId): DateTime? = kvCache.get(postId) ?.created
|
||||
|
||||
override suspend fun getFirstMessageInfo(postId: PostId): PostContentInfo? = kvCache.get(postId) ?.content ?.firstOrNull()
|
||||
}
|
||||
Reference in New Issue
Block a user