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

Feat/create redis #410

Merged
merged 5 commits into from
Aug 22, 2023
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: 2 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

}

Expand Down
2 changes: 2 additions & 0 deletions backend/src/main/java/autoever2/cartag/CartagApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions backend/src/main/java/autoever2/cartag/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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;
Expand Down Expand Up @@ -47,6 +48,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<BoughtCarDto> getAllHistorySum() {
return carService.findAllBoughInfos();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ void testShare() {
void testBoughtInfo(){
List<BoughtCarDto> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
@ActiveProfiles("test")
@Sql(scripts = {"classpath:/insert/insertColor-h2.sql"})
class ColorRepositoryTest {

private final ColorRepository repository;

@Autowired
Expand Down