From dfddcb2b54cdf11a6e98981c860df045212783a8 Mon Sep 17 00:00:00 2001 From: gkfktkrh153 Date: Tue, 19 Dec 2023 00:05:30 +0900 Subject: [PATCH 1/2] =?UTF-8?q?setting=20:=20Redis=20=EA=B8=B0=EB=B3=B8=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 7 ++++ .../common/redis/EmbeddedRedisConfig.java | 30 +++++++++++++ .../api/trip/common/redis/RedisConfig.java | 42 +++++++++++++++++++ .../api/trip/common/redis/RedisService.java | 37 ++++++++++++++++ src/main/resources/application.yml | 6 ++- 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/api/trip/common/redis/EmbeddedRedisConfig.java create mode 100644 src/main/java/com/api/trip/common/redis/RedisConfig.java create mode 100644 src/main/java/com/api/trip/common/redis/RedisService.java diff --git a/build.gradle b/build.gradle index 1c0c1b5..b8f3764 100644 --- a/build.gradle +++ b/build.gradle @@ -52,6 +52,13 @@ dependencies { runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5' + // redis + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.4.10' + + // Embedded Redis + testImplementation ('it.ozimov:embedded-redis:0.7.2') { exclude group: "org.slf4j", module: "slf4j-simple" } + implementation group: 'it.ozimov', name: 'embedded-redis', version: '0.7.2' + runtimeOnly 'com.mysql:mysql-connector-j' runtimeOnly 'com.h2database:h2' diff --git a/src/main/java/com/api/trip/common/redis/EmbeddedRedisConfig.java b/src/main/java/com/api/trip/common/redis/EmbeddedRedisConfig.java new file mode 100644 index 0000000..9e050b3 --- /dev/null +++ b/src/main/java/com/api/trip/common/redis/EmbeddedRedisConfig.java @@ -0,0 +1,30 @@ +package com.api.trip.common.redis; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import redis.embedded.RedisServer; + +@Configuration +@Profile("dev") +public class EmbeddedRedisConfig { + + private RedisServer redisServer; + + @Value("${custom.redis.port}") + private int redisPort; + + @PostConstruct + public void startRedis() { + redisServer = new RedisServer(redisPort); + redisServer.start(); + } + + @PreDestroy + public void stopRedis() { + redisServer.stop(); + } + +} diff --git a/src/main/java/com/api/trip/common/redis/RedisConfig.java b/src/main/java/com/api/trip/common/redis/RedisConfig.java new file mode 100644 index 0000000..e32a813 --- /dev/null +++ b/src/main/java/com/api/trip/common/redis/RedisConfig.java @@ -0,0 +1,42 @@ +package com.api.trip.common.redis; + +import lombok.RequiredArgsConstructor; +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.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import org.springframework.data.redis.serializer.GenericToStringSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@RequiredArgsConstructor +@Configuration +@EnableRedisRepositories +public class RedisConfig { + + @Value("${custom.redis.host}") + private String redisHost; + + @Value("${custom.redis.port}") + private int redisPort; + + @Bean + public LettuceConnectionFactory redisConnectionFactory() { + RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); + config.setHostName(redisHost); + config.setPort(redisPort); + return new LettuceConnectionFactory(config); + } + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory()); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new GenericToStringSerializer(Long.class)); + redisTemplate.setValueSerializer(new StringRedisSerializer()); + return redisTemplate; + } +} \ No newline at end of file diff --git a/src/main/java/com/api/trip/common/redis/RedisService.java b/src/main/java/com/api/trip/common/redis/RedisService.java new file mode 100644 index 0000000..b529e18 --- /dev/null +++ b/src/main/java/com/api/trip/common/redis/RedisService.java @@ -0,0 +1,37 @@ +package com.api.trip.common.redis; + + +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@RequiredArgsConstructor +@Service +public class RedisService { + + private final RedisTemplate redisTemplate; + + + public void setData(String key, Object value, Long time, TimeUnit timeUnit) { + redisTemplate.opsForValue().set(key, value.toString(), time, timeUnit); + } + + public Object getData(String key) { + return redisTemplate.opsForValue().get(key); + } + + + public void deleteData(String key) { + redisTemplate.delete(key); + } + + + public void increaseData(String key) { + redisTemplate.opsForValue().increment(key); + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 366d765..e4c0191 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -21,4 +21,8 @@ spring: logging: level: root: info - com.api.trip: debug \ No newline at end of file + com.api.trip: debug +custom: + redis: + host: localhost + port: 6379 \ No newline at end of file From 5fe9af49b5043b84c686f90b3f6d161ef9046981 Mon Sep 17 00:00:00 2001 From: gkfktkrh153 Date: Tue, 19 Dec 2023 15:33:37 +0900 Subject: [PATCH 2/2] =?UTF-8?q?remove=20:=20embeddedRedis=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/redis/EmbeddedRedisConfig.java | 30 ------------------- .../api/trip/common/redis/RedisConfig.java | 4 +-- src/main/resources/application.yml | 7 ++--- 3 files changed, 5 insertions(+), 36 deletions(-) delete mode 100644 src/main/java/com/api/trip/common/redis/EmbeddedRedisConfig.java diff --git a/src/main/java/com/api/trip/common/redis/EmbeddedRedisConfig.java b/src/main/java/com/api/trip/common/redis/EmbeddedRedisConfig.java deleted file mode 100644 index 9e050b3..0000000 --- a/src/main/java/com/api/trip/common/redis/EmbeddedRedisConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.api.trip.common.redis; - -import jakarta.annotation.PostConstruct; -import jakarta.annotation.PreDestroy; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; -import redis.embedded.RedisServer; - -@Configuration -@Profile("dev") -public class EmbeddedRedisConfig { - - private RedisServer redisServer; - - @Value("${custom.redis.port}") - private int redisPort; - - @PostConstruct - public void startRedis() { - redisServer = new RedisServer(redisPort); - redisServer.start(); - } - - @PreDestroy - public void stopRedis() { - redisServer.stop(); - } - -} diff --git a/src/main/java/com/api/trip/common/redis/RedisConfig.java b/src/main/java/com/api/trip/common/redis/RedisConfig.java index e32a813..f3bf51e 100644 --- a/src/main/java/com/api/trip/common/redis/RedisConfig.java +++ b/src/main/java/com/api/trip/common/redis/RedisConfig.java @@ -16,10 +16,10 @@ @EnableRedisRepositories public class RedisConfig { - @Value("${custom.redis.host}") + @Value("${spring.data.redis.host}") private String redisHost; - @Value("${custom.redis.port}") + @Value("${spring.data.redis.port}") private int redisPort; @Bean diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e4c0191..085b034 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -13,6 +13,9 @@ spring: web: pageable: one-indexed-parameters: true + redis: + host: localhost + port: 6379 servlet: multipart: max-file-size: 20MB @@ -22,7 +25,3 @@ logging: level: root: info com.api.trip: debug -custom: - redis: - host: localhost - port: 6379 \ No newline at end of file