使用ehCache作为本地缓存

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:

         <dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.8</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

ehcache.xml

 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect"
dynamicConfig="true"> <diskStore path="java.io.tmpdir"/> <!--项目列表缓存,时间为1天-->
<cache name="projectList" timeToLiveSeconds="3600"
maxElementsInMemory="500" eternal="false" overflowToDisk="false"
maxElementsOnDisk="1000" diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/> </ehcache> <!--
name : 缓存器名称
maxElementsInMemory : 内存中缓存元素的最大数目
maxElementsOnDisk : 磁盘中缓存元素的最大数目
eternal : 缓存是否会过期,如果为 true 则忽略timeToIdleSeconds 和 timeToLiveSeconds
overflowToDisk : 内存中缓存已满时是否缓存到磁盘,如果为 false 则忽略
maxElementsOnDisk timeToIdleSeconds : 缓存元素的最大闲置时间(秒),这段时间内如果不访问该元素则缓存失效
timeToLiveSeconds : 缓存元素的最大生存时间(秒),超过这段时间则强制缓存失效
memoryStoreEvictionPolicy : 使用 LFU 算法清除缓存
-->

使用:

 package nd.sdp.auditingtools.systems.service;

 import nd.sdp.basic.utils.HttpUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service; import java.util.*; /**
*
*/
@Service
@PropertySource("classpath:app.properties")
public class ProjectService { @Value("${level_one_projects_url}")
String levelOneProjectsUrl; @Value("${level_two_projects_url}")
String levelTwoProjectsUrl; @Cacheable(value = "projectList",key = "'levelOneProjects_'+#userId")
public List<Map<String, String>> getLevelOneProjects(String userId) throws Exception {
String result = HttpUtil.get(levelOneProjectsUrl.replace("{code}", userId));
Document document = DocumentHelper.parseText(result);
return parseDocument(document); } @Cacheable(value = "projectList",key = "'levelTwoProjects_'+#code")
public List<Map<String, String>> getLevelTwoProjects(String code) throws Exception {
String result = HttpUtil.get(levelTwoProjectsUrl.replace("{code}", code));
Document document = DocumentHelper.parseText(result);
return parseDocument(document);
} private List<Map<String, String>> parseDocument(Document document) throws DocumentException {
Element root = document.getRootElement();
List<Map<String, String>> list = new ArrayList<>();
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
Map<String, String> map = new HashMap<>();
map.put("key", element.getXPathResult(1).getStringValue());
map.put("value", element.getXPathResult(3).getStringValue());
list.add(map);
}
return list;
} }
上一篇:ConcurrentHashMap原理分析


下一篇:ipad在非viewport 1:1下缩放问题