com.github.bootfastconfig.cache.CachingConfigurer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of boot-fast-config-cache Show documentation
Show all versions of boot-fast-config-cache Show documentation
Parent pom providing dependency and plugin management for applications
built with Maven
The newest version!
package com.github.bootfastconfig.cache;
import com.github.bootfastconfig.cache.properties.CacheConfigProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleCacheErrorHandler;
import org.springframework.context.annotation.Bean;
import java.util.Map;
/**
* @author mister
*/
public class CachingConfigurer extends CachingConfigurerSupport {
@Autowired(required = false)
private Map cacheManagers;
@Autowired
private CacheConfigProperties propertiesCacheConfig;
/**
* 重写这个方法,目的是用以提供默认的cacheManager
*
* @return
* @author Stephen.Shi
*/
@Override
public CacheManager cacheManager() {
if (cacheManagers == null) {
return null;
}
for (String key : propertiesCacheConfig.getOrders()) {
CacheManager cacheManager = cacheManagers.get(key);
if (cacheManager != null) {
return cacheManager;
}
}
return null;
}
/**
* 如果cache出错, 我们会记录在日志里,方便排查,比如反序列化异常
*/
@Override
public CacheErrorHandler errorHandler() {
return new LoggingCacheErrorHandler();
}
/**
* 在使用@Cacheable时,如果不指定key,则使用找个默认的key生成器生成的key
*/
@Override
@Bean
public KeyGenerator keyGenerator() {
return (t, m, p) -> {
StringBuilder sb = new StringBuilder();
sb.append(t.getClass().getName());
sb.append(m.getName());
for (Object obj : p) {
sb.append(obj.toString());
}
return sb.toString();
};
}
/* non-public */ static class LoggingCacheErrorHandler extends SimpleCacheErrorHandler {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
logger.error(String.format("cacheName:%s,cacheKey:%s",
cache == null ? "unknown" : cache.getName(), key), exception);
super.handleCacheGetError(exception, cache, key);
}
@Override
public void handleCachePutError(RuntimeException exception, Cache cache, Object key,
Object value) {
logger.error(String.format("cacheName:%s,cacheKey:%s",
cache == null ? "unknown" : cache.getName(), key), exception);
super.handleCachePutError(exception, cache, key, value);
}
@Override
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
logger.error(String.format("cacheName:%s,cacheKey:%s",
cache == null ? "unknown" : cache.getName(), key), exception);
super.handleCacheEvictError(exception, cache, key);
}
@Override
public void handleCacheClearError(RuntimeException exception, Cache cache) {
logger.error(String.format("cacheName:%s", cache == null ? "unknown" : cache.getName()),
exception);
super.handleCacheClearError(exception, cache);
}
}
}