Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,23 @@ public MessageCache(BotConfig botConfig, MessageCacheRepository cacheRepository,
}

/**
* Synchronizes Messages saved in the Database with what is currently stored in memory.
* Synchronizes Messages saved in the Database with what is currently stored in memory. This action is executed in the background.
*/
public void synchronize() {
asyncPool.execute(()->{
cacheRepository.delete(cache.size());
cacheRepository.insertList(new ArrayList<>(cache));
messageCount = 0;
log.info("Synchronized Database with local Cache.");
synchronizeNow();
});
}

/**
* Synchronizes Messages saved in the Database with what is currently stored in memory and wait until the synchronization finishes.
*/
public void synchronizeNow() {
cacheRepository.delete(cache.size());
cacheRepository.insertList(new ArrayList<>(cache));
messageCount = 0;
log.info("Synchronized Database with local Cache.");
}

/**
* Caches a single {@link Message} object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ public int getBatchSize() {
public List<CachedMessage> getAll() throws DataAccessException {
List<CachedMessage> messagesWithLink = jdbcTemplate.query(
"SELECT * FROM message_cache LEFT JOIN message_cache_attachments ON message_cache.message_id = message_cache_attachments.message_id",
(rs, rowNum) -> this.read(rs));
(rs, _) -> this.read(rs));
Map<Long, CachedMessage> messages=new LinkedHashMap<>();
for (CachedMessage msg : messagesWithLink) {
CachedMessage previous = messages.putIfAbsent(msg.getMessageId(), msg);
if(previous!=null) {
previous.getAttachments().addAll(msg.getAttachments());
}
messages.merge(msg.getMessageId(), msg, (oldValue, value) -> {
ArrayList<String> attachments = new ArrayList<>(oldValue.getAttachments());
attachments.addAll(value.getAttachments());
return new CachedMessage(oldValue.getMessageId(), oldValue.getAuthorId(), oldValue.getMessageContent(), attachments);
});
}
return new ArrayList<>(messages.values());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
}
log.warn("Redeploying... Requested by: " + UserUtils.getUserTag(event.getUser()));
event.reply("**Redeploying...** This may take some time.").queue();
messageCache.synchronize();
messageCache.synchronizeNow();
asyncPool.shutdownNow();
try {
asyncPool.awaitTermination(3, TimeUnit.SECONDS);
Expand Down