From 9ee6efd73322ec7c68a25e3e8e01289c07754aeb Mon Sep 17 00:00:00 2001 From: hoelee Date: Mon, 16 Mar 2026 09:00:00 +0800 Subject: [PATCH] feat(java-cache): add CacheConfig() --- .../hoelee/platform/cache/CacheConfig.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 platform/java-cache/src/main/java/com/hoelee/platform/cache/CacheConfig.java diff --git a/platform/java-cache/src/main/java/com/hoelee/platform/cache/CacheConfig.java b/platform/java-cache/src/main/java/com/hoelee/platform/cache/CacheConfig.java new file mode 100644 index 00000000..f4aa5d68 --- /dev/null +++ b/platform/java-cache/src/main/java/com/hoelee/platform/cache/CacheConfig.java @@ -0,0 +1,29 @@ +package com.hoelee.platform.cache; + +import com.github.benmanes.caffeine.cache.Caffeine; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @version v2, 2024-10-29 10:05:00PM + * @author hoelee + * Learning note: a bounded local cache improves hot-read latency while its statistics make the benefit observable. + */ +@Configuration +@EnableCaching +public class CacheConfig { + + @Bean + CacheManager cacheManager(CacheProperties properties) { + CaffeineCacheManager cacheManager = new CaffeineCacheManager("posts"); + cacheManager.setCaffeine(Caffeine.newBuilder() + .recordStats() + .maximumSize(properties.maximumSize()) + .expireAfterWrite(properties.ttl())); + return cacheManager; + } +} +