fixes in CachedRatingsRepo

This commit is contained in:
InsanusMokrassar 2024-05-13 21:25:13 +06:00
parent e6a0d67444
commit aa6c62d66e
2 changed files with 14 additions and 14 deletions

View File

@ -71,11 +71,13 @@ object Plugin : Plugin {
autoClearLogger.i { "Start autoclear" } autoClearLogger.i { "Start autoclear" }
val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds
autoClearLogger.i { "Posts drop created before: ${dropCreatedBefore.toStringDefault()}" } autoClearLogger.i { "Posts drop created before: ${dropCreatedBefore.toStringDefault()}" }
val idsToDelete = ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.filter { val idsToDelete = ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.also {
autoClearLogger.i { "Selected posts by rating: $it" }
}.filter {
val postCreationDateTime = postsRepo.getPostCreationTime(it) ?: (dropCreatedBefore - 1.seconds) // do dropping if post creation time is not available val postCreationDateTime = postsRepo.getPostCreationTime(it) ?: (dropCreatedBefore - 1.seconds) // do dropping if post creation time is not available
postCreationDateTime < dropCreatedBefore postCreationDateTime < dropCreatedBefore
} }
autoClearLogger.i { "Posts to drop: $idsToDelete" } autoClearLogger.i { "Filtered posts by datetime: $idsToDelete" }
if (idsToDelete.isNotEmpty()) { if (idsToDelete.isNotEmpty()) {
runCatching { ratingsRepo.unset(idsToDelete) } runCatching { ratingsRepo.unset(idsToDelete) }
autoClearLogger.i { "Ratings dropped" } autoClearLogger.i { "Ratings dropped" }

View File

@ -1,6 +1,8 @@
package dev.inmo.plaguposter.ratings.repo package dev.inmo.plaguposter.ratings.repo
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.repos.KeyValueRepo import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.MapKeyValueRepo import dev.inmo.micro_utils.repos.MapKeyValueRepo
import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo
@ -19,20 +21,16 @@ class CachedRatingsRepo(
count: Int?, count: Int?,
exclude: List<PostId> exclude: List<PostId>
): Map<PostId, Rating> { ): Map<PostId, Rating> {
val result = mutableMapOf<PostId, Rating>() return kvCache.getAll().filter { (it, rating) ->
it !in exclude && rating in range
doForAllWithNextPaging { }.let {
kvCache.keys(it).also { if (count == null) {
it.results.forEach { it
val rating = get(it) ?: return@forEach } else {
if (it !in exclude && rating in range) { val keys = it.keys.optionallyReverse(reversed).take(count)
result[it] = rating keys.associateWith { id -> it.getValue(id) }
}
}
} }
} }
return result.toMap()
} }
override suspend fun getPostsWithRatingGreaterEq( override suspend fun getPostsWithRatingGreaterEq(