Skip to content

Commit a11dc49

Browse files
committed
Fix version resultion sorting
Set cache expiry to 100 days
1 parent 0c8d217 commit a11dc49

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

core/src/main/kotlin/org/sinytra/probe/core/platform/ModrinthAPI.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.ktor.client.plugins.*
99
import io.ktor.client.plugins.contentnegotiation.*
1010
import io.ktor.client.plugins.logging.*
1111
import io.ktor.client.plugins.resources.*
12+
import io.ktor.client.plugins.resources.Resources
1213
import io.ktor.client.request.*
1314
import io.ktor.http.*
1415
import io.ktor.resources.*
@@ -133,8 +134,8 @@ object ModrinthAPI {
133134
.takeIf { it.status.isSuccess() }
134135
?.body<ModrinthVersion>()
135136

136-
suspend fun getCandidateVersion(projectId: String, gameVersions: List<String>, loader: String): ModrinthVersion? =
137-
client.get(
137+
suspend fun getCandidateVersion(projectId: String, gameVersions: List<String>, loader: String): ModrinthVersion? {
138+
val versions = client.get(
138139
Projects.Id.Version(
139140
Projects.Id(id = projectId),
140141
loaders = "[\"${loader}\"]",
@@ -143,10 +144,17 @@ object ModrinthAPI {
143144
)
144145
.takeIf { it.status.isSuccess() }
145146
?.body<List<ModrinthVersion>>()
146-
?.minByOrNull {
147-
val fileGameVersions = it.gameVersions.sortedBy(gameVersions::indexOf)
148-
gameVersions.indexOf(fileGameVersions.first())
149-
}
147+
148+
if (versions == null) return null
149+
150+
// Priority order is defined by the order inside gameVersions
151+
for (target in gameVersions) {
152+
val match = versions.firstOrNull { target in it.gameVersions }
153+
if (match != null) return match
154+
}
155+
156+
return null
157+
}
150158

151159
suspend fun searchProjects(limit: Int, offset: Int, gameVersion: String, loader: String, excludeLoader: String?): ModrinthSearchResults? =
152160
client

core/src/main/kotlin/org/sinytra/probe/core/platform/ModrinthPlatform.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import kotlinx.coroutines.flow.toList
66
import kotlinx.coroutines.launch
77
import kotlinx.serialization.Serializable
88
import org.sinytra.probe.base.ProjectSearchResult
9-
import org.sinytra.probe.core.platform.ModrinthAPI.ModrinthVersionDependency
10-
import org.sinytra.probe.core.platform.ModrinthAPI.ModrinthVersion
11-
import org.sinytra.probe.core.platform.ModrinthAPI.ModrinthSearchResult
129
import org.sinytra.probe.core.platform.ModrinthAPI.ModrinthProject
10+
import org.sinytra.probe.core.platform.ModrinthAPI.ModrinthSearchResult
11+
import org.sinytra.probe.core.platform.ModrinthAPI.ModrinthVersion
12+
import org.sinytra.probe.core.platform.ModrinthAPI.ModrinthVersionDependency
1313
import org.sinytra.probe.core.service.CacheService
1414
import org.slf4j.LoggerFactory
1515
import java.nio.file.Path

core/src/main/kotlin/org/sinytra/probe/core/service/CacheService.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ import io.lettuce.core.api.coroutines
66
import kotlinx.coroutines.flow.map
77
import kotlinx.coroutines.flow.toList
88
import kotlinx.serialization.json.Json
9+
import java.time.Duration
910

1011
@OptIn(ExperimentalLettuceCoroutinesApi::class)
1112
class CacheService(
1213
redis: StatefulRedisConnection<String, String>
1314
) {
1415
val cmd = redis.coroutines()
16+
val defaultExpiration: Duration = Duration.ofDays(100)
1517

1618
suspend inline fun get(key: String): String? =
1719
cmd.get(key)
1820

19-
suspend inline fun set(key: String, value: String) =
21+
suspend inline fun set(key: String, value: String, expiry: Duration? = defaultExpiration) {
2022
cmd.set(key, value)
23+
if (expiry != null) {
24+
cmd.expire(key, expiry)
25+
}
26+
}
2127

2228
suspend inline fun del(key: String) =
2329
cmd.del(key)
@@ -36,7 +42,10 @@ class CacheService(
3642
suspend inline fun <reified T> getObject(key: String): T? =
3743
cmd.get(key)?.let { Json.decodeFromString<T>(it) }
3844

39-
suspend inline fun <reified T> setObject(key: String, value: T) {
45+
suspend inline fun <reified T> setObject(key: String, value: T, expiry: Duration? = defaultExpiration) {
4046
cmd.set(key, Json.encodeToString(value))
47+
if (expiry != null) {
48+
cmd.expire(key, expiry)
49+
}
4150
}
4251
}

0 commit comments

Comments
 (0)