From 3f0607ed4f2aed46340ca4856dab7722cc03be7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=8A=B9=EC=A7=84?= Date: Fri, 15 Mar 2024 20:40:26 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20cache=20api=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20redisson=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 1 - .../backend/config/redis/RedisConfig.java | 23 ++----------------- .../place/controller/PlaceController.java | 6 +++++ .../domain/place/service/PlaceService.java | 14 +++++++++-- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/backend/build.gradle b/backend/build.gradle index 7f0d4923..b5841005 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -67,7 +67,6 @@ dependencies { implementation 'org.springframework.cloud:spring-cloud-gcp-storage:1.2.5.RELEASE' implementation 'com.google.firebase:firebase-admin:6.8.1' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310' - implementation 'org.redisson:redisson-spring-data-30:3.27.1' annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta" diff --git a/backend/src/main/java/com/twtw/backend/config/redis/RedisConfig.java b/backend/src/main/java/com/twtw/backend/config/redis/RedisConfig.java index 6383d6d6..00c5940a 100644 --- a/backend/src/main/java/com/twtw/backend/config/redis/RedisConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/redis/RedisConfig.java @@ -1,13 +1,7 @@ package com.twtw.backend.config.redis; import com.twtw.backend.global.properties.RedisProperties; - import lombok.RequiredArgsConstructor; - -import org.redisson.Redisson; -import org.redisson.api.RedissonClient; -import org.redisson.config.Config; -import org.redisson.spring.data.connection.RedissonConnectionFactory; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; @@ -15,6 +9,7 @@ import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; 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.repository.configuration.EnableRedisRepositories; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; @@ -29,25 +24,11 @@ @RequiredArgsConstructor public class RedisConfig { private static final Long TIME_TO_LIVE = 1L; - private static final String REDISSON_HOST_PREFIX = "redis://"; - private static final String URL_DELIMITER = ":"; private final RedisProperties redisProperties; - @Bean - public RedissonClient redissonClient() { - final Config config = new Config(); - config.useSingleServer() - .setAddress( - REDISSON_HOST_PREFIX - + redisProperties.getHost() - + URL_DELIMITER - + redisProperties.getPort()); - return Redisson.create(config); - } - @Bean public RedisConnectionFactory redisConnectionFactory() { - return new RedissonConnectionFactory(redissonClient()); + return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort()); } @Bean diff --git a/backend/src/main/java/com/twtw/backend/domain/place/controller/PlaceController.java b/backend/src/main/java/com/twtw/backend/domain/place/controller/PlaceController.java index d6956b35..4f875c11 100644 --- a/backend/src/main/java/com/twtw/backend/domain/place/controller/PlaceController.java +++ b/backend/src/main/java/com/twtw/backend/domain/place/controller/PlaceController.java @@ -23,4 +23,10 @@ public ResponseEntity searchSurroundPlace( @ModelAttribute final SurroundPlaceRequest surroundPlaceRequest) { return ResponseEntity.ok(placeService.searchSurroundPlace(surroundPlaceRequest)); } + + @GetMapping("surround/cache") + public ResponseEntity searchSurroundPlaceWithCache( + @ModelAttribute final SurroundPlaceRequest surroundPlaceRequest) { + return ResponseEntity.ok(placeService.searchSurroundPlaceWithCache(surroundPlaceRequest)); + } } diff --git a/backend/src/main/java/com/twtw/backend/domain/place/service/PlaceService.java b/backend/src/main/java/com/twtw/backend/domain/place/service/PlaceService.java index 2422cd4c..03ff8d9f 100644 --- a/backend/src/main/java/com/twtw/backend/domain/place/service/PlaceService.java +++ b/backend/src/main/java/com/twtw/backend/domain/place/service/PlaceService.java @@ -10,6 +10,7 @@ import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @@ -19,12 +20,21 @@ public class PlaceService { private final PlaceMapper placeMapper; private final KakaoMapClient surroundPlaceClient; + @CacheEvict( + value = "surroundPlace", + key = "'searchSurroundPlace'.concat(#surroundPlaceRequest.toString())", + cacheManager = "cacheManager") + public PlaceResponse searchSurroundPlace(final SurroundPlaceRequest surroundPlaceRequest) { + final SurroundPlaceResponse response = surroundPlaceClient.request(surroundPlaceRequest); + return new PlaceResponse(response.getDocuments(), response.getMeta().getIsEnd()); + } + @Cacheable( value = "surroundPlace", - key = "'searchSurroundPlace'.concat(#surroundPlaceRequest)", + key = "'searchSurroundPlace'.concat(#surroundPlaceRequest.toString())", cacheManager = "cacheManager", unless = "#result.results.size() <= 0") - public PlaceResponse searchSurroundPlace(final SurroundPlaceRequest surroundPlaceRequest) { + public PlaceResponse searchSurroundPlaceWithCache(final SurroundPlaceRequest surroundPlaceRequest) { final SurroundPlaceResponse response = surroundPlaceClient.request(surroundPlaceRequest); return new PlaceResponse(response.getDocuments(), response.getMeta().getIsEnd()); }