Skip to content

Commit

Permalink
feat: cache config 구현 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
gahyuun committed Feb 12, 2025
1 parent ba4d1bc commit fb9f0c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ dependencies {

// Spring Security
implementation 'org.springframework.boot:spring-boot-starter-security'

// Caffeine Cache
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'com.github.ben-manes.caffeine:caffeine'
}

tasks.named('test') {
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/acon/server/global/auth/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.acon.server.global.auth;


import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfig {
@Value("${jwt.refresh-token-expire-time}")
private long REFRESH_TOKEN_EXPIRATION_TIME;

@Bean
public CacheManager cacheManager() {

// 최대 용량 설정을 따로 진행하지 않음. 메모리 부족 문제 주의 필요
Caffeine<Object, Object> caffeineBuilder = Caffeine.newBuilder()
.expireAfterWrite(REFRESH_TOKEN_EXPIRATION_TIME, TimeUnit.MILLISECONDS);

CaffeineCache refreshTokenCache = new CaffeineCache(
"refreshTokenCache",
caffeineBuilder.build()
);

SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Collections.singletonList(refreshTokenCache));
return cacheManager;
}
}

0 comments on commit fb9f0c3

Please sign in to comment.