Skip to content

Commit d5d8cdb

Browse files
authored
Merge pull request #264 from pylonmc/config-caching
Add config caching
2 parents 7524a2a + 26bd08f commit d5d8cdb

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/addon/PylonAddon.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ interface PylonAddon : Keyed {
8989
fun mergeGlobalConfig(from: String, to: String): Config {
9090
require(from.endsWith(".yml")) { "Config file must be a YAML file" }
9191
require(to.endsWith(".yml")) { "Config file must be a YAML file" }
92+
val cached = globalConfigCache[from to to]
93+
if (cached != null) {
94+
return cached
95+
}
9296
val globalConfig = PylonCore.dataFolder.resolve(to)
9397
if (!globalConfig.exists()) {
9498
globalConfig.parentFile.mkdirs()
@@ -105,10 +109,13 @@ interface PylonAddon : Keyed {
105109
config.merge(ConfigSection(newConfig))
106110
config.save()
107111
}
112+
globalConfigCache[from to to] = config
108113
return config
109114
}
110115

111116
companion object : Listener {
117+
private val globalConfigCache: MutableMap<Pair<String, String>, Config> = mutableMapOf()
118+
112119
@EventHandler
113120
private fun onPluginDisable(event: PluginDisableEvent) {
114121
val plugin = event.plugin

pylon-core/src/main/kotlin/io/github/pylonmc/pylon/core/config/ConfigSection.kt

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import kotlin.reflect.KProperty
1313

1414
open class ConfigSection(val internalSection: ConfigurationSection) {
1515

16+
private val cache: MutableMap<String, Any?> = mutableMapOf()
17+
1618
val keys: Set<String>
1719
get() = internalSection.getKeys(false)
1820

@@ -25,18 +27,38 @@ open class ConfigSection(val internalSection: ConfigurationSection) {
2527
}
2628

2729
fun getSection(key: String): ConfigSection? {
30+
val cached = cache[key]
31+
if (cached != null) {
32+
if (cached !is ConfigSection) {
33+
error("$key is not a config section")
34+
}
35+
return cached
36+
}
37+
2838
val newConfig = internalSection.getConfigurationSection(key) ?: return null
29-
return ConfigSection(newConfig)
39+
val configSection = ConfigSection(newConfig)
40+
cache[key] = configSection
41+
return configSection
3042
}
3143

3244
fun getSectionOrThrow(key: String): ConfigSection =
3345
getSection(key) ?: throw KeyNotFoundException(internalSection.currentPath, key)
3446

3547
fun getFluid(key: String): PylonFluid? {
48+
val cached = cache[key]
49+
if (cached != null) {
50+
if (cached !is PylonFluid) {
51+
return null
52+
}
53+
return cached
54+
}
55+
3656
val name = get<String>(key) ?: return null
37-
return PylonRegistry.FLUIDS[
57+
val fluid = PylonRegistry.FLUIDS[
3858
NamespacedKey.fromString(name) ?: error("'$name' is not a namespaced key")
3959
]
60+
cache[key] = fluid
61+
return fluid
4062
}
4163

4264
fun getFluidOrThrow(key: String): PylonFluid {
@@ -47,8 +69,18 @@ open class ConfigSection(val internalSection: ConfigurationSection) {
4769
}
4870

4971
fun getMaterial(key: String): Material? {
72+
val cached = cache[key]
73+
if (cached != null) {
74+
if (cached !is Material) {
75+
return null
76+
}
77+
return cached
78+
}
79+
5080
val name = get<String>(key) ?: return null
51-
return Material.getMaterial(name.uppercase())
81+
val material = Material.getMaterial(name.uppercase())
82+
cache[key] = material
83+
return material
5284
}
5385

5486
fun getMaterialOrThrow(key: String): Material {
@@ -64,7 +96,17 @@ open class ConfigSection(val internalSection: ConfigurationSection) {
6496
?: error("No such item $key")
6597

6698
fun <T> get(key: String, type: Class<out T>): T? {
99+
val cached = cache[key]
100+
if (cached != null) {
101+
if (!type.isInstance(cached)) {
102+
return null
103+
}
104+
@Suppress("UNCHECKED_CAST")
105+
return cached as T?
106+
}
107+
67108
val value = internalSection.get(key) ?: return null
109+
cache[key] = value
68110
return type.cast(value)
69111
}
70112

0 commit comments

Comments
 (0)