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.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<FlexLibAdapter> 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();
}

View File

@ -17,6 +17,7 @@ public final class PlayerFriends {
private final Set<UUID> friends = new HashSet<>();
private final Set<UUID> incomingFriendRequests = 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 {
this.graphQLApi = graphQLApi;
@ -27,6 +28,10 @@ public final class PlayerFriends {
}
}
protected long getLastUsed() {
return this.lastUsed;
}
protected boolean update() {
final Optional<JsonObject> result = this.graphQLApi.execute(
"query{" +
@ -66,6 +71,7 @@ public final class PlayerFriends {
* @return friends
*/
public Set<UUID> getFriends() {
this.lastUsed = System.currentTimeMillis();
return this.friends;
}
@ -74,6 +80,7 @@ public final class PlayerFriends {
* @return friend requests
*/
public Set<UUID> getIncomingFriendRequests() {
this.lastUsed = System.currentTimeMillis();
return this.incomingFriendRequests;
}
@ -82,6 +89,7 @@ public final class PlayerFriends {
* @return friend requests
*/
public Set<UUID> 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<JsonObject> 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<JsonObject> 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<JsonObject> 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<JsonObject> result = this.graphQLApi.execute(
"mutation{" +
"updateAcceptFriendRequests(" +

View File

@ -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() {
}
protected void connect() {
if (!webSocket.isOpen()) {
try {
webSocket.connect();
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();
}