使用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;
} }
使用ehCache作为本地缓存的更多相关文章
- redis订阅发布消息操作本地缓存
Redis 本地缓存+远程缓存方案 使用纯java的ehcache作为本地缓存 Reids 作为远程分布式缓存 解决redis缓存压力过大,提高缓存速度,以及缓存性能. Redis和ehcache缓存 ...
- 【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)
一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...
- Spring Boot2.0+Redis+Ehcache实现二级缓存
EHCache 本地缓存 Redis 分布式缓存(可以共享) 一级 Redis 二级Ehcache 当redis挂了 有备胎 反之: 先走本地,本地没有再走网络 尽量少走Redis 效率会高 ...
- 实现 Java 本地缓存,该从这几点开始
缓存,我相信大家对它一定不陌生,在项目中,缓存肯定是必不可少的.市面上有非常多的缓存工具,比如 Redis.Guava Cache 或者 EHcache.对于这些工具,我想大家肯定都非常熟悉,所以今天 ...
- java中的本地缓存
java中的本地缓存,工作后陆续用到,一直想写,一直无从下手,最近又涉及到这方面的问题了,梳理了一下.自己构造单例.guava.ehcache基本上涵盖了目前的大多数行为了. 为什么要有本地缓存? ...
- Spring集成GuavaCache实现本地缓存
Spring集成GuavaCache实现本地缓存: 一.SimpleCacheManager集成GuavaCache 1 package com.bwdz.sp.comm.util.test; 2 3 ...
- Java高性能本地缓存框架Caffeine
一.序言 Caffeine是一个进程内部缓存框架,使用了Java 8最新的[StampedLock]乐观锁技术,极大提高缓存并发吞吐量,一个高性能的 Java 缓存库,被称为最快缓存. 二.缓存简介 ...
- HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)
1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息. 但是c ...
- Hibernate+EhCache配置二级缓存
步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...
随机推荐
- logback 热修改
<configuration scan="true" scanPeriod="60 seconds" debug="false"> ...
- nancyfx 自定义路由module
在源码的Nancy.Demo.CustomModule项目示例中 查看UglifiedNancyModule.cs文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
- Linux FIO
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...
- DataTables使用总结
一.使用方法 1.引入JS文件 <script src="js/plugin/datatables/jquery.dataTables.min.js">< ...
- WP8.1StoreApp(WP8.1RT)---本地Toast
WP7/8中的Toast是不能在前台弹出的. WP8.1StoreApp可以利用Win8中的方式: private void Toast(string title,string content) { ...
- AndroidSDK下载
C:\Windows\System32\drivers\etc\hosts74.125.237.1 dl-ssl.google.com
- Java多线程编程 — 锁优化
阅读目录 一.尽量不要锁住方法 二.缩小同步代码块,只锁数据 三.锁中尽量不要再包含锁 四.将锁私有化,在内部管理锁 五.进行适当的锁分解 正文 并发环境下进行编程时,需要使用锁机制来同步多线程间 ...
- 使HTML页面上获取到的文本保留空格和换行符等格式
<pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre>的内容自动换行的问题(兼容多个浏览器): ...
- Spring 事务相关点整理
Spring和事务的关系 关系型数据库.某些消息队列等产品或中间件称为事务性资源,因为它们本身支持事务,也能够处理事务. Spring很显然不是事务性资源,但是它可以管理事务性资源,所以Spring和 ...
- 考试题T3
题意分析 这题一看没有什么思路 幸好我们机房的红太阳\(ghj1222\)切了这道题 首先我们考虑风跑一个来回之后人怎么样 就是跑了一个区间 也就是风跑了若干个来回之后 人跑了若干个区间 所以我们考虑 ...