diff --git a/api/src/main/resources/application.yml b/api/src/main/resources/application.yml index d74c444c..f46eadd6 100644 --- a/api/src/main/resources/application.yml +++ b/api/src/main/resources/application.yml @@ -1,3 +1,6 @@ spring: profiles: active: local + +application: + type: api diff --git a/batch/src/main/resources/application.yml b/batch/src/main/resources/application.yml index d74c444c..d7c3ac90 100644 --- a/batch/src/main/resources/application.yml +++ b/batch/src/main/resources/application.yml @@ -1,3 +1,6 @@ spring: profiles: active: local + +application: + type: batch diff --git a/core/src/main/java/moa/client/wincube/auth/WincubeAuthClient.java b/core/src/main/java/moa/client/wincube/auth/WincubeAuthClient.java index 2d3ee26c..2969c289 100644 --- a/core/src/main/java/moa/client/wincube/auth/WincubeAuthClient.java +++ b/core/src/main/java/moa/client/wincube/auth/WincubeAuthClient.java @@ -2,6 +2,8 @@ import static moa.client.exception.ExternalApiExceptionType.EXTERNAL_API_EXCEPTION; import static moa.client.wincube.auth.Aes256Iv.generateIv; +import static moa.global.config.cache.CacheConfig.WINCUBE_ACCESS_TOKEN_CACHE_MANAGER_NAME; +import static moa.global.config.cache.CacheConfig.WINCUBE_ACCESS_TOKEN_CACHE_NAME; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -16,6 +18,7 @@ import moa.client.wincube.dto.WincubeIssueAuthTokenResponse; import moa.client.wincube.dto.WincubeTokenSignature; import moa.global.jwt.JwtService; +import org.springframework.cache.annotation.Cacheable; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @@ -34,7 +37,10 @@ public class WincubeAuthClient { private final Rsa rsa; private final JwtService jwtService; - // TODO 캐시 + @Cacheable( + cacheNames = WINCUBE_ACCESS_TOKEN_CACHE_NAME, + cacheManager = WINCUBE_ACCESS_TOKEN_CACHE_MANAGER_NAME + ) public String getAuthToken() { String aesIv = generateIv(AES_IV_BYTE); String codeId = getAuthCode(aesIv); diff --git a/core/src/main/java/moa/global/config/cache/CacheConfig.java b/core/src/main/java/moa/global/config/cache/CacheConfig.java new file mode 100644 index 00000000..2a03c3b1 --- /dev/null +++ b/core/src/main/java/moa/global/config/cache/CacheConfig.java @@ -0,0 +1,47 @@ +package moa.global.config.cache; + +import static org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair; + +import java.time.Duration; +import lombok.RequiredArgsConstructor; +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.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.StringRedisSerializer; + +@EnableCaching +@Configuration +@RequiredArgsConstructor +public class CacheConfig { + + public static final String WINCUBE_ACCESS_TOKEN_CACHE_NAME = "wincubeAccessToken"; + public static final String WINCUBE_ACCESS_TOKEN_CACHE_MANAGER_NAME = "wincubeAccessTokenCacheManager"; + + private static final String API = "api"; + + private final RedisConnectionFactory redisConnectionFactory; + + @Value("${application.type:#{null}}") + private String applicationType; + + @Bean + public CacheManager wincubeAccessTokenCacheManager() { + if (!API.equals(applicationType)) { + return new NoCacheManager(); + } + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() + .serializeKeysWith(SerializationPair.fromSerializer(new StringRedisSerializer())) + .serializeValuesWith(SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) + .entryTtl(Duration.ofHours(23)); // 캐시 저장 시간 23시간 설정, 윈큐브 토큰 지속시간이 24시간임 + return RedisCacheManager.RedisCacheManagerBuilder + .fromConnectionFactory(redisConnectionFactory) + .cacheDefaults(redisCacheConfiguration) + .build(); + } +} diff --git a/core/src/main/java/moa/global/config/cache/NoCacheManager.java b/core/src/main/java/moa/global/config/cache/NoCacheManager.java new file mode 100644 index 00000000..a8063c61 --- /dev/null +++ b/core/src/main/java/moa/global/config/cache/NoCacheManager.java @@ -0,0 +1,22 @@ +package moa.global.config.cache; + +import java.util.Collection; +import java.util.Collections; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCache; + +public class NoCacheManager implements CacheManager { + + private final Cache noCache = new ConcurrentMapCache("nocache"); + + public Cache getCache(String name) { + noCache.clear(); + return noCache; + } + + @Override + public Collection getCacheNames() { + return Collections.emptyList(); + } +}