From 8e09d4e6686365e637406bea768c22d73230ae3a Mon Sep 17 00:00:00 2001 From: baejunil Date: Wed, 22 Nov 2023 23:43:09 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20redis=20=EB=8F=99=EC=9E=91=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=EC=9D=84=20=EC=9C=84=ED=95=9C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20api=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-dev.yml | 2 +- .../anifriends/global/config/RedisConfig.java | 24 ++++++------ src/main/resources/backend-config | 2 +- .../global/RedisTestController.java | 38 +++++++++++++++++++ 4 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 src/test/java/com/clova/anifriends/global/RedisTestController.java diff --git a/.github/workflows/cd-dev.yml b/.github/workflows/cd-dev.yml index ccbc46f52..c0c3efbc7 100644 --- a/.github/workflows/cd-dev.yml +++ b/.github/workflows/cd-dev.yml @@ -2,7 +2,7 @@ name: cd-dev-docker on: push: - branches: [ "dev" ] + branches: [ "dev", "feat/#298" ] jobs: build: diff --git a/src/main/java/com/clova/anifriends/global/config/RedisConfig.java b/src/main/java/com/clova/anifriends/global/config/RedisConfig.java index ee7ddebad..3d2d3486b 100644 --- a/src/main/java/com/clova/anifriends/global/config/RedisConfig.java +++ b/src/main/java/com/clova/anifriends/global/config/RedisConfig.java @@ -1,19 +1,23 @@ package com.clova.anifriends.global.config; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericToStringSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; +@EnableCaching @Configuration public class RedisConfig { - @Value("${spring.redis.host}") + @Value("${spring.data.redis.host}") private String host; - @Value("${spring.redis.port}") + @Value("${spring.data.redis.port}") private int port; @Bean @@ -21,18 +25,12 @@ public RedisConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(host, port); } -// @Bean -// public RedisTemplate redisTemplate() { -// RedisTemplate redisTemplate = new RedisTemplate<>(); -// redisTemplate.setKeySerializer(new StringRedisSerializer()); -// redisTemplate.setValueSerializer(new StringRedisSerializer()); -// redisTemplate.setConnectionFactory(redisConnectionFactory()); -// return redisTemplate; -// } - @Bean - public RedisTemplate redisTemplate() { - RedisTemplate redisTemplate = new RedisTemplate<>(); + public RedisTemplate redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer( + new GenericToStringSerializer<>(Integer.class)); redisTemplate.setConnectionFactory(redisConnectionFactory()); return redisTemplate; } diff --git a/src/main/resources/backend-config b/src/main/resources/backend-config index 313b0a86e..7dcc9f245 160000 --- a/src/main/resources/backend-config +++ b/src/main/resources/backend-config @@ -1 +1 @@ -Subproject commit 313b0a86e8ccdf29967106967f73e16b915ca086 +Subproject commit 7dcc9f245798480517b8386f78cff09823f933e4 diff --git a/src/test/java/com/clova/anifriends/global/RedisTestController.java b/src/test/java/com/clova/anifriends/global/RedisTestController.java new file mode 100644 index 000000000..5b8d94c19 --- /dev/null +++ b/src/test/java/com/clova/anifriends/global/RedisTestController.java @@ -0,0 +1,38 @@ +package com.clova.anifriends.global; + +import java.util.Map; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class RedisTestController { + + private final RedisTemplate redisTemplate; + + public RedisTestController(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + @PostMapping("/data") + public ResponseEntity setRedisData( + @RequestBody(required = true) Map map) throws Exception { + + redisTemplate.opsForValue().set(String.valueOf(map.get("key")), map.get("value")); + + return new ResponseEntity<>("정상 등록", HttpStatus.CREATED); + } + + @GetMapping("/data") + public ResponseEntity getRedisData( + @RequestParam(required = true) String key) { + + return new ResponseEntity<>(redisTemplate.opsForValue().get(key), HttpStatus.OK); + + } +}