From fcbfcf21d60991d4d9ca1d70690d1a6076cba910 Mon Sep 17 00:00:00 2001 From: Bram Date: Wed, 24 Apr 2024 00:14:20 +0200 Subject: [PATCH 1/2] Testing --- build.gradle.kts | 4 +++- common/build.gradle.kts | 1 + .../antihealthindicator/managers/CacheManager.java | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index aa8dfa5..4d2f866 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,8 +37,10 @@ tasks { minimize() archiveFileName.set("${project.name}-${project.version}.jar") - subprojects.forEach { subproject -> + project.subprojects.forEach { subproject -> from(project(subproject.path).sourceSets.main.get().output) } + + relocate("com.github.benmanes.caffeine", "com.deathmotion.antihealthindicator.shaded.caffeine") } } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 1e2c9e7..7b0d1cf 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -4,6 +4,7 @@ dependencies { compileOnlyApi("com.github.retrooper.packetevents:api:2.2.1") compileOnlyApi("org.projectlombok:lombok:1.18.30") annotationProcessor("org.projectlombok:lombok:1.18.30") + implementation("com.github.ben-manes.caffeine:caffeine:2.5.6") compileOnlyApi("net.kyori:adventure-api:$adventureVersion") compileOnlyApi("net.kyori:adventure-text-serializer-gson:$adventureVersion") diff --git a/common/src/main/java/com/deathmotion/antihealthindicator/managers/CacheManager.java b/common/src/main/java/com/deathmotion/antihealthindicator/managers/CacheManager.java index ffbbe8f..5f4846b 100644 --- a/common/src/main/java/com/deathmotion/antihealthindicator/managers/CacheManager.java +++ b/common/src/main/java/com/deathmotion/antihealthindicator/managers/CacheManager.java @@ -22,6 +22,9 @@ import com.deathmotion.antihealthindicator.data.cache.LivingEntityData; import com.deathmotion.antihealthindicator.data.cache.RidableEntityData; import com.deathmotion.antihealthindicator.packetlisteners.EntityState; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.RemovalCause; import lombok.Getter; import java.util.Map; @@ -34,11 +37,18 @@ public class CacheManager

{ private final AHIPlatform

platform; private final ConcurrentHashMap livingEntityDataCache; + private final Cache test; public CacheManager(AHIPlatform

platform) { this.platform = platform; livingEntityDataCache = new ConcurrentHashMap<>(); + test = Caffeine.newBuilder() + .expireAfterAccess(5, TimeUnit.MINUTES) + .removalListener((Integer entityId, LivingEntityData livingEntityData, RemovalCause cause) -> + System.out.println("Entity ID: " + entityId + " has been removed.")) + .build(); + this.CleanCache(); } From 64f7746ec2be6814b311596979808e6a587d46f1 Mon Sep 17 00:00:00 2001 From: Bram Date: Wed, 24 Apr 2024 10:36:22 +0200 Subject: [PATCH 2/2] wip --- build.gradle.kts | 7 ++- common/build.gradle.kts | 2 +- .../managers/CacheManager.java | 54 ++++++++----------- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4d2f866..ffa5776 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,6 +24,10 @@ allprojects { } } +dependencies { + api("com.github.ben-manes.caffeine:caffeine:2.5.6") +} + tasks { build { dependsOn(shadowJar) @@ -34,13 +38,12 @@ tasks { } shadowJar { - minimize() archiveFileName.set("${project.name}-${project.version}.jar") - project.subprojects.forEach { subproject -> from(project(subproject.path).sourceSets.main.get().output) } relocate("com.github.benmanes.caffeine", "com.deathmotion.antihealthindicator.shaded.caffeine") + minimize() } } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 7b0d1cf..e7d0fe6 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -4,7 +4,7 @@ dependencies { compileOnlyApi("com.github.retrooper.packetevents:api:2.2.1") compileOnlyApi("org.projectlombok:lombok:1.18.30") annotationProcessor("org.projectlombok:lombok:1.18.30") - implementation("com.github.ben-manes.caffeine:caffeine:2.5.6") + api("com.github.ben-manes.caffeine:caffeine:2.5.6") compileOnlyApi("net.kyori:adventure-api:$adventureVersion") compileOnlyApi("net.kyori:adventure-text-serializer-gson:$adventureVersion") diff --git a/common/src/main/java/com/deathmotion/antihealthindicator/managers/CacheManager.java b/common/src/main/java/com/deathmotion/antihealthindicator/managers/CacheManager.java index 5f4846b..674d0f7 100644 --- a/common/src/main/java/com/deathmotion/antihealthindicator/managers/CacheManager.java +++ b/common/src/main/java/com/deathmotion/antihealthindicator/managers/CacheManager.java @@ -21,39 +21,34 @@ import com.deathmotion.antihealthindicator.AHIPlatform; import com.deathmotion.antihealthindicator.data.cache.LivingEntityData; import com.deathmotion.antihealthindicator.data.cache.RidableEntityData; -import com.deathmotion.antihealthindicator.packetlisteners.EntityState; -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.RemovalCause; +import com.github.benmanes.caffeine.cache.*; import lombok.Getter; import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; @Getter public class CacheManager

{ private final AHIPlatform

platform; - private final ConcurrentHashMap livingEntityDataCache; - private final Cache test; + private final Cache livingEntityDataCache; public CacheManager(AHIPlatform

platform) { this.platform = platform; - livingEntityDataCache = new ConcurrentHashMap<>(); - test = Caffeine.newBuilder() - .expireAfterAccess(5, TimeUnit.MINUTES) - .removalListener((Integer entityId, LivingEntityData livingEntityData, RemovalCause cause) -> - System.out.println("Entity ID: " + entityId + " has been removed.")) + this.livingEntityDataCache = Caffeine.newBuilder() + .expireAfterAccess(10, TimeUnit.SECONDS) + .removalListener((RemovalListener) (key, value, cause) -> { + if (key != null && cause.wasEvicted()) { + this.checkDeletedEntity(key, value); + } + }) .build(); - - this.CleanCache(); } public Optional getLivingEntityData(int entityId) { - return Optional.ofNullable(livingEntityDataCache.get(entityId)); + return Optional.ofNullable(livingEntityDataCache.getIfPresent(entityId)); } public Optional getVehicleData(int entityId) { @@ -62,15 +57,15 @@ public Optional getVehicleData(int entityId) { } public boolean isLivingEntityCached(int entityId) { - return livingEntityDataCache.containsKey(entityId); + return livingEntityDataCache.asMap().containsKey(entityId); } public void addLivingEntity(int entityId, LivingEntityData livingEntityData) { - livingEntityDataCache.putIfAbsent(entityId, livingEntityData); + livingEntityDataCache.put(entityId, livingEntityData); } public void removeLivingEntity(int entityId) { - livingEntityDataCache.remove(entityId); + livingEntityDataCache.invalidate(entityId); } public void updateVehiclePassenger(int entityId, int passengerId) { @@ -90,25 +85,20 @@ public int getPassengerId(int entityId) { } public int getEntityIdByPassengerId(int passengerId) { - return livingEntityDataCache.entrySet().stream() + return livingEntityDataCache.asMap().entrySet().stream() .filter(entry -> entry.getValue() instanceof RidableEntityData && ((RidableEntityData) entry.getValue()).getPassengerId() == passengerId) .mapToInt(Map.Entry::getKey) .findFirst() .orElse(0); } - /** - * Technically the {@link EntityState} handler should remove entities from the cache - * when an entity is being removed, - * but this is a safety measure to ensure that the cache won't be creating memory leaks. - */ - private void CleanCache() { - this.platform.getScheduler().runAsyncTaskAtFixedRate((o) -> { - livingEntityDataCache.keySet().forEach(key -> { - if (this.platform.isEntityRemoved(key, null)) { - livingEntityDataCache.remove(key); - } - }); - }, 1, 1, TimeUnit.MINUTES); + private void checkDeletedEntity(int entityId, LivingEntityData livingEntityData) { + if (platform.isEntityRemoved(entityId, null)) { + this.platform.getLoggerWrapper().info("Entity with id " + entityId + " was removed from the world, removing from cache"); + } + else { + this.platform.getLoggerWrapper().info("Entity with id " + entityId + " was not removed from the world, re-adding to cache"); + this.addLivingEntity(entityId, livingEntityData); + } } } \ No newline at end of file