Add cache cleanup

This commit is contained in:
Jonttuuu 2023-07-30 13:36:33 +03:00
parent 340fed098f
commit 413800515f
3 changed files with 42 additions and 21 deletions

View File

@ -5,12 +5,16 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger; import java.util.logging.Logger;
public final class FlexLib { public final class FlexLib {
private final String token; private final String token;
private final Logger logger; private final Logger logger;
private final Timer timer;
private final TimerTask timerTask;
private final GraphQLApi graphQLApi; private final GraphQLApi graphQLApi;
private final Set<FlexLibAdapter> eventListeners = new HashSet<>(); private final Set<FlexLibAdapter> eventListeners = new HashSet<>();
private WebSocketClient webSocketClient; private WebSocketClient webSocketClient;
@ -32,11 +36,11 @@ public final class FlexLib {
* @param token token for FlexPlex API * @param token token for FlexPlex API
*/ */
public FlexLib(final String token, final Logger logger) { public FlexLib(final String token, final Logger logger) {
logger.info("Initializing FlexLib");
this.token = token; this.token = token;
this.logger = logger; this.logger = logger;
this.logger.info("Initializing FlexLib");
this.graphQLApi = new GraphQLApi(this, "https://api.flexplex.fi/graphql"); this.graphQLApi = new GraphQLApi(this, "https://api.flexplex.fi/graphql");
new FlexLibAdapterImpl(this); new FlexLibAdapterImpl(this);
try { try {
@ -44,6 +48,17 @@ public final class FlexLib {
} catch (final IOException e) { } catch (final IOException e) {
this.logger.warning("Failed to initialize FlexLib WebSocket client"); 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 * Disableds FlexLib API instance
*/ */
public void disable() { public void disable() {
this.timerTask.cancel();
this.timer.cancel();
if (this.webSocketClient != null) { if (this.webSocketClient != null) {
this.webSocketClient.disable(); this.webSocketClient.disable();
} }
this.eventListeners.clear(); this.eventListeners.clear();
this.playerFriends.clear(); this.playerFriends.clear();
} }

View File

@ -17,6 +17,7 @@ public final class PlayerFriends {
private final Set<UUID> friends = new HashSet<>(); private final Set<UUID> friends = new HashSet<>();
private final Set<UUID> incomingFriendRequests = new HashSet<>(); private final Set<UUID> incomingFriendRequests = new HashSet<>();
private final Set<UUID> outcomingFriendRequests = new HashSet<>(); private final Set<UUID> outcomingFriendRequests = new HashSet<>();
private long lastUsed = System.currentTimeMillis();
protected PlayerFriends(final GraphQLApi graphQLApi, final String token, final String player) throws IOException { protected PlayerFriends(final GraphQLApi graphQLApi, final String token, final String player) throws IOException {
this.graphQLApi = graphQLApi; this.graphQLApi = graphQLApi;
@ -27,6 +28,10 @@ public final class PlayerFriends {
} }
} }
protected long getLastUsed() {
return this.lastUsed;
}
protected boolean update() { protected boolean update() {
final Optional<JsonObject> result = this.graphQLApi.execute( final Optional<JsonObject> result = this.graphQLApi.execute(
"query{" + "query{" +
@ -66,6 +71,7 @@ public final class PlayerFriends {
* @return friends * @return friends
*/ */
public Set<UUID> getFriends() { public Set<UUID> getFriends() {
this.lastUsed = System.currentTimeMillis();
return this.friends; return this.friends;
} }
@ -74,6 +80,7 @@ public final class PlayerFriends {
* @return friend requests * @return friend requests
*/ */
public Set<UUID> getIncomingFriendRequests() { public Set<UUID> getIncomingFriendRequests() {
this.lastUsed = System.currentTimeMillis();
return this.incomingFriendRequests; return this.incomingFriendRequests;
} }
@ -82,6 +89,7 @@ public final class PlayerFriends {
* @return friend requests * @return friend requests
*/ */
public Set<UUID> getOutcomingFriendRequests() { public Set<UUID> getOutcomingFriendRequests() {
this.lastUsed = System.currentTimeMillis();
return this.outcomingFriendRequests; return this.outcomingFriendRequests;
} }
@ -92,6 +100,7 @@ public final class PlayerFriends {
* @return true if was created successfully * @return true if was created successfully
*/ */
public boolean createFriendRequest(final String player, final String targetPlayer) { public boolean createFriendRequest(final String player, final String targetPlayer) {
this.lastUsed = System.currentTimeMillis();
final Optional<JsonObject> result = this.graphQLApi.execute( final Optional<JsonObject> result = this.graphQLApi.execute(
"mutation{" + "mutation{" +
"createFriendRequest(" + "createFriendRequest(" +
@ -111,6 +120,7 @@ public final class PlayerFriends {
* @return true if was accepted successfully * @return true if was accepted successfully
*/ */
public boolean acceptFriendRequest(final String player, final String targetPlayer) { public boolean acceptFriendRequest(final String player, final String targetPlayer) {
this.lastUsed = System.currentTimeMillis();
final Optional<JsonObject> result = this.graphQLApi.execute( final Optional<JsonObject> result = this.graphQLApi.execute(
"mutation{" + "mutation{" +
"acceptFriendRequest(" + "acceptFriendRequest(" +
@ -130,6 +140,7 @@ public final class PlayerFriends {
* @return true if was deleted successfully * @return true if was deleted successfully
*/ */
public boolean deleteFriend(final String player, final String targetPlayer) { public boolean deleteFriend(final String player, final String targetPlayer) {
this.lastUsed = System.currentTimeMillis();
final Optional<JsonObject> result = this.graphQLApi.execute( final Optional<JsonObject> result = this.graphQLApi.execute(
"mutation{" + "mutation{" +
"deleteFriend(" + "deleteFriend(" +
@ -149,6 +160,7 @@ public final class PlayerFriends {
* @return true if was changed successfully * @return true if was changed successfully
*/ */
public boolean setFriendRequestsEnabled(final String player, final boolean enabled) { public boolean setFriendRequestsEnabled(final String player, final boolean enabled) {
this.lastUsed = System.currentTimeMillis();
final Optional<JsonObject> result = this.graphQLApi.execute( final Optional<JsonObject> result = this.graphQLApi.execute(
"mutation{" + "mutation{" +
"updateAcceptFriendRequests(" + "updateAcceptFriendRequests(" +

View File

@ -1,8 +1,6 @@
package fi.flexplex.lib; package fi.flexplex.lib;
import java.io.IOException; import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocketAdapter;
@ -12,32 +10,24 @@ import com.neovisionaries.ws.client.WebSocketFactory;
public final class WebSocketClient { public final class WebSocketClient {
private final FlexLib flexLib; private final FlexLib flexLib;
private final Timer timer;
private final TimerTask timerTask;
private final WebSocket webSocket; private final WebSocket webSocket;
protected WebSocketClient(final FlexLib flexLib, final String url, final String token) throws IOException { protected WebSocketClient(final FlexLib flexLib, final String url, final String token) throws IOException {
this.flexLib = flexLib; this.flexLib = flexLib;
this.webSocket = this.createWebSocket(url, token); this.webSocket = this.createWebSocket(url, token);
this.timer = new Timer(); }
this.timerTask = new TimerTask() {
@Override protected void connect() {
public void run() { if (!webSocket.isOpen()) {
if (!webSocket.isOpen()) { try {
try { this.webSocket.connect();
webSocket.connect(); } catch (final WebSocketException e) {
} catch (final WebSocketException e) { flexLib.getLogger().warning("WebSocket connection to FlexPlex server failed.");
flexLib.getLogger().warning("WebSocket connection to FlexPlex server failed.");
}
}
} }
}; }
this.timer.schedule(this.timerTask, 0, 60_000);
} }
protected void disable() { protected void disable() {
this.timerTask.cancel();
this.timer.cancel();
this.webSocket.disconnect(); this.webSocket.disconnect();
} }