From 6119da81bfc84a6c87e3c0ad86eb3f5aeb9166cd Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 26 Jun 2024 21:29:31 +0100 Subject: [PATCH] in-mem cache --- pom.xml | 4 ++++ src/main/java/cc/fascinated/bat/BatApplication.java | 3 ++- .../java/cc/fascinated/bat/service/GuildService.java | 4 ++++ .../cc/fascinated/bat/service/ScoreSaberService.java | 1 + .../java/cc/fascinated/bat/service/UserService.java | 4 ++++ src/main/resources/application.yml | 12 ------------ 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index afe8af3..caf12f6 100644 --- a/pom.xml +++ b/pom.xml @@ -85,6 +85,10 @@ org.springframework.boot spring-boot-starter-websocket + + org.springframework.boot + spring-boot-starter-cache + diff --git a/src/main/java/cc/fascinated/bat/BatApplication.java b/src/main/java/cc/fascinated/bat/BatApplication.java index 8782999..36e3b53 100644 --- a/src/main/java/cc/fascinated/bat/BatApplication.java +++ b/src/main/java/cc/fascinated/bat/BatApplication.java @@ -7,6 +7,7 @@ import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.scheduling.annotation.EnableScheduling; import java.io.File; @@ -14,7 +15,7 @@ import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Objects; -@EnableScheduling +@EnableScheduling @EnableCaching @SpringBootApplication @Log4j2(topic = "Ember") public class BatApplication { diff --git a/src/main/java/cc/fascinated/bat/service/GuildService.java b/src/main/java/cc/fascinated/bat/service/GuildService.java index c31d9f5..6a8fe1d 100644 --- a/src/main/java/cc/fascinated/bat/service/GuildService.java +++ b/src/main/java/cc/fascinated/bat/service/GuildService.java @@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2; 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; @@ -36,6 +38,7 @@ 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) { long start = System.currentTimeMillis(); Optional optionalGuild = guildRepository.findById(id); @@ -52,6 +55,7 @@ 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); } diff --git a/src/main/java/cc/fascinated/bat/service/ScoreSaberService.java b/src/main/java/cc/fascinated/bat/service/ScoreSaberService.java index a976d4d..479012e 100644 --- a/src/main/java/cc/fascinated/bat/service/ScoreSaberService.java +++ b/src/main/java/cc/fascinated/bat/service/ScoreSaberService.java @@ -13,6 +13,7 @@ import lombok.NonNull; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index aaec511..6630c34 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -5,6 +5,8 @@ import cc.fascinated.bat.repository.UserRepository; import lombok.NonNull; import lombok.extern.log4j.Log4j2; 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; @@ -32,6 +34,7 @@ public class UserService { * @param id The ID of the user * @return The user */ + @Cacheable(cacheNames = "users", key = "#id") public BatUser getUser(@NonNull String id) { long start = System.currentTimeMillis(); Optional optionalUser = userRepository.findById(id); @@ -48,6 +51,7 @@ public class UserService { * * @param user The user to save */ + @CachePut(cacheNames = "users", key = "#user.id") public void saveUser(@NonNull BatUser user) { userRepository.save(user); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index fd1aeeb..a75ff70 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,18 +6,6 @@ spring: # Disable the Spring Web Server main: web-application-type: none - - # Database caching - cache: - type: redis - redis: - time-to-live: 300000 # 5 minutes - redis: - host: localhost - port: 6379 - timeout: 5000 - database: 0 - data: # MongoDB Configuration mongodb: