From 5c7a067f7a3a93ca98f3ec3491f1a3929aea5d26 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 27 Jun 2024 23:51:40 +0100 Subject: [PATCH] use expiring map for guild and users --- src/main/java/cc/fascinated/bat/service/GuildService.java | 6 +++++- src/main/java/cc/fascinated/bat/service/UserService.java | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/service/GuildService.java b/src/main/java/cc/fascinated/bat/service/GuildService.java index 2a41387..2ae2132 100644 --- a/src/main/java/cc/fascinated/bat/service/GuildService.java +++ b/src/main/java/cc/fascinated/bat/service/GuildService.java @@ -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 guilds = new HashMap<>(); + private final Map guilds = ExpiringMap.builder() + .expiration(6, TimeUnit.HOURS) + .build(); /** * The guild repository to use diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index 37b8aed..1297c7d 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -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 users = new HashMap<>(); + private final Map users = ExpiringMap.builder() + .expiration(6, TimeUnit.HOURS) + .build(); /** * The user repository to use