티스토리 뷰

공부

[spring] caffein cache

승가비 2023. 9. 10. 16:31
728x90
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'com.github.ben-manes.caffeine:caffeine'
@EnableCaching
@Configuration
public class CacheConfig {
    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        List<CaffeineCache> caches = Arrays.stream(CacheType.values())
            .map(cache -> new CaffeineCache(
                cache.getKey(),
                Caffeine.newBuilder()
                    .expireAfterWrite(cache.getExpireAfterWrite(), TimeUnit.SECONDS)
                    .maximumSize(cache.getMaximumSize())
                    .recordStats()
                    .build()
            ))
            .collect(Collectors.toList());

        cacheManager.setCaches(caches);
        return cacheManager;
    }
}
@Getter
public enum CacheType {
    ASDF(ConstConfig.ASDF),
    QWER(ConstConfig.QWER, 1),
    ;

    private String key;
    private int expireAfterWrite;
    private int maximumSize;

    CacheType(String key) {
        this(key, ConstConfig.DEFAULT_TTL_SEC);
    }

    CacheType(String key, int expireAfterWrite) {
        this.key = key;
        this.expireAfterWrite = expireAfterWrite;
        this.maximumSize = ConstConfig.DEFAULT_MAX_SIZE;
    }

    public static class ConstConfig {
        public static final String ASDF = "asdf";
        public static final String QWER = "qwer";

        static final int DEFAULT_TTL_SEC = 30;
        static final int DEFAULT_MAX_SIZE = 10000;
    }
}

https://gngsn.tistory.com/170

 

Caffeine Cache, 어렵지 않게 사용하기 2

Caffeine Cache에 대해 알아보고 사용법에 대해 알아보는 것이 해당 포스팅의 목표입니다. 본 편은 이전 포스팅인 Caffeine Cache, 제대로 사용하기에 이은 두 번째 포스팅입니다. 전반적인 소개글인 이

gngsn.tistory.com

 

728x90
댓글