in-mem cache

This commit is contained in:
Lee 2024-06-26 21:29:31 +01:00
parent 52e5d50782
commit 6119da81bf
6 changed files with 15 additions and 13 deletions

@ -85,6 +85,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId> <artifactId>spring-boot-starter-websocket</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Libraries --> <!-- Libraries -->
<dependency> <dependency>

@ -7,6 +7,7 @@ import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import java.io.File; import java.io.File;
@ -14,7 +15,7 @@ import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.Objects; import java.util.Objects;
@EnableScheduling @EnableScheduling @EnableCaching
@SpringBootApplication @SpringBootApplication
@Log4j2(topic = "Ember") @Log4j2(topic = "Ember")
public class BatApplication { public class BatApplication {

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent; import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired; 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.context.annotation.DependsOn;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -36,6 +38,7 @@ public class GuildService extends ListenerAdapter {
* @param id The ID of the guild * @param id The ID of the guild
* @return The guild * @return The guild
*/ */
@Cacheable(cacheNames = "guilds", key = "#id")
public BatGuild getGuild(@NonNull String id) { public BatGuild getGuild(@NonNull String id) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Optional<BatGuild> optionalGuild = guildRepository.findById(id); Optional<BatGuild> optionalGuild = guildRepository.findById(id);
@ -52,6 +55,7 @@ public class GuildService extends ListenerAdapter {
* *
* @param guild The guild to save * @param guild The guild to save
*/ */
@CachePut(cacheNames = "guilds", key = "#guild.id")
public void saveGuild(@NonNull BatGuild guild) { public void saveGuild(@NonNull BatGuild guild) {
guildRepository.save(guild); guildRepository.save(guild);
} }

@ -13,6 +13,7 @@ import lombok.NonNull;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TextMessage;

@ -5,6 +5,8 @@ import cc.fascinated.bat.repository.UserRepository;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; 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.context.annotation.DependsOn;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -32,6 +34,7 @@ public class UserService {
* @param id The ID of the user * @param id The ID of the user
* @return The user * @return The user
*/ */
@Cacheable(cacheNames = "users", key = "#id")
public BatUser getUser(@NonNull String id) { public BatUser getUser(@NonNull String id) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Optional<BatUser> optionalUser = userRepository.findById(id); Optional<BatUser> optionalUser = userRepository.findById(id);
@ -48,6 +51,7 @@ public class UserService {
* *
* @param user The user to save * @param user The user to save
*/ */
@CachePut(cacheNames = "users", key = "#user.id")
public void saveUser(@NonNull BatUser user) { public void saveUser(@NonNull BatUser user) {
userRepository.save(user); userRepository.save(user);
} }

@ -6,18 +6,6 @@ spring:
# Disable the Spring Web Server # Disable the Spring Web Server
main: main:
web-application-type: none 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: data:
# MongoDB Configuration # MongoDB Configuration
mongodb: mongodb: