package ${basePackage}.config; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.config.CacheConfiguration; import net.sf.ehcache.config.DiskStoreConfiguration; 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 { public static final String TOKEN_CACHE = "tokenCache"; @Bean(name = TOKEN_CACHE) public EhCacheCacheManager getCacheManager() { net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration(); configuration.setMaxBytesLocalHeap("1G"); configuration.updateCheck(false); configuration.addDiskStore(new DiskStoreConfiguration().path("java.io.tmpdir")); CacheManager cacheManager = CacheManager.create(configuration); // 添加token缓存 cacheManager.addCache(buildTokenCache()); return new EhCacheCacheManager(cacheManager); } /** * 构建TokenCache * * @return 缓存 */ private Cache buildTokenCache() { CacheConfiguration config = new CacheConfiguration(); config.setMemoryStoreEvictionPolicy("LFU");//最少使用 config.setTimeToLiveSeconds(60 * 60);//最长有效时间 config.setTimeToIdleSeconds(60 * 60);//无访问最长有效时间 config.setName(TOKEN_CACHE); return new Cache(config); } }