From 413800515f2167578205c93046cf8119ce54b6b1 Mon Sep 17 00:00:00 2001 From: Jonttuuu <50843099+Jonttuuu@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:36:33 +0300 Subject: [PATCH] Add cache cleanup --- src/main/java/fi/flexplex/lib/FlexLib.java | 23 +++++++++++++-- .../java/fi/flexplex/lib/PlayerFriends.java | 12 ++++++++ .../java/fi/flexplex/lib/WebSocketClient.java | 28 ++++++------------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/main/java/fi/flexplex/lib/FlexLib.java b/src/main/java/fi/flexplex/lib/FlexLib.java index 72943a5..2262d00 100644 --- a/src/main/java/fi/flexplex/lib/FlexLib.java +++ b/src/main/java/fi/flexplex/lib/FlexLib.java @@ -5,12 +5,16 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Optional; import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; import java.util.logging.Logger; public final class FlexLib { private final String token; private final Logger logger; + private final Timer timer; + private final TimerTask timerTask; private final GraphQLApi graphQLApi; private final Set eventListeners = new HashSet<>(); private WebSocketClient webSocketClient; @@ -32,11 +36,11 @@ public final class FlexLib { * @param token token for FlexPlex API */ public FlexLib(final String token, final Logger logger) { + logger.info("Initializing FlexLib"); this.token = token; this.logger = logger; - this.logger.info("Initializing FlexLib"); - this.graphQLApi = new GraphQLApi(this, "https://api.flexplex.fi/graphql"); + new FlexLibAdapterImpl(this); try { @@ -44,6 +48,17 @@ public final class FlexLib { } catch (final IOException e) { this.logger.warning("Failed to initialize FlexLib WebSocket client"); } + + this.timer = new Timer(); + this.timerTask = new TimerTask() { + @Override + public void run() { + final long removeTime = System.currentTimeMillis() - 10_800_000; + playerFriends.entrySet().removeIf(entry -> entry.getValue().getLastUsed() < removeTime); + webSocketClient.connect(); + } + }; + this.timer.schedule(this.timerTask, 0, 60_000); } /** @@ -91,9 +106,13 @@ public final class FlexLib { * Disableds FlexLib API instance */ public void disable() { + this.timerTask.cancel(); + this.timer.cancel(); + if (this.webSocketClient != null) { this.webSocketClient.disable(); } + this.eventListeners.clear(); this.playerFriends.clear(); } diff --git a/src/main/java/fi/flexplex/lib/PlayerFriends.java b/src/main/java/fi/flexplex/lib/PlayerFriends.java index 019b329..ef8945f 100644 --- a/src/main/java/fi/flexplex/lib/PlayerFriends.java +++ b/src/main/java/fi/flexplex/lib/PlayerFriends.java @@ -17,6 +17,7 @@ public final class PlayerFriends { private final Set friends = new HashSet<>(); private final Set incomingFriendRequests = new HashSet<>(); private final Set outcomingFriendRequests = new HashSet<>(); + private long lastUsed = System.currentTimeMillis(); protected PlayerFriends(final GraphQLApi graphQLApi, final String token, final String player) throws IOException { this.graphQLApi = graphQLApi; @@ -27,6 +28,10 @@ public final class PlayerFriends { } } + protected long getLastUsed() { + return this.lastUsed; + } + protected boolean update() { final Optional result = this.graphQLApi.execute( "query{" + @@ -66,6 +71,7 @@ public final class PlayerFriends { * @return friends */ public Set getFriends() { + this.lastUsed = System.currentTimeMillis(); return this.friends; } @@ -74,6 +80,7 @@ public final class PlayerFriends { * @return friend requests */ public Set getIncomingFriendRequests() { + this.lastUsed = System.currentTimeMillis(); return this.incomingFriendRequests; } @@ -82,6 +89,7 @@ public final class PlayerFriends { * @return friend requests */ public Set getOutcomingFriendRequests() { + this.lastUsed = System.currentTimeMillis(); return this.outcomingFriendRequests; } @@ -92,6 +100,7 @@ public final class PlayerFriends { * @return true if was created successfully */ public boolean createFriendRequest(final String player, final String targetPlayer) { + this.lastUsed = System.currentTimeMillis(); final Optional result = this.graphQLApi.execute( "mutation{" + "createFriendRequest(" + @@ -111,6 +120,7 @@ public final class PlayerFriends { * @return true if was accepted successfully */ public boolean acceptFriendRequest(final String player, final String targetPlayer) { + this.lastUsed = System.currentTimeMillis(); final Optional result = this.graphQLApi.execute( "mutation{" + "acceptFriendRequest(" + @@ -130,6 +140,7 @@ public final class PlayerFriends { * @return true if was deleted successfully */ public boolean deleteFriend(final String player, final String targetPlayer) { + this.lastUsed = System.currentTimeMillis(); final Optional result = this.graphQLApi.execute( "mutation{" + "deleteFriend(" + @@ -149,6 +160,7 @@ public final class PlayerFriends { * @return true if was changed successfully */ public boolean setFriendRequestsEnabled(final String player, final boolean enabled) { + this.lastUsed = System.currentTimeMillis(); final Optional result = this.graphQLApi.execute( "mutation{" + "updateAcceptFriendRequests(" + diff --git a/src/main/java/fi/flexplex/lib/WebSocketClient.java b/src/main/java/fi/flexplex/lib/WebSocketClient.java index 5d5a6e5..901049d 100644 --- a/src/main/java/fi/flexplex/lib/WebSocketClient.java +++ b/src/main/java/fi/flexplex/lib/WebSocketClient.java @@ -1,8 +1,6 @@ package fi.flexplex.lib; import java.io.IOException; -import java.util.Timer; -import java.util.TimerTask; import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketAdapter; @@ -12,32 +10,24 @@ import com.neovisionaries.ws.client.WebSocketFactory; public final class WebSocketClient { private final FlexLib flexLib; - private final Timer timer; - private final TimerTask timerTask; private final WebSocket webSocket; protected WebSocketClient(final FlexLib flexLib, final String url, final String token) throws IOException { this.flexLib = flexLib; this.webSocket = this.createWebSocket(url, token); - this.timer = new Timer(); - this.timerTask = new TimerTask() { - @Override - public void run() { - if (!webSocket.isOpen()) { - try { - webSocket.connect(); - } catch (final WebSocketException e) { - flexLib.getLogger().warning("WebSocket connection to FlexPlex server failed."); - } - } + } + + protected void connect() { + if (!webSocket.isOpen()) { + try { + this.webSocket.connect(); + } catch (final WebSocketException e) { + flexLib.getLogger().warning("WebSocket connection to FlexPlex server failed."); } - }; - this.timer.schedule(this.timerTask, 0, 60_000); + } } protected void disable() { - this.timerTask.cancel(); - this.timer.cancel(); this.webSocket.disconnect(); }