From 67403b5cd72c3537c6c0f04e80ee6339467cc835 Mon Sep 17 00:00:00 2001 From: dydwo0740 <80679257+dydwo0740@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:41:05 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[FEAT]=20#226=20:=20redis=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A3=BC?= =?UTF-8?q?=EC=9E=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 2 ++ backend/src/main/java/autoever2/cartag/CahcheConfig.java | 0 2 files changed, 2 insertions(+) create mode 100644 backend/src/main/java/autoever2/cartag/CahcheConfig.java diff --git a/backend/build.gradle b/backend/build.gradle index 29333fb..0aa6e21 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -33,6 +33,8 @@ dependencies { testImplementation group: 'com.h2database', name: 'h2', version: '2.2.220' implementation 'org.springdoc:springdoc-openapi-ui:1.6.9' implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' + implementation 'org.springframework.boot:spring-boot-starter-data-redis' + implementation 'org.springframework.boot:spring-boot-starter-cache' } diff --git a/backend/src/main/java/autoever2/cartag/CahcheConfig.java b/backend/src/main/java/autoever2/cartag/CahcheConfig.java new file mode 100644 index 0000000..e69de29 From 9d46f7231ceb35a8cfa0ef87bc2df9214499979a Mon Sep 17 00:00:00 2001 From: dydwo0740 <80679257+dydwo0740@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:41:36 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[FEAT]=20#226=20:=20redis=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20config=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/autoever2/cartag/CahcheConfig.java | 0 .../autoever2/cartag/CartagApplication.java | 2 + .../java/autoever2/cartag/RedisConfig.java | 44 +++++++++++++++++++ 3 files changed, 46 insertions(+) delete mode 100644 backend/src/main/java/autoever2/cartag/CahcheConfig.java create mode 100644 backend/src/main/java/autoever2/cartag/RedisConfig.java diff --git a/backend/src/main/java/autoever2/cartag/CahcheConfig.java b/backend/src/main/java/autoever2/cartag/CahcheConfig.java deleted file mode 100644 index e69de29..0000000 diff --git a/backend/src/main/java/autoever2/cartag/CartagApplication.java b/backend/src/main/java/autoever2/cartag/CartagApplication.java index 953ef87..21ae507 100644 --- a/backend/src/main/java/autoever2/cartag/CartagApplication.java +++ b/backend/src/main/java/autoever2/cartag/CartagApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication +@EnableCaching public class CartagApplication { public static void main(String[] args) { SpringApplication.run(CartagApplication.class, args); diff --git a/backend/src/main/java/autoever2/cartag/RedisConfig.java b/backend/src/main/java/autoever2/cartag/RedisConfig.java new file mode 100644 index 0000000..e0104bc --- /dev/null +++ b/backend/src/main/java/autoever2/cartag/RedisConfig.java @@ -0,0 +1,44 @@ +package autoever2.cartag; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +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.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; + +import java.time.Duration; + +@EnableCaching +@Configuration +@PropertySource("classpath:application.yml") +public class RedisConfig { + + @Value("${spring.redis.host}") // application.properties 에서 불러옴 + private String host; + + @Value("${spring.redis.port}") // application.properties 에서 불러옴 + private int port; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(host, port); + } + + @Bean + public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { + RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory()); + RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig() + .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) + .entryTtl(Duration.ofHours(12)); // TTL 12시간으로 지정 + builder.cacheDefaults(configuration); + + return builder.build(); + } +} \ No newline at end of file From e245ee24e00dd5f7749ad88919b85258eaf19ac1 Mon Sep 17 00:00:00 2001 From: dydwo0740 <80679257+dydwo0740@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:41:52 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[FEAT]=20#226=20:=20redis=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EC=85=98=EC=9D=84=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cartag/controller/QuoteController.java | 2 ++ .../autoever2/cartag/domain/car/BoughtCarDto.java | 15 ++++++++++++++- .../autoever2/cartag/integration/QuoteTest.java | 6 +++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/autoever2/cartag/controller/QuoteController.java b/backend/src/main/java/autoever2/cartag/controller/QuoteController.java index 954ad33..42498ac 100644 --- a/backend/src/main/java/autoever2/cartag/controller/QuoteController.java +++ b/backend/src/main/java/autoever2/cartag/controller/QuoteController.java @@ -13,6 +13,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.Cacheable; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -44,6 +45,7 @@ public HistoryShortDto getRecommendedList(@RequestBody QuoteDataDto quoteDataDto @ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = BoughtCarDto.class))), }) @GetMapping("bought/infos") + @Cacheable(value = "boughtlist") public List getAllHistorySum() { return carService.findAllBoughInfos(); } diff --git a/backend/src/main/java/autoever2/cartag/domain/car/BoughtCarDto.java b/backend/src/main/java/autoever2/cartag/domain/car/BoughtCarDto.java index 92143a0..de7ac85 100644 --- a/backend/src/main/java/autoever2/cartag/domain/car/BoughtCarDto.java +++ b/backend/src/main/java/autoever2/cartag/domain/car/BoughtCarDto.java @@ -3,14 +3,27 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.springframework.data.redis.core.RedisHash; + +import java.beans.ConstructorProperties; @Getter -@Builder +@Setter +@NoArgsConstructor +//@RedisHash(value = "test") @Schema(description = "구매된 차량의 가격과 그에 따른 갯수 반환 DTO") public class BoughtCarDto { private Long totalPrice; private int count; + @Builder + @ConstructorProperties({"totalPrice", "count"}) + public BoughtCarDto(Long totalPrice, int count) { + this.totalPrice = totalPrice; + this.count = count; + } public static BoughtCarDto toBoughtCarDto(Long totalPrice, int count) { return BoughtCarDto.builder() diff --git a/backend/src/test/java/autoever2/cartag/integration/QuoteTest.java b/backend/src/test/java/autoever2/cartag/integration/QuoteTest.java index e3acfcd..fcf8c81 100644 --- a/backend/src/test/java/autoever2/cartag/integration/QuoteTest.java +++ b/backend/src/test/java/autoever2/cartag/integration/QuoteTest.java @@ -78,8 +78,8 @@ void testShare() { void testBoughtInfo(){ List allHistorySum = quoteController.getAllHistorySum(); - assertEquals(6, allHistorySum.size()); - assertEquals(2, allHistorySum.get(0).getCount()); - assertEquals(42300000L, allHistorySum.get(1).getTotalPrice()); + assertEquals(144, allHistorySum.size()); + assertEquals(228, allHistorySum.get(0).getCount()); + assertEquals(46200000L, allHistorySum.get(1).getTotalPrice()); } } From b7b905aeec6c6d4ed2fe227d38df10d648481e6c Mon Sep 17 00:00:00 2001 From: dydwo0740 <80679257+dydwo0740@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:47:24 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[FEAT]=20#226=20:=20redis=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EC=85=98=EC=9D=84=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/autoever2/cartag/repository/ColorRepositoryTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/test/java/autoever2/cartag/repository/ColorRepositoryTest.java b/backend/src/test/java/autoever2/cartag/repository/ColorRepositoryTest.java index 8223e3e..f44d554 100644 --- a/backend/src/test/java/autoever2/cartag/repository/ColorRepositoryTest.java +++ b/backend/src/test/java/autoever2/cartag/repository/ColorRepositoryTest.java @@ -25,7 +25,6 @@ @ActiveProfiles("test") @Sql(scripts = {"classpath:/insert/insertColor-h2.sql"}) class ColorRepositoryTest { - private final ColorRepository repository; @Autowired