use expiring map for users

This commit is contained in:
Lee 2024-07-06 00:34:53 +01:00
parent de1da2391d
commit 27244d0d98

@ -11,6 +11,7 @@ import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent;
import net.jodah.expiringmap.ExpiringMap;
import org.bson.Document; import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.DependsOn;
@ -33,7 +34,14 @@ public class UserService implements EventListener {
/** /**
* The cached users * The cached users
*/ */
private final Map<String, BatUser> users = new HashMap<>(); private final Map<String, BatUser> users = ExpiringMap.builder()
.expiration(1, TimeUnit.HOURS)
.expirationListener((key, cachedUser) -> {
BatUser user = (BatUser) cachedUser;
log.info("Removed user \"{}\" - \"{}\" from cache", user.getName(), user.getId());
user.save();
})
.build();
@Autowired @Autowired
public UserService() { public UserService() {