From 3a29bf4ddc85425860851bc3365b1659afd00b71 Mon Sep 17 00:00:00 2001 From: granny Date: Sun, 28 Jan 2024 19:07:46 -0800 Subject: [PATCH] only send SSE events if the data has been modified --- .../core/renderer/task/UpdateMarkerData.java | 26 ++++++++++++------- .../renderer/task/UpdateSettingsData.java | 16 ++++++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateMarkerData.java b/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateMarkerData.java index 0cd609e46..06fd84a6c 100644 --- a/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateMarkerData.java +++ b/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateMarkerData.java @@ -23,6 +23,8 @@ */ package net.pl3x.map.core.renderer.task; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; @@ -35,6 +37,7 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; import net.pl3x.map.core.Pl3xMap; import net.pl3x.map.core.markers.JsonObjectWrapper; import net.pl3x.map.core.markers.layer.Layer; @@ -56,7 +59,10 @@ public class UpdateMarkerData extends Task { private final World world; private final Map<@NotNull String, @NotNull Long> lastUpdated = new HashMap<>(); - private final Map<@NotNull String, @NotNull Long> fileLastUpdated = new HashMap<>(); + private final Cache<@NotNull String, String> markerCache = CacheBuilder.newBuilder() + .maximumSize(1000) + .expireAfterWrite(1, TimeUnit.MINUTES) + .build(); private final ExecutorService executor; private CompletableFuture future; @@ -103,17 +109,19 @@ private void parseLayers() { layers.add(layer.toJson()); long now = System.currentTimeMillis(); - long lastUpdate = this.lastUpdated.getOrDefault(key, 0L); - long fileLastUpdated = this.fileLastUpdated.getOrDefault(key, 0L); + long lastUpdated = this.lastUpdated.getOrDefault(key, 0L); List> list = new ArrayList<>(layer.getMarkers()); - if (now - lastUpdate > Math.max(TickUtil.toMilliseconds(layer.getUpdateInterval()), 50)) { - Pl3xMap.api().getHttpdServer().sendSSE("markers", String.format("{ \"world\": \"%s\", \"key\": \"%s\", \"markers\": %s}", this.world.getName(), key, this.gson.toJson(list))); - this.lastUpdated.put(key, now); + String json = this.gson.toJson(list); + String markerCacheIfPresent = markerCache.getIfPresent(key); + if (markerCacheIfPresent == null || !markerCacheIfPresent.equals(json)) { + Pl3xMap.api().getHttpdServer().sendSSE("markers", String.format("{ \"world\": \"%s\", \"key\": \"%s\", \"markers\": %s}", this.world.getName(), key, json)); + markerCache.put(key, json); } - if (now - fileLastUpdated > Math.max(TickUtil.toMilliseconds(layer.getUpdateInterval()), 1000)) { - FileUtil.writeJson(this.gson.toJson(list), this.world.getMarkersDirectory().resolve(key.replace(":", "-") + ".json")); - this.fileLastUpdated.put(key, now); + + if (now - lastUpdated > Math.max(TickUtil.toMilliseconds(layer.getUpdateInterval()), 1000)) { + FileUtil.writeJson(json, this.world.getMarkersDirectory().resolve(key.replace(":", "-") + ".json")); + this.lastUpdated.put(key, now); } } catch (Throwable t) { t.printStackTrace(); diff --git a/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateSettingsData.java b/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateSettingsData.java index d69fc84c2..92ae42fc6 100644 --- a/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateSettingsData.java +++ b/core/src/main/java/net/pl3x/map/core/renderer/task/UpdateSettingsData.java @@ -42,13 +42,14 @@ import org.jetbrains.annotations.NotNull; public class UpdateSettingsData extends Task { - private int tempTick; + private int fileTick; private final Gson gson = new GsonBuilder() //.setPrettyPrinting() .disableHtmlEscaping() .serializeNulls() .setLenient() .create(); + private String cachedJsonData = ""; public UpdateSettingsData() { super(1, true); @@ -150,11 +151,16 @@ private void parseSettings() { t.printStackTrace(); } - Pl3xMap.api().getHttpdServer().sendSSE("settings", this.gson.toJson(map)); + String json = this.gson.toJson(map); - if (tempTick++ >= 20) { - tempTick = 0; - FileUtil.writeJson(this.gson.toJson(map), FileUtil.getTilesDir().resolve("settings.json")); + if (!cachedJsonData.equals(json)) { + Pl3xMap.api().getHttpdServer().sendSSE("settings", json); + cachedJsonData = json; + } + + if (fileTick++ >= 20) { + fileTick = 0; + FileUtil.writeJson(json, FileUtil.getTilesDir().resolve("settings.json")); } } }