re-add caching (featuring Redis!!!!!)

This commit is contained in:
Lee 2024-06-26 14:05:51 +01:00
parent b3a5a4c02e
commit 3fc26583f2
7 changed files with 84 additions and 8 deletions

@ -87,7 +87,7 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Libraries -->
@ -114,6 +114,10 @@
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- Test Dependencies -->
<dependency>

@ -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<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
}

@ -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

@ -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

@ -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<BatGuild> 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);
}

@ -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<BatUser> 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);
}

@ -6,6 +6,18 @@ 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: