Skip to content

Commit

Permalink
creating and using TimeLimitedCache and TimeLimitedSet instead of gua…
Browse files Browse the repository at this point in the history
…va cache.
  • Loading branch information
hannibal002 committed Jan 20, 2024
1 parent dd86bba commit 680b203
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 37 deletions.
10 changes: 4 additions & 6 deletions src/main/java/at/hannibal2/skyhanni/data/SlayerAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ import at.hannibal2.skyhanni.utils.NEUItems.getNpcPriceOrNull
import at.hannibal2.skyhanni.utils.NEUItems.getPrice
import at.hannibal2.skyhanni.utils.NumberUtil
import at.hannibal2.skyhanni.utils.RecalculatingValue
import com.google.common.cache.CacheBuilder
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

object SlayerAPI {

private var nameCache =
CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES)
.build<Pair<NEUInternalName, Int>, Pair<String, Double>>()
private var nameCache = TimeLimitedCache<Pair<NEUInternalName, Int>, Pair<String, Double>>(1.minutes)

var questStartTime = 0L
var isInCorrectArea = false
Expand All @@ -43,7 +41,7 @@ object SlayerAPI {

fun getItemNameAndPrice(internalName: NEUInternalName, amount: Int): Pair<String, Double> {
val key = internalName to amount
nameCache.getIfPresent(key)?.let {
nameCache.getOrNull(key)?.let {
return it
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.RenderUtils.drawString
import at.hannibal2.skyhanni.utils.RenderUtils.exactLocation
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import com.google.common.cache.CacheBuilder
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import net.minecraft.entity.item.EntityItem
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.milliseconds

class ShowFishingItemName {
private val config get() = SkyHanniMod.feature.fishing.fishedItemName
private var hasRodInHand = false
private var cache =
CacheBuilder.newBuilder().expireAfterWrite(750, TimeUnit.MILLISECONDS)
.build<EntityItem, Pair<LorenzVec, String>>()
private var cache = TimeLimitedCache<EntityItem, Pair<LorenzVec, String>>(750.milliseconds)

// Taken from Skytils
private val cheapCoins = setOf(
Expand Down Expand Up @@ -77,7 +75,7 @@ class ShowFishingItemName {
}
}

for ((location, text) in cache.asMap().values) {
for ((location, text) in cache.values()) {
event.drawString(location, text)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import com.google.common.cache.CacheBuilder
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.TimeUnit
import kotlin.random.Random
import kotlin.time.Duration.Companion.minutes

object AdvancedPlayerList {
private val config get() = SkyHanniMod.feature.misc.compactTabList.advancedPlayerList
Expand Down Expand Up @@ -188,11 +188,10 @@ object AdvancedPlayerList {
return "$level $playerName ${suffix.trim()}"
}

private var randomOrderCache =
CacheBuilder.newBuilder().expireAfterWrite(20, TimeUnit.MINUTES).build<String, Int>()
private var randomOrderCache = TimeLimitedCache<String, Int>(20.minutes)

private fun getRandomOrder(name: String): Int {
val saved = randomOrderCache.getIfPresent(name)
val saved = randomOrderCache.getOrNull(name)
if (saved != null) {
return saved
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package at.hannibal2.skyhanni.features.misc.visualwords
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.StringUtils.convertToFormatted
import com.google.common.cache.Cache
import com.google.common.cache.CacheBuilder
import java.util.concurrent.TimeUnit
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import kotlin.time.Duration.Companion.minutes

object ModifyVisualWords {
private val config get() = SkyHanniMod.feature.gui.modifyWords
var textCache: Cache<String, String> = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build()
var textCache = TimeLimitedCache<String, String>(5.minutes)

var modifiedWords = mutableListOf<VisualWord>()

Expand All @@ -23,7 +22,7 @@ object ModifyVisualWords {
modifiedWords = SkyHanniMod.feature.storage.modifiedWords
}

val cachedResult = textCache.getIfPresent(originalText)
val cachedResult = textCache.getOrNull(originalText)
if (cachedResult != null) {
return cachedResult
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ open class VisualWordGui : GuiScreen() {

private fun saveChanges() {
ModifyVisualWords.modifiedWords = modifiedWords
ModifyVisualWords.textCache.invalidateAll()
ModifyVisualWords.textCache.clear()
SkyHanniMod.feature.storage.modifiedWords = modifiedWords
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.RenderUtils.drawString
import at.hannibal2.skyhanni.utils.RenderUtils.exactLocation
import com.google.common.cache.CacheBuilder
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import net.minecraft.entity.item.EntityItem
import net.minecraft.init.Items
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.seconds

class SlayerItemsOnGround {
private val config get() = SkyHanniMod.feature.slayer.itemsOnGround

private var itemsOnGround =
CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.SECONDS)
.build<EntityItem, Pair<LorenzVec, String>>()
private var itemsOnGround = TimeLimitedCache<EntityItem, Pair<LorenzVec, String>>(2.seconds)

@SubscribeEvent
fun onRenderWorld(event: LorenzRenderWorldEvent) {
Expand All @@ -47,7 +45,7 @@ class SlayerItemsOnGround {
itemsOnGround.put(entityItem, location to itemName)
}

for ((location, text) in itemsOnGround.asMap().values) {
for ((location, text) in itemsOnGround.values()) {
event.drawString(location, text)
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/at/hannibal2/skyhanni/test/command/ErrorManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.OSUtils
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import com.google.common.cache.CacheBuilder
import at.hannibal2.skyhanni.utils.TimeLimitedSet
import net.minecraft.client.Minecraft
import java.util.UUID
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.minutes

object ErrorManager {
// random id -> error message
private val errorMessages = mutableMapOf<String, String>()
private val fullErrorMessages = mutableMapOf<String, String>()
private var cache =
CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build<Pair<String, Int>, Unit>()
private var cache = TimeLimitedSet<Pair<String, Int>>(10.minutes)

fun resetCache() {
cache.asMap().clear()
cache.clear()
}

fun skyHanniError(message: String): Nothing {
Expand Down Expand Up @@ -77,10 +76,10 @@ object ErrorManager {

if (!ignoreErrorCache) {
val pair = if (throwable.stackTrace.isNotEmpty()) {
throwable.stackTrace[0].let { it.fileName to it.lineNumber }
throwable.stackTrace[0].let { it.fileName!! to it.lineNumber }
} else message to 0
if (cache.getIfPresent(pair) != null) return
cache.put(pair, Unit)
if (cache.contains(pair)) return
cache.add(pair)
}

val fullStackTrace = throwable.getCustomStackTrace(true).joinToString("\n")
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package at.hannibal2.skyhanni.utils

import com.google.common.cache.CacheBuilder
import java.util.concurrent.TimeUnit
import kotlin.time.Duration

class TimeLimitedCache<K, V>(expireAfterWrite: Duration) {

private val cache = CacheBuilder.newBuilder()
.expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS).build<K, V>()

fun put(key: K, value: V) = cache.put(key, value)

fun getOrNull(key: K): V? = cache.getIfPresent(key)

fun clear() = cache.invalidateAll()

fun values(): MutableCollection<V> = cache.asMap().values

fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null
}
14 changes: 14 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedSet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package at.hannibal2.skyhanni.utils

import kotlin.time.Duration

class TimeLimitedSet<T>(expireAfterWrite: Duration) {

private val cache = TimeLimitedCache<T, Unit>(expireAfterWrite)

fun add(element: T) = cache.put(element, Unit)

fun contains(element: T): Boolean = cache.containsKey(element)

fun clear() = cache.clear()
}

0 comments on commit 680b203

Please sign in to comment.