Skip to content

Commit

Permalink
fix(cache): in the event of a negative cache, reset it
Browse files Browse the repository at this point in the history
  • Loading branch information
seailz committed Nov 11, 2024
1 parent 5ae82a8 commit 5bdbb16
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/main/java/com/seailz/discordjar/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
public class Cache<T> {

private final List<T> cache = new ArrayList<>();
private List<T> cache = new ArrayList<>();
private final DiscordJar discordJar;
private final Class<T> clazz;
private final DiscordRequest discordRequest;
Expand Down Expand Up @@ -83,14 +83,19 @@ public void cache(@NotNull T t) {
synchronized (cache) {

Check warning on line 83 in src/main/java/com/seailz/discordjar/cache/Cache.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Synchronization on a non-final field

Synchronization on a non-final field `cache`
try {
if (cache.size() == -1) {

Check warning on line 85 in src/main/java/com/seailz/discordjar/cache/Cache.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Constant values

Condition `cache.size() == -1` is always `false`
Logger.getLogger("DiscordJar").warning("[discord.jar] Failed to add obj to cache - cache size is -1");
if (discordJar.isDebug()) Logger.getLogger("DiscordJar").warning("[discord.jar] Failed to add obj to cache - cache size is -1");
cache = new ArrayList<>();
return;
}; // Also this seems impossible, recent exceptions prove otherwise, so I'm leaving it in. Please ignore your IDE.
}; // Also this seems impossible, recent exceptions prove otherwise (possibly related to this? https://bugs.openjdk.org/browse/JDK-7007540), so I'm leaving it in. Please ignore your IDE.
cache.add(0, t);
} catch (Exception e) {
// We can ignore this - since the cache isn't critical.
// We'll print the stacktrace for debugging purposes.
Logger.getLogger("DiscordJar").warning("[discord.jar] Failed to add obj to cache - " + e.getMessage());
if (discordJar.isDebug()) Logger.getLogger("DiscordJar").warning("[discord.jar] Failed to add obj to cache - " + e.getMessage());
if (discordJar.isDebug()) e.printStackTrace();

// To avoid any further issues, we'll set the cache to a new ArrayList.
cache = new ArrayList<>();
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/seailz/discordjar/ws/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class WebSocket extends WebSocketListener {
private static byte[] buffer = {};
private static final Inflater inflator = new Inflater();
private boolean open = false;
private static List<WebSocket> webSockets = new ArrayList<>();
private static final List<WebSocket> webSockets = new ArrayList<>();

public WebSocket(String url, boolean debug) {
this.url = url;
Expand Down

0 comments on commit 5bdbb16

Please sign in to comment.