1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.tanpu.community.config;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.util.SpringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "cache")
public class CacheConfig {
private String caffeineSpec;
@Autowired
private SpringUtils springUtils;
@Bean
public RedisCache redisCache() {
return new RedisCache.Builder().cacheName("community2").build();
}
@Bean
public CaffeineCacheManager caffeineCacheManager() {
log.info("initialize local cache with caffeineSpec:{}", caffeineSpec);
if (StringUtils.isBlank(caffeineSpec)) {
caffeineSpec = "maximumSize=10000,expireAfterWrite=300s";
}
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCacheNames(Arrays.asList("local"));
// todo 配置化
cacheManager.setCacheSpecification(caffeineSpec);
return cacheManager;
}
}