package nd.sdp.basic.config; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; /** * 本地缓存配置,默认为ehcache */ @Configuration @EnableCaching(proxyTargetClass = true) public class LocalCacheConfig extends CachingConfigurerSupport { @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() { return getEhCacheManagerFactoryBean(); } /** * 获得缓存管理器。默认的为EhCacheCacheManager */ protected EhCacheManagerFactoryBean getEhCacheManagerFactoryBean() { EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean(); ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache/ehcache.xml")); return ehCacheManagerFactoryBean; } @Bean public CacheManager cacheManager() { return getCacheManager(); } protected CacheManager getCacheManager() { EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager(); ehCacheCacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject()); return ehCacheCacheManager; } }
pom:
1 <dependency> 2 <groupId>net.sf.ehcache</groupId> 3 <artifactId>ehcache-core</artifactId> 4 <version>2.6.8</version> 5 <exclusions> 6 <exclusion> 7 <groupId>commons-logging</groupId> 8 <artifactId>commons-logging</artifactId> 9 </exclusion> 10 </exclusions> 11 </dependency>
ehcache.xml
1 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" 3 monitoring="autodetect" 4 dynamicConfig="true"> 5 6 <diskStore path="java.io.tmpdir"/> 7 8 <!--项目列表缓存,时间为1天--> 9 <cache name="projectList" timeToLiveSeconds="3600" 10 maxElementsInMemory="500" eternal="false" overflowToDisk="false" 11 maxElementsOnDisk="1000" diskPersistent="false" 12 memoryStoreEvictionPolicy="LRU"/> 13 14 15 16 </ehcache> 17 18 19 <!-- 20 name : 缓存器名称 21 maxElementsInMemory : 内存中缓存元素的最大数目 22 maxElementsOnDisk : 磁盘中缓存元素的最大数目 23 eternal : 缓存是否会过期,如果为 true 则忽略timeToIdleSeconds 和 timeToLiveSeconds 24 overflowToDisk : 内存中缓存已满时是否缓存到磁盘,如果为 false 则忽略 25 maxElementsOnDisk timeToIdleSeconds : 缓存元素的最大闲置时间(秒),这段时间内如果不访问该元素则缓存失效 26 timeToLiveSeconds : 缓存元素的最大生存时间(秒),超过这段时间则强制缓存失效 27 memoryStoreEvictionPolicy : 使用 LFU 算法清除缓存 28 -->
使用:
1 package nd.sdp.auditingtools.systems.service; 2 3 import nd.sdp.basic.utils.HttpUtil; 4 import org.dom4j.Document; 5 import org.dom4j.DocumentException; 6 import org.dom4j.DocumentHelper; 7 import org.dom4j.Element; 8 import org.springframework.beans.factory.annotation.Value; 9 import org.springframework.cache.annotation.Cacheable; 10 import org.springframework.context.annotation.PropertySource; 11 import org.springframework.stereotype.Service; 12 13 import java.util.*; 14 15 /** 16 * 17 */ 18 @Service 19 @PropertySource("classpath:app.properties") 20 public class ProjectService { 21 22 @Value("${level_one_projects_url}") 23 String levelOneProjectsUrl; 24 25 @Value("${level_two_projects_url}") 26 String levelTwoProjectsUrl; 27 28 29 30 @Cacheable(value = "projectList",key = "'levelOneProjects_'+#userId") 31 public List<Map<String, String>> getLevelOneProjects(String userId) throws Exception { 32 String result = HttpUtil.get(levelOneProjectsUrl.replace("{code}", userId)); 33 Document document = DocumentHelper.parseText(result); 34 return parseDocument(document); 35 36 } 37 38 @Cacheable(value = "projectList",key = "'levelTwoProjects_'+#code") 39 public List<Map<String, String>> getLevelTwoProjects(String code) throws Exception { 40 String result = HttpUtil.get(levelTwoProjectsUrl.replace("{code}", code)); 41 Document document = DocumentHelper.parseText(result); 42 return parseDocument(document); 43 } 44 45 46 private List<Map<String, String>> parseDocument(Document document) throws DocumentException { 47 Element root = document.getRootElement(); 48 List<Map<String, String>> list = new ArrayList<>(); 49 for (Iterator i = root.elementIterator(); i.hasNext(); ) { 50 Element element = (Element) i.next(); 51 Map<String, String> map = new HashMap<>(); 52 map.put("key", element.getXPathResult(1).getStringValue()); 53 map.put("value", element.getXPathResult(3).getStringValue()); 54 list.add(map); 55 } 56 return list; 57 } 58 59 }