forked from Fascinated/Bat
add guild, user and scoresaber account caching
This commit is contained in:
pom.xml
src/main/java/cc/fascinated/bat
@ -2,25 +2,29 @@ package cc.fascinated.bat.service;
|
||||
|
||||
import cc.fascinated.bat.model.BatGuild;
|
||||
import cc.fascinated.bat.repository.GuildRepository;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Service @Log4j2
|
||||
@Service @Log4j2 @Getter
|
||||
@DependsOn("discordService")
|
||||
public class GuildService extends ListenerAdapter {
|
||||
/**
|
||||
* The cached guilds
|
||||
*/
|
||||
private final Map<String, BatGuild> guilds = new HashMap<>();
|
||||
|
||||
/**
|
||||
* The guild repository to use
|
||||
*/
|
||||
@ -38,12 +42,17 @@ public class GuildService extends ListenerAdapter {
|
||||
* @param id The ID of the guild
|
||||
* @return The guild
|
||||
*/
|
||||
@Cacheable(cacheNames = "guilds", key = "#id")
|
||||
public BatGuild getGuild(@NonNull String id) {
|
||||
if (guilds.containsKey(id)) {
|
||||
return guilds.get(id);
|
||||
}
|
||||
long start = System.currentTimeMillis();
|
||||
Optional<BatGuild> optionalGuild = guildRepository.findById(id);
|
||||
|
||||
if (optionalGuild.isPresent()) {
|
||||
return optionalGuild.get();
|
||||
BatGuild guild = optionalGuild.get();
|
||||
guilds.put(id, guild);
|
||||
return guild;
|
||||
}
|
||||
BatGuild guild = guildRepository.save(new BatGuild(id));
|
||||
log.info("Created guild \"{}\" in {}ms", id, System.currentTimeMillis() - start);
|
||||
@ -55,7 +64,6 @@ public class GuildService extends ListenerAdapter {
|
||||
*
|
||||
* @param guild The guild to save
|
||||
*/
|
||||
@CachePut(cacheNames = "guilds", key = "#guild.id")
|
||||
public void saveGuild(@NonNull BatGuild guild) {
|
||||
guildRepository.save(guild);
|
||||
}
|
||||
@ -66,7 +74,11 @@ public class GuildService extends ListenerAdapter {
|
||||
* @return all guilds
|
||||
*/
|
||||
public List<BatGuild> getAllGuilds() {
|
||||
return guildRepository.findAll();
|
||||
List<BatGuild> guilds = new ArrayList<>();
|
||||
for (Guild guild : DiscordService.JDA.getGuilds()) {
|
||||
guilds.add(getGuild(guild.getId()));
|
||||
}
|
||||
return guilds;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user