mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2025-09-15 13:09:29 +00:00
add caches
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package dev.inmo.plaguposter.ratings.source.repos
|
||||
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.cache.KeyValueCacheRepo
|
||||
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
||||
import dev.inmo.micro_utils.repos.cache.full.cached
|
||||
import dev.inmo.plaguposter.common.ShortMessageInfo
|
||||
import dev.inmo.tgbotapi.types.PollIdentifier
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
class CachedPollsToMessagesInfoRepo(
|
||||
private val repo: PollsToMessagesInfoRepo,
|
||||
private val scope: CoroutineScope,
|
||||
private val kvCache: FullKVCache<PollIdentifier, ShortMessageInfo> = FullKVCache()
|
||||
) : PollsToMessagesInfoRepo, KeyValueRepo<PollIdentifier, ShortMessageInfo> by repo.cached(kvCache, scope)
|
@@ -0,0 +1,15 @@
|
||||
package dev.inmo.plaguposter.ratings.source.repos
|
||||
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
||||
import dev.inmo.micro_utils.repos.cache.full.cached
|
||||
import dev.inmo.plaguposter.common.ShortMessageInfo
|
||||
import dev.inmo.plaguposter.posts.models.PostId
|
||||
import dev.inmo.tgbotapi.types.PollIdentifier
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
class CachedPollsToPostsIdsRepo(
|
||||
private val repo: PollsToPostsIdsRepo,
|
||||
private val scope: CoroutineScope,
|
||||
private val kvCache: FullKVCache<PollIdentifier, PostId> = FullKVCache()
|
||||
) : PollsToPostsIdsRepo, KeyValueRepo<PollIdentifier, PostId> by repo.cached(kvCache, scope)
|
@@ -67,8 +67,29 @@ object Plugin : Plugin {
|
||||
get<Json>().decodeFromJsonElement(Config.serializer(), params["ratingsPolls"] ?: error("Unable to load config for rating polls in $params"))
|
||||
}
|
||||
single<RatingsVariants>(ratingVariantsQualifier) { get<Config>().variants }
|
||||
single<PollsToPostsIdsRepo> { ExposedPollsToPostsIdsRepo(database) }
|
||||
single<PollsToMessagesInfoRepo> { ExposedPollsToMessagesInfoRepo(database) }
|
||||
|
||||
single { ExposedPollsToPostsIdsRepo(database) }
|
||||
single<PollsToPostsIdsRepo> {
|
||||
val base = get<ExposedPollsToPostsIdsRepo>()
|
||||
|
||||
if (useCache) {
|
||||
CachedPollsToPostsIdsRepo(base, get())
|
||||
} else {
|
||||
base
|
||||
}
|
||||
}
|
||||
|
||||
single { ExposedPollsToMessagesInfoRepo(database) }
|
||||
single<PollsToMessagesInfoRepo> {
|
||||
val base = get<ExposedPollsToMessagesInfoRepo>()
|
||||
|
||||
if (useCache) {
|
||||
CachedPollsToMessagesInfoRepo(base, get())
|
||||
} else {
|
||||
base
|
||||
}
|
||||
}
|
||||
|
||||
single<VariantTransformer> {
|
||||
val ratingsSettings = get<RatingsVariants>(ratingVariantsQualifier)
|
||||
VariantTransformer {
|
||||
|
61
ratings/src/commonMain/kotlin/repo/CachedRatingsRepo.kt
Normal file
61
ratings/src/commonMain/kotlin/repo/CachedRatingsRepo.kt
Normal file
@@ -0,0 +1,61 @@
|
||||
package dev.inmo.plaguposter.ratings.repo
|
||||
|
||||
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
||||
import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo
|
||||
import dev.inmo.plaguposter.posts.models.PostId
|
||||
import dev.inmo.plaguposter.ratings.models.Rating
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
class CachedRatingsRepo(
|
||||
private val base: RatingsRepo,
|
||||
private val scope: CoroutineScope,
|
||||
private val kvCache: FullKVCache<PostId, Rating> = FullKVCache()
|
||||
) : RatingsRepo, KeyValueRepo<PostId, Rating> by FullKeyValueCacheRepo(base, kvCache, scope) {
|
||||
override suspend fun getPosts(
|
||||
range: ClosedRange<Rating>,
|
||||
reversed: Boolean,
|
||||
count: Int?,
|
||||
exclude: List<PostId>
|
||||
): Map<PostId, Rating> {
|
||||
val result = mutableMapOf<PostId, Rating>()
|
||||
|
||||
doForAllWithNextPaging {
|
||||
kvCache.keys(it).also {
|
||||
it.results.forEach {
|
||||
val rating = kvCache.get(it) ?: return@forEach
|
||||
if (it !in exclude && rating in range) {
|
||||
result[it] = rating
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.toMap()
|
||||
}
|
||||
|
||||
override suspend fun getPostsWithRatingGreaterEq(
|
||||
then: Rating,
|
||||
reversed: Boolean,
|
||||
count: Int?,
|
||||
exclude: List<PostId>
|
||||
): Map<PostId, Rating> = getPosts(
|
||||
then .. Rating(Double.MAX_VALUE),
|
||||
reversed,
|
||||
count,
|
||||
exclude
|
||||
)
|
||||
|
||||
override suspend fun getPostsWithRatingLessEq(
|
||||
then: Rating,
|
||||
reversed: Boolean,
|
||||
count: Int?,
|
||||
exclude: List<PostId>
|
||||
): Map<PostId, Rating> = getPosts(
|
||||
Rating(Double.MIN_VALUE) .. then,
|
||||
reversed,
|
||||
count,
|
||||
exclude
|
||||
)
|
||||
}
|
@@ -1,8 +1,10 @@
|
||||
package dev.inmo.plaguposter.ratings
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.micro_utils.koin.singleWithBinds
|
||||
import dev.inmo.micro_utils.repos.unset
|
||||
import dev.inmo.plagubot.Plugin
|
||||
import dev.inmo.plaguposter.common.useCache
|
||||
import dev.inmo.plaguposter.posts.exposed.ExposedPostsRepo
|
||||
import dev.inmo.plaguposter.posts.repo.PostsRepo
|
||||
import dev.inmo.plaguposter.ratings.exposed.ExposedRatingsRepo
|
||||
@@ -16,11 +18,16 @@ import org.koin.dsl.binds
|
||||
|
||||
object Plugin : Plugin {
|
||||
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||
single { ExposedRatingsRepo(database) } binds arrayOf(
|
||||
RatingsRepo::class,
|
||||
ReadRatingsRepo::class,
|
||||
WriteRatingsRepo::class,
|
||||
)
|
||||
single { ExposedRatingsRepo(database) }
|
||||
singleWithBinds<RatingsRepo> {
|
||||
val base = get<ExposedRatingsRepo>()
|
||||
|
||||
if (useCache) {
|
||||
CachedRatingsRepo(base, get())
|
||||
} else {
|
||||
base
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||
|
Reference in New Issue
Block a user