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

使用ehCache作为本地缓存的更多相关文章

  1. redis订阅发布消息操作本地缓存

    Redis 本地缓存+远程缓存方案 使用纯java的ehcache作为本地缓存 Reids 作为远程分布式缓存 解决redis缓存压力过大,提高缓存速度,以及缓存性能. Redis和ehcache缓存 ...

  2. 【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)

    一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...

  3. Spring Boot2.0+Redis+Ehcache实现二级缓存

    EHCache 本地缓存 Redis 分布式缓存(可以共享) 一级 Redis 二级Ehcache    当redis挂了 有备胎 反之: 先走本地,本地没有再走网络  尽量少走Redis  效率会高 ...

  4. 实现 Java 本地缓存,该从这几点开始

    缓存,我相信大家对它一定不陌生,在项目中,缓存肯定是必不可少的.市面上有非常多的缓存工具,比如 Redis.Guava Cache 或者 EHcache.对于这些工具,我想大家肯定都非常熟悉,所以今天 ...

  5. java中的本地缓存

    java中的本地缓存,工作后陆续用到,一直想写,一直无从下手,最近又涉及到这方面的问题了,梳理了一下.自己构造单例.guava.ehcache基本上涵盖了目前的大多数行为了.   为什么要有本地缓存? ...

  6. Spring集成GuavaCache实现本地缓存

    Spring集成GuavaCache实现本地缓存: 一.SimpleCacheManager集成GuavaCache 1 package com.bwdz.sp.comm.util.test; 2 3 ...

  7. Java高性能本地缓存框架Caffeine

    一.序言 Caffeine是一个进程内部缓存框架,使用了Java 8最新的[StampedLock]乐观锁技术,极大提高缓存并发吞吐量,一个高性能的 Java 缓存库,被称为最快缓存. 二.缓存简介 ...

  8. HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)

    1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息.   但是c ...

  9. Hibernate+EhCache配置二级缓存

    步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...

随机推荐

  1. Objective-C 学习笔记(三) Numbers/数字

    Objective-C Numbers/数字 Objective-C中提供了一系列的NSNumber和重要的工作方法: + (NSNumber *)numberWithBool:(BOOL)value ...

  2. append2 给append 添加回调方法

    $.fn.append2 = function(html, callback) { var originalHtmlLength = $("body").html().length ...

  3. 一个hql语句

    在hql语句里面,in的使用方法比较特别. from DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' ) in后面是一个list,我写的 ...

  4. SSH案例--入门级

    1.项目功能展示 (1)注册 (2)修改地址与级别信息,点击修改   (3)再添加一位成员,进行删除 点击第二行的删除 (4)登录模块测试 输入数据库中没有的信息: 输入数据库中存在的信息: 2. W ...

  5. Selenium窗口切换-----Selenium快速入门(六)

    有时候,我们打开多个窗口,进行多窗口操作,那么窗口间该如何切换呢? 切换的方法有两个,一个是通过窗口标题来验证,另一个是通过窗口特定的内容来验证,这两个方法都要求得到的标题或内容是唯一的. 用到的相关 ...

  6. 自己从0开始学习Unity的笔记 VIII (C#中类继承练习 II)

    自己写了一个关于兵种的,因为一直在测试,到底面向对象是个什么玩意...然后就做了这个 namespace 兵种 { class Role //作为父类,构建一个普通角色属性用于继承 { protect ...

  7. NETCore调用AD域验证

    一.添加引用 System.DirectoryServices System.DirectoryServices.AccountManagement 二.验证代码 声明域 string domainN ...

  8. Android 异步网络图片加载

    ListView异步加载图片 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=161586 [Android分享] ListVie ...

  9. 使用ssm实现校验密码

    由于审题不清,在完成作业“servlet实现进行用户名和密码验证”中使用了jdbc连接数据库的方式实现,没用静态方式验证,故本次作业使用ssm实现 本次作业上传到百度网盘:链接:https://pan ...

  10. DP刷题

    http://blog.csdn.net/a1dark/article/details/17115137