package ${basePackage}.config; import net.sf.ehcache.CacheManager; import net.sf.ehcache.config.CacheConfiguration; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CacheConfig { @Bean(name = "tokenCacheManager") public EhCacheCacheManager tokenCacheManager() { EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager(); net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration(); configuration.setMaxBytesLocalHeap("2G"); configuration.setUpdateCheck(false);//设置不检查更新 CacheConfiguration cacheConfiguration = new CacheConfiguration(); cacheConfiguration.setTimeToLiveSeconds(60 * 60);//一个小时有效缓存时间 cacheConfiguration.setTimeToIdleSeconds(30 * 60);//30分钟不使用eternal为true会失效 configuration.addDefaultCache(cacheConfiguration); CacheManager cacheManager = CacheManager.create(configuration); ehCacheCacheManager.setCacheManager(cacheManager); return ehCacheCacheManager; } }