Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting: redis setting #47

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend-config
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// Redisson
implementation 'org.redisson:redisson-spring-boot-starter:3.17.6'
//jwt
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.thirdparty.ticketing.global.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import lombok.Setter;

@Configuration
@ConfigurationProperties(prefix = "spring.data.redis.lettuce")
@Setter
public class LettuceConfig {
private String host;
private Integer port;

@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
return new LettuceConnectionFactory();
}

@Bean
public StringRedisTemplate lettuceRedisTemplate(
LettuceConnectionFactory lettuceConnectionFactory) {
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.thirdparty.ticketing.global.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import lombok.Setter;

@Configuration
@ConfigurationProperties(prefix = "spring.data.redis.redisson")
@Setter
public class RedissonConfig {
private String host;
private Integer port;

@Bean
public RedissonClient redissonClient() {
Config config = new Config();
SingleServerConfig serverConfig = config.useSingleServer();
serverConfig.setAddress("redis://" + host + ":" + port);
return Redisson.create(config);
}

@Bean
public StringRedisTemplate redissonRedisTemplate(RedissonClient redissonClient) {
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(new RedissonConnectionFactory(redissonClient));
return redisTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.thirdparty.ticketing.global.config;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest(
properties = {
"spring.data.redis.lettuce.host=localhost",
"spring.data.redis.lettuce.port=6379"
})
class LettuceConfigTest {

@Autowired private StringRedisTemplate lettuceRedisTemplate;

@Test
void testLettuceRedisTemplate() {
assertThat(lettuceRedisTemplate).isNotNull();
lettuceRedisTemplate.opsForValue().set("testKey", "testValue");
String value = lettuceRedisTemplate.opsForValue().get("testKey");
assertThat(value).isEqualTo("testValue");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thirdparty.ticketing.global.config;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest(
properties = {
"spring.data.redis.redisson.host=localhost",
"spring.data.redis.redisson.port=6379"
})
class RedissonConfigTest {

@Autowired private RedissonClient redissonClient;

@Autowired private StringRedisTemplate redissonRedisTemplate;

@Test
void testRedissonClient() {
assertThat(redissonClient).isNotNull();
}

@Test
void testRedissonRedisTemplate() {
assertThat(redissonRedisTemplate).isNotNull();
redissonRedisTemplate.opsForValue().set("testKey", "testValue");
String value = redissonRedisTemplate.opsForValue().get("testKey");
assertThat(value).isEqualTo("testValue");
}
}
Loading