forked from Fascinated/Bat
re-add caching (featuring Redis!!!!!)
This commit is contained in:
parent
b3a5a4c02e
commit
3fc26583f2
6
pom.xml
6
pom.xml
@ -87,7 +87,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-cache</artifactId>
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Libraries -->
|
<!-- Libraries -->
|
||||||
@ -114,6 +114,10 @@
|
|||||||
<artifactId>httpclient5</artifactId>
|
<artifactId>httpclient5</artifactId>
|
||||||
<version>5.3.1</version>
|
<version>5.3.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>redis.clients</groupId>
|
||||||
|
<artifactId>jedis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Test Dependencies -->
|
<!-- Test Dependencies -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
50
src/main/java/cc/fascinated/bat/config/RedisCacheConfig.java
Normal file
50
src/main/java/cc/fascinated/bat/config/RedisCacheConfig.java
Normal file
@ -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.annotation.Id;
|
||||||
import org.springframework.data.mongodb.core.mapping.Document;
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,7 +20,8 @@ import java.util.Date;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Getter @Setter
|
@Getter @Setter
|
||||||
@Document(collection = "guilds")
|
@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
|
* 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.annotation.Id;
|
||||||
import org.springframework.data.mongodb.core.mapping.Document;
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,7 +20,8 @@ import java.util.Date;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Getter @Setter
|
@Getter @Setter
|
||||||
@Document(collection = "users")
|
@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
|
* 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.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,7 +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")
|
@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);
|
||||||
@ -53,7 +55,7 @@ public class GuildService extends ListenerAdapter {
|
|||||||
*
|
*
|
||||||
* @param guild The guild to save
|
* @param guild The guild to save
|
||||||
*/
|
*/
|
||||||
// @CachePut(cacheNames = {"guilds"}, key = "#id")
|
@CachePut(cacheNames = {"guilds"}, key = "#guild.id")
|
||||||
public void saveGuild(@NonNull BatGuild guild) {
|
public void saveGuild(@NonNull BatGuild guild) {
|
||||||
guildRepository.save(guild);
|
guildRepository.save(guild);
|
||||||
}
|
}
|
||||||
|
@ -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,7 +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")
|
@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);
|
||||||
@ -49,7 +51,7 @@ public class UserService {
|
|||||||
*
|
*
|
||||||
* @param user The user to save
|
* @param user The user to save
|
||||||
*/
|
*/
|
||||||
// @CachePut(cacheNames = {"users"}, key = "#id")
|
@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,9 +6,21 @@ 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: 60000
|
||||||
|
redis:
|
||||||
|
host: localhost
|
||||||
|
port: 6379
|
||||||
|
timeout: 5000
|
||||||
|
database: 0
|
||||||
|
|
||||||
data:
|
data:
|
||||||
# MongoDB Configuration
|
# MongoDB Configuration
|
||||||
mongodb:
|
mongodb:
|
||||||
uri: "mongodb://bat:p4$$w0rd@localhost:27017"
|
uri: "mongodb://bat:p4$$w0rd@localhost:27017"
|
||||||
database: "bat"
|
database: "bat"
|
||||||
auto-index-creation: true # Automatically create collection indexes
|
auto-index-creation: true # Automatically create collection indexes
|
Loading…
Reference in New Issue
Block a user