Skip to content

Commit 00b52ae

Browse files
Add cache to get playername from db instead of mojang cache (#357)
1 parent e22ce3e commit 00b52ae

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[versions]
2-
minecraft = "1.21.11-rc2"
2+
minecraft = "1.21.11"
33
fabric-loader = "0.18.1"
44

55
fabric-api = "0.139.4+1.21.11"
@@ -9,7 +9,7 @@ kotlin = "2.2.0"
99
# Also modrinth version in gradle.properties
1010
fabric-kotlin = "1.13.4+kotlin.2.2.0"
1111

12-
fabric-permissions = "0.6.0-patbox"
12+
fabric-permissions = "0.6.1"
1313
translations = "2.5.2+1.21.9-pre3"
1414

1515
exposed = "1.0.0-rc-2"

src/main/kotlin/com/github/quiltservertools/ledger/database/DatabaseCacheService.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.github.quiltservertools.ledger.database
33
import com.google.common.collect.BiMap
44
import com.google.common.collect.HashBiMap
55
import net.minecraft.resources.Identifier
6-
import java.util.UUID
6+
import java.util.*
77

88
object DatabaseCacheService {
99
val actionIdentifierKeys: BiMap<String, Int> = HashBiMap.create()
@@ -15,4 +15,6 @@ object DatabaseCacheService {
1515
val sourceKeys: BiMap<String, Int> = HashBiMap.create()
1616

1717
val playerKeys: BiMap<UUID, Int> = HashBiMap.create()
18+
19+
val playernameKeys: BiMap<String, Int> = HashBiMap.create()
1820
}

src/main/kotlin/com/github/quiltservertools/ledger/database/DatabaseManager.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,20 @@ object DatabaseManager {
127127
suspend fun setupCache() {
128128
execute {
129129
Tables.ActionIdentifier.all().forEach {
130-
cache.actionIdentifierKeys.put(it.identifier, it.id.value)
130+
cache.actionIdentifierKeys[it.identifier] = it.id.value
131131
}
132132
Tables.World.all().forEach {
133-
cache.worldIdentifierKeys.put(it.identifier, it.id.value)
133+
cache.worldIdentifierKeys[it.identifier] = it.id.value
134134
}
135135
Tables.ObjectIdentifier.all().forEach {
136-
cache.objectIdentifierKeys.put(it.identifier, it.id.value)
136+
cache.objectIdentifierKeys[it.identifier] = it.id.value
137137
}
138138
Tables.Source.all().forEach {
139-
cache.sourceKeys.put(it.name, it.id.value)
139+
cache.sourceKeys[it.name] = it.id.value
140140
}
141141
Tables.Player.all().forEach {
142-
cache.playerKeys.put(it.playerId, it.id.value)
142+
cache.playerKeys[it.playerId] = it.id.value
143+
cache.playernameKeys[it.playerName] = it.id.value
143144
}
144145
}
145146
}
@@ -220,6 +221,7 @@ object DatabaseManager {
220221
val objectIdentifierCache = DatabaseCacheService.objectIdentifierKeys.inverse()
221222
val sourceCache = DatabaseCacheService.sourceKeys.inverse()
222223
val playerCache = DatabaseCacheService.playerKeys.inverse()
224+
val playerNameCache = DatabaseCacheService.playernameKeys.inverse()
223225

224226
for (action in query) {
225227
val typeSupplier = ActionRegistry.getType(
@@ -241,7 +243,7 @@ object DatabaseManager {
241243
type.oldObjectState = action[Tables.Actions.oldBlockState]
242244
type.sourceName = sourceCache[action[Tables.Actions.sourceName].value]!!
243245
type.sourceProfile = action.getOrNull(Tables.Actions.sourcePlayer)?.let {
244-
Ledger.server.services().nameToIdCache?.get(playerCache[it.value]!!)?.orElse(null)
246+
NameAndId(playerCache[it.value]!!, playerNameCache[it.value]!!)
245247
}
246248
type.extraData = action[Tables.Actions.extraData]
247249
type.rolledBack = action[Tables.Actions.rolledBack]
@@ -494,11 +496,14 @@ object DatabaseManager {
494496
if (player != null) {
495497
player.lastJoin = Instant.now()
496498
player.playerName = name
499+
cache.playernameKeys[name] = player.id.value
497500
} else {
498-
Tables.Player.new {
501+
val entity = Tables.Player.new {
499502
this.playerId = uuid
500503
this.playerName = name
501504
}
505+
cache.playerKeys[uuid] = entity.id.value
506+
cache.playernameKeys[name] = entity.id.value
502507
}
503508
}
504509

0 commit comments

Comments
 (0)