Commit 6f8c2c4a authored by 张辰's avatar 张辰

caffine cache config

parent 8e24d4f3
...@@ -2,16 +2,24 @@ package com.tanpu.community.config; ...@@ -2,16 +2,24 @@ package com.tanpu.community.config;
import com.tanpu.community.cache.RedisCache; import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.util.SpringUtils; import com.tanpu.community.util.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.Arrays; import java.util.Arrays;
@Configuration @Slf4j
@Component
@ConfigurationProperties(prefix = "cache")
public class CacheConfig { public class CacheConfig {
private String caffeineSpec;
@Autowired @Autowired
private SpringUtils springUtils; private SpringUtils springUtils;
...@@ -22,10 +30,16 @@ public class CacheConfig { ...@@ -22,10 +30,16 @@ public class CacheConfig {
@Bean @Bean
public CaffeineCacheManager caffeineCacheManager() { public CaffeineCacheManager caffeineCacheManager() {
log.info("initialize local cache with caffeineSpec:{}", caffeineSpec);
if (StringUtils.isBlank(caffeineSpec)) {
caffeineSpec = "maximumSize=10000,expireAfterWrite=300s";
}
CaffeineCacheManager cacheManager = new CaffeineCacheManager(); CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCacheNames(Arrays.asList("local")); cacheManager.setCacheNames(Arrays.asList("local"));
// todo 配置化 // todo 配置化
cacheManager.setCacheSpecification("maximumSize=1000,expireAfterWrite=30s"); cacheManager.setCacheSpecification(caffeineSpec);
return cacheManager; return cacheManager;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment