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;
} }

最新文章

  1. MapReduce的核心资料索引 [转]
  2. tomcat常用配置
  3. Lucene/Solr搜索引擎开发笔记 - 第1章 Solr安装与部署(Jetty篇)
  4. WPF 的datagrid 列名中没有显示下划线是怎么回事?
  5. Qt Error: dependent &#39;..\***&#39; does not exist.
  6. Android下实现tab页个人比较推崇的方法
  7. WiFi相关知识
  8. jQuery切换网页皮肤并保存到Cookie示例代码
  9. Bar 3D 和Pie 3D的统计图形
  10. spring &lt;context:annotation-config&gt; 跟 &lt;context:component-scan&gt;诠释及区别
  11. window.open 打开子窗口,关闭所有的子窗口
  12. 微信小程序区分点击,长按事件
  13. cf1088E Ehab and a component choosing problem (树形dp)
  14. Manthan, Codefest 18 (rated, Div. 1 + Div. 2) C D
  15. 为嵌入式mplayer移植添加ALSA音频驱动(全志V3s荔枝派zero)
  16. Long类型转json时前端js丢失精度解决方案
  17. css(层叠样式表)属性
  18. U盘修复技巧
  19. Insert插入语句中带有select语句
  20. ssi框架学习总结

热门文章

  1. 常用脚本--Kill所有连接到指定数据库上的回话
  2. jenkins任务构建失败重试插件Naginator Plugin
  3. 用.msi安装node时安装失败,出现rolling back action(转载)
  4. 记开发个人图书收藏清单小程序开发(六)Web开发
  5. css细节复习笔记——浮动
  6. Help Jimmy(动态规划)
  7. javaweb从mysql中获取数据验证用户名密码成功跳转,失败重新验证
  8. DataList用法总结
  9. javascript获取wx.config内部字段解决微信分享
  10. 利用python 学习数据分析 (学习三)