Skip to content

Commit

Permalink
[MOA-498] 윈큐브의 AccessToken 캐시하여 사용 (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
shin-mallang committed Mar 1, 2024
1 parent 1ac2306 commit 18f5b44
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
spring:
profiles:
active: local

application:
type: api
3 changes: 3 additions & 0 deletions batch/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
spring:
profiles:
active: local

application:
type: batch
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions core/src/main/java/moa/global/config/cache/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
22 changes: 22 additions & 0 deletions core/src/main/java/moa/global/config/cache/NoCacheManager.java
Original file line number Diff line number Diff line change
@@ -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<String> getCacheNames() {
return Collections.emptyList();
}
}

0 comments on commit 18f5b44

Please sign in to comment.