-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/moabam/api/application/coupon/CouponCacheService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.moabam.api.application.coupon; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Optional; | ||
|
||
import org.springframework.cache.annotation.CacheConfig; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.moabam.api.domain.coupon.Coupon; | ||
import com.moabam.api.domain.coupon.repository.CouponRepository; | ||
import com.moabam.global.error.exception.NotFoundException; | ||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@CacheConfig(cacheNames = "coupons") | ||
public class CouponCacheService { | ||
|
||
private final CouponRepository couponRepository; | ||
|
||
@Cacheable(key = "#couponName + #now") | ||
public Coupon getByNameAndStartAt(String couponName, LocalDate now) { | ||
return couponRepository.findByNameAndStartAt(couponName, now) | ||
.orElseThrow(() -> new NotFoundException(ErrorMessage.INVALID_COUPON_PERIOD)); | ||
} | ||
|
||
@Cacheable(key = "#now") | ||
public Optional<Coupon> getByStartAt(LocalDate now) { | ||
return couponRepository.findByStartAt(now); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.moabam.global.config; | ||
|
||
import static org.springframework.data.redis.serializer.RedisSerializationContext.*; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalTime; | ||
|
||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; | ||
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.moabam.global.common.util.ClockHolder; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@EnableCaching | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class CacheConfig { | ||
|
||
private final ClockHolder clockHolder; | ||
|
||
@Bean | ||
public RedisCacheConfiguration redisCacheConfiguration() { | ||
var strSerializePair = SerializationPair.fromSerializer(new StringRedisSerializer()); | ||
var objSerializePair = SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapper())); | ||
|
||
return RedisCacheConfiguration.defaultCacheConfig() | ||
.entryTtl(getTtl()) | ||
.serializeKeysWith(strSerializePair) | ||
.serializeValuesWith(objSerializePair); | ||
} | ||
|
||
private Duration getTtl() { | ||
LocalTime now = clockHolder.time(); | ||
LocalTime end = clockHolder.endOfDay(); | ||
|
||
return Duration.between(now, end); | ||
} | ||
|
||
private ObjectMapper objectMapper() { | ||
PolymorphicTypeValidator polymorphicTypeValidator = BasicPolymorphicTypeValidator.builder() | ||
.allowIfSubType(Object.class) | ||
.build(); | ||
|
||
return JsonMapper.builder() | ||
.polymorphicTypeValidator(polymorphicTypeValidator) | ||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
.addModule(new JavaTimeModule()) | ||
.activateDefaultTyping(polymorphicTypeValidator, ObjectMapper.DefaultTyping.NON_FINAL) | ||
.build(); | ||
} | ||
} |