Skip to content

Commit

Permalink
Merge pull request #142 from Team-Shaka/feature/118
Browse files Browse the repository at this point in the history
โœจ  Feat: API ์‘๋‹ต ์บ์‹ฑ์„ ํ†ตํ•œ ๋ธŒ๋ฆฌํ•‘ V2 ์กฐํšŒ ์†๋„ ๊ฐœ์„ 
  • Loading branch information
swa07016 authored Jan 5, 2024
2 parents 7a1afa9 + 7fc9200 commit c6ed247
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main/java/briefing/BriefingApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
Expand All @@ -17,6 +18,7 @@
@Server(url = "https://api.newsbreifing.store", description = "release server")
})
@SpringBootApplication
@EnableCaching
@EnableFeignClients
@EnableRedisRepositories
@ImportAutoConfiguration({FeignAutoConfiguration.class})
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/briefing/briefing/api/BriefingV2Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Optional;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import briefing.briefing.application.BriefingCommandService;
Expand Down Expand Up @@ -32,6 +33,7 @@ public class BriefingV2Api {

@GetMapping("/briefings")
@Operation(summary = "03-01Briefing \uD83D\uDCF0 ๋ธŒ๋ฆฌํ•‘ ๋ชฉ๋ก ์กฐํšŒ V2", description = "")
@Cacheable(value = "findBriefingsV2", key = "#params.toString()")
public CommonResponse<BriefingResponseDTO.BriefingPreviewListDTOV2> findBriefingsV2(
@ParameterObject @ModelAttribute BriefingRequestParam.BriefingPreviewListParam params) {
List<Briefing> briefingList = briefingQueryService.findBriefings(params, APIVersion.V2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class BriefingRequestParam {
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public static class BriefingPreviewListParam {
@NotNull private BriefingType type;
private LocalDate date;
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/briefing/common/response/CommonResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
public class CommonResponse<T> {

@JsonProperty("isSuccess")
private final Boolean isSuccess;
private Boolean isSuccess;

private final String code;
private final String message;
@JsonProperty("code")
private String code;

@JsonProperty("message")
private String message;

@JsonProperty("result")
private T result;

// ์š”์ฒญ์— ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ11
// ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ์ถ”๊ฐ€
public CommonResponse() {}

public static <T> CommonResponse<T> onSuccess(T data) {
return new CommonResponse<>(true, "์š”์ฒญ์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค.", "1000", data);
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/briefing/config/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package briefing.config;

import java.time.Duration;

import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

@Configuration
public class CacheConfig {

@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
PolymorphicTypeValidator typeValidator =
BasicPolymorphicTypeValidator.builder().allowIfSubType(Object.class).build();

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.activateDefaultTyping(typeValidator, ObjectMapper.DefaultTyping.NON_FINAL);

RedisCacheConfiguration cacheConfiguration =
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30)) // ์บ์‹œ ์œ ํšจ ์‹œ๊ฐ„ 30๋ถ„
.disableCachingNullValues()
.prefixCacheNameWith("responseCache::") // ์บ์‹œ ํ‚ค ์ ‘๋‘์‚ฌ ์„ค์ •
.serializeKeysWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new GenericJackson2JsonRedisSerializer(objectMapper)));

return RedisCacheManager.builder(connectionFactory)
.cacheDefaults(cacheConfiguration)
.build();
}
}
3 changes: 3 additions & 0 deletions src/main/java/briefing/scrap/api/ScrapV2Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import briefing.common.response.CommonResponse;
Expand All @@ -22,6 +23,7 @@ public class ScrapV2Api {
private final ScrapQueryService scrapQueryService;
private final ScrapCommandService scrapCommandService;

@CacheEvict(value = "findBriefingsV2", allEntries = true)
@Operation(summary = "05-01 Scrap๐Ÿ“ ์Šคํฌ๋žฉํ•˜๊ธฐ V2", description = "๋ธŒ๋ฆฌํ•‘์„ ์Šคํฌ๋žฉํ•˜๋Š” API์ž…๋‹ˆ๋‹ค.")
@PostMapping("/scraps/briefings")
public CommonResponse<ScrapResponse.CreateDTOV2> createV2(
Expand All @@ -31,6 +33,7 @@ public CommonResponse<ScrapResponse.CreateDTOV2> createV2(
return CommonResponse.onSuccess(ScrapConverter.toCreateDTOV2(createdScrap, scrapCount));
}

@CacheEvict(value = "findBriefingsV2", allEntries = true)
@Operation(summary = "05-02 Scrap๐Ÿ“ ์Šคํฌ๋žฉ ์ทจ์†Œ V2", description = "์Šคํฌ๋žฉ์„ ์ทจ์†Œํ•˜๋Š” API์ž…๋‹ˆ๋‹ค.")
@DeleteMapping("/scraps/briefings/{briefingId}/members/{memberId}")
public CommonResponse<ScrapResponse.DeleteDTOV2> deleteV2(
Expand Down

0 comments on commit c6ed247

Please sign in to comment.