From 3fc26583f22c8cf29cc59f414a3a7b7f7c4fb8de Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 26 Jun 2024 14:05:51 +0100 Subject: [PATCH] re-add caching (featuring Redis!!!!!) --- pom.xml | 6 ++- .../bat/config/RedisCacheConfig.java | 50 +++++++++++++++++++ .../cc/fascinated/bat/model/BatGuild.java | 5 +- .../java/cc/fascinated/bat/model/BatUser.java | 5 +- .../fascinated/bat/service/GuildService.java | 6 ++- .../fascinated/bat/service/UserService.java | 6 ++- src/main/resources/application.yml | 14 +++++- 7 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/config/RedisCacheConfig.java diff --git a/pom.xml b/pom.xml index caf12f6..5de353c 100644 --- a/pom.xml +++ b/pom.xml @@ -87,7 +87,7 @@ org.springframework.boot - spring-boot-starter-cache + spring-boot-starter-data-redis @@ -114,6 +114,10 @@ httpclient5 5.3.1 + + redis.clients + jedis + diff --git a/src/main/java/cc/fascinated/bat/config/RedisCacheConfig.java b/src/main/java/cc/fascinated/bat/config/RedisCacheConfig.java new file mode 100644 index 0000000..d44af8d --- /dev/null +++ b/src/main/java/cc/fascinated/bat/config/RedisCacheConfig.java @@ -0,0 +1,50 @@ +package cc.fascinated.bat.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; + +import java.time.Duration; + +/** + * @author Fascinated (fascinated7) + */ +@Configuration +public class RedisCacheConfig { + @Value(value="${spring.redis.host}") + private String host; + + @Value(value="${spring.redis.port}") + private String port; + + @Value(value="${spring.redis.timeout}") + private String timeout; + + @Value(value="${spring.redis.database}") + private String database; + + @Bean + JedisConnectionFactory jedisConnectionFactory() { + RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); + redisStandaloneConfiguration.setHostName(host); + redisStandaloneConfiguration.setPort(Integer.parseInt(port)); + redisStandaloneConfiguration.setDatabase(Integer.parseInt(database)); + + JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder(); + jedisClientConfiguration.connectTimeout(Duration.ofSeconds(Integer.parseInt(timeout)));// connection timeout + + return new JedisConnectionFactory(redisStandaloneConfiguration, + jedisClientConfiguration.build()); + } + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(jedisConnectionFactory()); + return template; + } +} diff --git a/src/main/java/cc/fascinated/bat/model/BatGuild.java b/src/main/java/cc/fascinated/bat/model/BatGuild.java index 039b52e..727cb44 100644 --- a/src/main/java/cc/fascinated/bat/model/BatGuild.java +++ b/src/main/java/cc/fascinated/bat/model/BatGuild.java @@ -10,6 +10,8 @@ import net.dv8tion.jda.api.entities.Guild; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import java.io.Serial; +import java.io.Serializable; import java.util.Date; /** @@ -18,7 +20,8 @@ import java.util.Date; @RequiredArgsConstructor @Getter @Setter @Document(collection = "guilds") -public class BatGuild extends ProfileHolder { +public class BatGuild extends ProfileHolder implements Serializable { + @Serial private static final long serialVersionUID = 1L; /** * The ID of the guild diff --git a/src/main/java/cc/fascinated/bat/model/BatUser.java b/src/main/java/cc/fascinated/bat/model/BatUser.java index ffec7b6..c7abc5d 100644 --- a/src/main/java/cc/fascinated/bat/model/BatUser.java +++ b/src/main/java/cc/fascinated/bat/model/BatUser.java @@ -10,6 +10,8 @@ import net.dv8tion.jda.api.entities.User; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import java.io.Serial; +import java.io.Serializable; import java.util.Date; /** @@ -18,7 +20,8 @@ import java.util.Date; @RequiredArgsConstructor @Getter @Setter @Document(collection = "users") -public class BatUser extends ProfileHolder { +public class BatUser extends ProfileHolder implements Serializable { + @Serial private static final long serialVersionUID = 1L; /** * The ID of the user diff --git a/src/main/java/cc/fascinated/bat/service/GuildService.java b/src/main/java/cc/fascinated/bat/service/GuildService.java index 17ed833..65e9106 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,7 +38,7 @@ public class GuildService extends ListenerAdapter { * @param id The ID of the guild * @return The guild */ -// @Cacheable(cacheNames = {"guilds"}, key = "#id") + @Cacheable(cacheNames = {"guilds"}, key = "#id") public BatGuild getGuild(@NonNull String id) { long start = System.currentTimeMillis(); Optional optionalGuild = guildRepository.findById(id); @@ -53,7 +55,7 @@ public class GuildService extends ListenerAdapter { * * @param guild The guild to save */ -// @CachePut(cacheNames = {"guilds"}, key = "#id") + @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/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index 2c2ceb4..e2980fc 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,7 +34,7 @@ public class UserService { * @param id The ID of the user * @return The user */ -// @Cacheable(cacheNames = {"users"}, key = "#id") + @Cacheable(cacheNames = {"users"}, key = "#id") public BatUser getUser(@NonNull String id) { long start = System.currentTimeMillis(); Optional optionalUser = userRepository.findById(id); @@ -49,7 +51,7 @@ public class UserService { * * @param user The user to save */ -// @CachePut(cacheNames = {"users"}, key = "#id") + @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 4235a83..89428f2 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,9 +6,21 @@ spring: # Disable the Spring Web Server main: web-application-type: none + + # Database caching + cache: + type: redis + redis: + time-to-live: 60000 + redis: + host: localhost + port: 6379 + timeout: 5000 + database: 0 + data: # MongoDB Configuration mongodb: uri: "mongodb://bat:p4$$w0rd@localhost:27017" database: "bat" - auto-index-creation: true # Automatically create collection indexes + auto-index-creation: true # Automatically create collection indexes \ No newline at end of file