Skip to content

Commit

Permalink
Don't cache Messages, Emojis and Stickers
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdennnoch committed Mar 19, 2024
1 parent a9998ed commit 30fb34f
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 50 deletions.
19 changes: 2 additions & 17 deletions javacord-core/src/main/java/org/javacord/core/DiscordApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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<Message> result = new WeakReference<>(message, messagesCleanupQueue);
messageIdByRef.put(result, key);
return result;
}
return value;
});
} finally {
messageCacheLock.unlock();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -773,7 +759,6 @@ public void removeRole(long roleId) {
* @param emoji The emoji to add.
*/
public void addCustomEmoji(KnownCustomEmoji emoji) {
customEmojis.add(emoji);
}

/**
Expand Down Expand Up @@ -2109,7 +2094,6 @@ public CompletableFuture<Sticker> requestStickerById(long id) {
* @param sticker The sticker to add to the server's cache.
*/
public void addSticker(Sticker sticker) {
stickers.put(sticker.getId(), sticker);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> 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();
}
}

/**
Expand Down

0 comments on commit 30fb34f

Please sign in to comment.