use expiring map for guild and users

This commit is contained in:
Lee 2024-06-27 23:51:40 +01:00
parent 63fbe27754
commit 5c7a067f7a
2 changed files with 10 additions and 2 deletions

View File

@ -8,12 +8,14 @@ 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 net.jodah.expiringmap.ExpiringMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author Fascinated (fascinated7)
@ -24,7 +26,9 @@ public class GuildService extends ListenerAdapter {
/**
* The cached guilds
*/
private final Map<String, BatGuild> guilds = new HashMap<>();
private final Map<String, BatGuild> guilds = ExpiringMap.builder()
.expiration(6, TimeUnit.HOURS)
.build();
/**
* The guild repository to use

View File

@ -5,6 +5,7 @@ import cc.fascinated.bat.repository.UserRepository;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import net.jodah.expiringmap.ExpiringMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
@ -12,6 +13,7 @@ import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
* @author Fascinated (fascinated7)
@ -22,7 +24,9 @@ public class UserService {
/**
* The cached users
*/
private final Map<String, BatUser> users = new HashMap<>();
private final Map<String, BatUser> users = ExpiringMap.builder()
.expiration(6, TimeUnit.HOURS)
.build();
/**
* The user repository to use