From 30fb34fdffa49396941f5375c4b7e274fee229b8 Mon Sep 17 00:00:00 2001 From: Adrian Paschkowski Date: Sat, 18 Nov 2023 21:33:20 +0100 Subject: [PATCH] Don't cache Messages, Emojis and Stickers --- .../org/javacord/core/DiscordApiImpl.java | 19 ++----------------- .../core/entity/server/ServerImpl.java | 16 ---------------- .../core/util/cache/MessageCacheImpl.java | 17 ----------------- 3 files changed, 2 insertions(+), 50 deletions(-) diff --git a/javacord-core/src/main/java/org/javacord/core/DiscordApiImpl.java b/javacord-core/src/main/java/org/javacord/core/DiscordApiImpl.java index e3a2eb768f..1b166c6bc5 100644 --- a/javacord-core/src/main/java/org/javacord/core/DiscordApiImpl.java +++ b/javacord-core/src/main/java/org/javacord/core/DiscordApiImpl.java @@ -1142,8 +1142,7 @@ public void setTimeOffset(Long timeOffset) { * @return The emoji for the given json object. */ public KnownCustomEmoji getOrCreateKnownCustomEmoji(Server server, JsonNode data) { - long id = Long.parseLong(data.get("id").asText()); - return customEmojis.computeIfAbsent(id, key -> new KnownCustomEmojiImpl(this, server, data)); + return new KnownCustomEmojiImpl(this, server, data); } /** @@ -1188,8 +1187,7 @@ public void removeCustomEmoji(KnownCustomEmoji emoji) { * @return The created sticker. */ public Sticker getOrCreateSticker(JsonNode data) { - long id = data.get("id").asLong(); - return stickers.computeIfAbsent(id, key -> new StickerImpl(this, data)); + return new StickerImpl(this, data); } /** @@ -1236,19 +1234,6 @@ public Message getOrCreateMessage(TextChannel channel, JsonNode data) { * @param message The message to add. */ public void addMessageToCache(Message message) { - messageCacheLock.lock(); - try { - messages.compute(message.getId(), (key, value) -> { - if ((value == null) || (value.get() == null)) { - WeakReference result = new WeakReference<>(message, messagesCleanupQueue); - messageIdByRef.put(result, key); - return result; - } - return value; - }); - } finally { - messageCacheLock.unlock(); - } } /** diff --git a/javacord-core/src/main/java/org/javacord/core/entity/server/ServerImpl.java b/javacord-core/src/main/java/org/javacord/core/entity/server/ServerImpl.java index 6586979c54..ed184aeca8 100644 --- a/javacord-core/src/main/java/org/javacord/core/entity/server/ServerImpl.java +++ b/javacord-core/src/main/java/org/javacord/core/entity/server/ServerImpl.java @@ -501,20 +501,6 @@ && getMembers().size() < getMemberCount() api.getWebSocketAdapter().queueRequestGuildMembers(this); } - if (data.has("emojis")) { - for (JsonNode emojiJson : data.get("emojis")) { - KnownCustomEmoji emoji = api.getOrCreateKnownCustomEmoji(this, emojiJson); - addCustomEmoji(emoji); - } - } - - if (data.has("stickers")) { - for (JsonNode stickerJson : data.get("stickers")) { - Sticker sticker = api.getOrCreateSticker(stickerJson); - addSticker(sticker); - } - } - if (data.has("presences")) { for (JsonNode presenceJson : data.get("presences")) { long userId = Long.parseLong(presenceJson.get("user").get("id").asText()); @@ -773,7 +759,6 @@ public void removeRole(long roleId) { * @param emoji The emoji to add. */ public void addCustomEmoji(KnownCustomEmoji emoji) { - customEmojis.add(emoji); } /** @@ -2109,7 +2094,6 @@ public CompletableFuture requestStickerById(long id) { * @param sticker The sticker to add to the server's cache. */ public void addSticker(Sticker sticker) { - stickers.put(sticker.getId(), sticker); } /** diff --git a/javacord-core/src/main/java/org/javacord/core/util/cache/MessageCacheImpl.java b/javacord-core/src/main/java/org/javacord/core/util/cache/MessageCacheImpl.java index 268cc6d1c6..d7644cb02a 100644 --- a/javacord-core/src/main/java/org/javacord/core/util/cache/MessageCacheImpl.java +++ b/javacord-core/src/main/java/org/javacord/core/util/cache/MessageCacheImpl.java @@ -120,23 +120,6 @@ public MessageCacheImpl(DiscordApiImpl api, int capacity, int storageTimeInSecon * @param message The message to add. */ public void addMessage(Message message) { - api.getMessageCacheLock().lock(); - try { - api.addMessageToCache(message); - if (messages.stream().map(Reference::get).anyMatch(message::equals)) { - return; - } - // Add the message in the correct order - messages.removeIf(messageRef -> messageRef.get() == null); - Reference messageRef = new SoftReference<>(message, messagesCleanupQueue); - int pos = Collections.binarySearch(messages, messageRef, Comparator.comparing(Reference::get)); - if (pos < 0) { - pos = -pos - 1; - } - messages.add(pos, messageRef); - } finally { - api.getMessageCacheLock().unlock(); - } } /**