9
0
mirror of https://github.com/Auxilor/EcoJobs.git synced 2026-01-06 15:51:59 +00:00

Improve cache further...

This commit is contained in:
NicoNeko
2025-12-18 23:25:15 +02:00
parent 758fcee9f7
commit 1502da603b
4 changed files with 74 additions and 43 deletions

View File

@@ -6,6 +6,7 @@ import com.willfp.eco.core.placeholder.context.placeholderContext
import com.willfp.eco.util.formatEco
import com.willfp.eco.util.savedDisplayName
import com.willfp.ecojobs.jobs.Jobs
import com.willfp.ecojobs.jobs.JobsLeaderboard.getTop
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.util.StringUtil
@@ -30,7 +31,7 @@ class CommandTop(plugin: EcoPlugin) : Subcommand(plugin, "top", "ecojobs.command
val offset = (page - 1) * 10
val positions = (offset + 1..offset + 10).toList()
val top = positions.mapNotNull { job.getTop(it) }
val top = positions.mapNotNull { getTop(job, it) }
val messages = plugin.langYml.getStrings("top.format").toMutableList()
val lines = mutableListOf<String>()

View File

@@ -4,6 +4,7 @@ import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.placeholder.RegistrablePlaceholder
import com.willfp.eco.core.placeholder.context.PlaceholderContext
import com.willfp.eco.util.savedDisplayName
import com.willfp.ecojobs.jobs.JobsLeaderboard.getTop
import java.util.regex.Pattern
class EcoJobsJobTopPlaceholder(
@@ -29,8 +30,8 @@ class EcoJobsJobTopPlaceholder(
val job = Jobs.getByID(jobId) ?: return null
return when (type) {
"name" -> job.getTop(place)?.player?.savedDisplayName ?: emptyPosition
"level", "amount" -> job.getTop(place)?.level?.toString() ?: emptyPosition
"name" -> getTop(job, place)?.player?.savedDisplayName ?: emptyPosition
"level", "amount" -> getTop(job, place)?.level?.toString() ?: emptyPosition
else -> null
}
}

View File

@@ -27,6 +27,7 @@ import com.willfp.ecojobs.api.getJobXP
import com.willfp.ecojobs.api.getJobXPRequired
import com.willfp.ecojobs.api.hasJobActive
import com.willfp.ecojobs.api.jobLimit
import com.willfp.ecojobs.jobs.JobsLeaderboard.getPosition
import com.willfp.ecojobs.util.LeaderboardCacheEntry
import com.willfp.ecojobs.util.LevelInjectable
import com.willfp.libreforge.ViolationContext
@@ -50,16 +51,6 @@ class Job(
val config: Config,
private val plugin: EcoJobsPlugin
) : Registrable {
private val topCache = mutableMapOf<Int, LeaderboardCacheEntry>()
private var topCacheLastUpdate: Long = System.currentTimeMillis()
private var topCacheNextUpdate: Long =
System.currentTimeMillis() + Duration.ofSeconds(plugin.configYml.getInt("leaderboard-cache-lifetime").toLong())
.toMillis()
private val posCache = mutableMapOf<UUID, Int>()
private var posCacheLastUpdate: Long = System.currentTimeMillis()
private var posCacheNextUpdate: Long =
System.currentTimeMillis() + Duration.ofSeconds(plugin.configYml.getInt("leaderboard-cache-lifetime").toLong())
.toMillis()
val name = config.getFormattedString("name")
@@ -199,7 +190,7 @@ class Job(
plugin, "${id}_leaderboard_rank"
) { player ->
val emptyPosition = plugin.langYml.getString("top.empty-position")
val position = getPosition(player.uniqueId)
val position = getPosition(this, player.uniqueId)
position?.toString() ?: emptyPosition
}.register()
}
@@ -341,7 +332,7 @@ class Job(
.replace("%leave_price%", this.leavePrice.getDisplay(player))
.replace(
"%rank%",
this.getPosition(player.uniqueId)?.toString() ?: plugin.langYml.getString("top.empty-position")
getPosition(this, player.uniqueId)?.toString() ?: plugin.langYml.getString("top.empty-position")
)
val level = forceLevel ?: player.getJobLevel(this)
@@ -456,34 +447,6 @@ class Job(
}
}
fun getTop(place: Int): LeaderboardCacheEntry? {
if (topCacheNextUpdate <= topCacheLastUpdate +
Duration.ofSeconds(plugin.configYml.getInt("leaderboard-cache-lifetime").toLong()).toMillis()
) {
topCacheLastUpdate = System.currentTimeMillis()
topCacheNextUpdate = topCacheLastUpdate +
Duration.ofSeconds(plugin.configYml.getInt("leaderboard-cache-lifetime").toLong()).toMillis()
topCache.clear()
topCache.putAll(Bukkit.getOfflinePlayers().sortedByDescending { it.getJobLevel(this) }
.mapIndexed { place, player -> place + 1 to LeaderboardCacheEntry(player, player.getJobLevel(this)) })
}
return topCache[place]
}
fun getPosition(uuid: UUID): Int? {
if (posCacheNextUpdate <= posCacheLastUpdate +
Duration.ofSeconds(plugin.configYml.getInt("leaderboard-cache-lifetime").toLong()).toMillis()
) {
posCacheLastUpdate = System.currentTimeMillis()
posCacheNextUpdate = posCacheLastUpdate +
Duration.ofSeconds(plugin.configYml.getInt("leaderboard-cache-lifetime").toLong()).toMillis()
posCache.clear()
posCache.putAll(Bukkit.getOfflinePlayers().sortedByDescending { it.getJobLevel(this) }
.map { it.uniqueId to it.getJobLevel(this) })
}
return posCache[uuid]?.plus(1)
}
override fun getID(): String {
return this.id
}

View File

@@ -0,0 +1,66 @@
package com.willfp.ecojobs.jobs
import com.willfp.eco.core.tuples.Pair
import com.willfp.ecojobs.EcoJobsPlugin
import com.willfp.ecojobs.api.getJobLevel
import com.willfp.ecojobs.util.LeaderboardCacheEntry
import org.bukkit.Bukkit
import java.time.Duration
import java.util.*
object JobsLeaderboard {
private val topCache = mutableMapOf<Pair<Job, Int>, LeaderboardCacheEntry>()
private var topCacheLastUpdate: Long = System.currentTimeMillis()
private var topCacheNextUpdate: Long =
System.currentTimeMillis() + Duration.ofSeconds(
EcoJobsPlugin.instance.configYml.getInt("leaderboard-cache-lifetime").toLong()
).toMillis()
private val posCache = mutableMapOf<Pair<Job, UUID>, Int>()
private var posCacheLastUpdate: Long = System.currentTimeMillis()
private var posCacheNextUpdate: Long =
System.currentTimeMillis() + Duration.ofSeconds(
EcoJobsPlugin.instance.configYml.getInt("leaderboard-cache-lifetime").toLong()
).toMillis()
fun getTop(job: Job, place: Int): LeaderboardCacheEntry? {
if (topCacheNextUpdate <= topCacheLastUpdate +
Duration.ofSeconds(EcoJobsPlugin.instance.configYml.getInt("leaderboard-cache-lifetime").toLong())
.toMillis()
) {
topCacheLastUpdate = System.currentTimeMillis()
topCacheNextUpdate = topCacheLastUpdate +
Duration.ofSeconds(EcoJobsPlugin.instance.configYml.getInt("leaderboard-cache-lifetime").toLong())
.toMillis()
topCache.clear()
val offlinePlayers = Bukkit.getOfflinePlayers()
for (job in Jobs.values())
topCache.putAll(offlinePlayers.sortedByDescending { it.getJobLevel(job) }
.mapIndexed { place, player ->
Pair(job, place + 1) to LeaderboardCacheEntry(
player,
player.getJobLevel(job)
)
})
}
return topCache[Pair(job, place)]
}
fun getPosition(job: Job, uuid: UUID): Int? {
if (posCacheNextUpdate <= posCacheLastUpdate +
Duration.ofSeconds(EcoJobsPlugin.instance.configYml.getInt("leaderboard-cache-lifetime").toLong())
.toMillis()
) {
posCacheLastUpdate = System.currentTimeMillis()
posCacheNextUpdate = posCacheLastUpdate +
Duration.ofSeconds(EcoJobsPlugin.instance.configYml.getInt("leaderboard-cache-lifetime").toLong())
.toMillis()
posCache.clear()
val offlinePlayers = Bukkit.getOfflinePlayers()
for (job in Jobs.values())
posCache.putAll(offlinePlayers.sortedByDescending { it.getJobLevel(job) }
.map { Pair(job, it.uniqueId) to it.getJobLevel(job) })
}
return posCache[Pair(job, uuid)]?.plus(1)
}
}