From 27244d0d98081429b2337e5a336d089f2c1dd7a2 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 6 Jul 2024 00:34:53 +0100 Subject: [PATCH] use expiring map for users --- .../java/cc/fascinated/bat/service/UserService.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index 324983c..8320958 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -11,6 +11,7 @@ import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent; +import net.jodah.expiringmap.ExpiringMap; import org.bson.Document; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.DependsOn; @@ -33,7 +34,14 @@ public class UserService implements EventListener { /** * The cached users */ - private final Map users = new HashMap<>(); + private final Map 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 public UserService() {