1. package nd.sdp.basic.config;
  2.  
  3. import org.springframework.cache.CacheManager;
  4. import org.springframework.cache.annotation.CachingConfigurerSupport;
  5. import org.springframework.cache.annotation.EnableCaching;
  6. import org.springframework.cache.ehcache.EhCacheCacheManager;
  7. import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.core.io.ClassPathResource;
  11.  
  12. /**
  13. * 本地缓存配置,默认为ehcache
  14. */
  15. @Configuration
  16. @EnableCaching(proxyTargetClass = true)
  17. public class LocalCacheConfig extends CachingConfigurerSupport {
  18.  
  19. @Bean
  20. public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
  21. return getEhCacheManagerFactoryBean();
  22. }
  23.  
  24. /**
  25. * 获得缓存管理器。默认的为EhCacheCacheManager
  26. */
  27. protected EhCacheManagerFactoryBean getEhCacheManagerFactoryBean() {
  28. EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
  29. ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache/ehcache.xml"));
  30. return ehCacheManagerFactoryBean;
  31. }
  32.  
  33. @Bean
  34. public CacheManager cacheManager() {
  35. return getCacheManager();
  36. }
  37.  
  38. protected CacheManager getCacheManager() {
  39. EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
  40. ehCacheCacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
  41. return ehCacheCacheManager;
  42. }
  43.  
  44. }

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. </ehcache>
  15.  
  16. <!--
  17. name : 缓存器名称
  18. maxElementsInMemory : 内存中缓存元素的最大数目
  19. maxElementsOnDisk : 磁盘中缓存元素的最大数目
  20. eternal : 缓存是否会过期,如果为 true 则忽略timeToIdleSeconds 和 timeToLiveSeconds
  21. overflowToDisk : 内存中缓存已满时是否缓存到磁盘,如果为 false 则忽略
  22. maxElementsOnDisk timeToIdleSeconds : 缓存元素的最大闲置时间(秒),这段时间内如果不访问该元素则缓存失效
  23. timeToLiveSeconds : 缓存元素的最大生存时间(秒),超过这段时间则强制缓存失效
  24. memoryStoreEvictionPolicy : 使用 LFU 算法清除缓存
  25. -->

使用:

  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. @Cacheable(value = "projectList",key = "'levelOneProjects_'+#userId")
  29. public List<Map<String, String>> getLevelOneProjects(String userId) throws Exception {
  30. String result = HttpUtil.get(levelOneProjectsUrl.replace("{code}", userId));
  31. Document document = DocumentHelper.parseText(result);
  32. return parseDocument(document);
  33.  
  34. }
  35.  
  36. @Cacheable(value = "projectList",key = "'levelTwoProjects_'+#code")
  37. public List<Map<String, String>> getLevelTwoProjects(String code) throws Exception {
  38. String result = HttpUtil.get(levelTwoProjectsUrl.replace("{code}", code));
  39. Document document = DocumentHelper.parseText(result);
  40. return parseDocument(document);
  41. }
  42.  
  43. private List<Map<String, String>> parseDocument(Document document) throws DocumentException {
  44. Element root = document.getRootElement();
  45. List<Map<String, String>> list = new ArrayList<>();
  46. for (Iterator i = root.elementIterator(); i.hasNext(); ) {
  47. Element element = (Element) i.next();
  48. Map<String, String> map = new HashMap<>();
  49. map.put("key", element.getXPathResult(1).getStringValue());
  50. map.put("value", element.getXPathResult(3).getStringValue());
  51. list.add(map);
  52. }
  53. return list;
  54. }
  55.  
  56. }

使用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. 遇到了IE10不能登录的问题,很早就有解决方案了

    1..net 2.0 的程序,请打开项目,打开vs开发环境的工具菜单下的  Package Manager Console ,中文名:程序包管理控制台,在打开的控制台中输入如下命令:Install-P ...

  2. 三部曲搭建本地nuget服务器(图文版)

    下载Demo: 1.新建web的空项目 2.引入nuget包 3.修改配置文件config(可以默认) 运行效果:

  3. C#语言各个版本特性(一)

    一.c#版本中添加的功能: C#2.0 泛型 部分类型 匿名方法 迭代器 可空类型 Getter / setter单独可访问性 方法组转换(代表) Co- and Contra-variance fo ...

  4. EF框架下的双表查询

    最近想使用ef做一些开发但是遇到了一些小问题就是如何实现多表的查询然后经过查资料终于找出了结果 我们知道ef中表的关系是一对一  一对多  多对多 接下来就讲一下一对一的关系下的栗子 先编写两个表 第 ...

  5. Let it crash philosophy for distributed systems

    This past weekend I read Joe Armstrong’s paper on the history of Erlang. Now, HOPL papers in general ...

  6. css--一些基本属性

    关于css各标签的属性: w3cschool一应俱全 设置固定的图片: body { background-image: url(bgimage.gif); background-attachment ...

  7. ClamAV学习【3】——scanmanager函数浏览

    吃饱饭继续浏览Manager.c的scanmanager函数,这个函数的功能吧,暂时理解如下. 接收一个命令行参数(经过处理的optstruct结构指针). 然后根据选项判断文件类型种类,还有一些扫描 ...

  8. P4016 负载平衡问题 网络流

    P4016 负载平衡问题 题目描述 GG 公司有 nn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 nn个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运 ...

  9. Mybatis 的输入参数学习

    mybatis 的输入参数: 指得就是parameterType的参数 这个参数就代表的sql语句中的输入参数 sql语句中的参数使用  有两种方式 : 使用#{} 获取:  8个基本数据类型 + S ...

  10. verify验证插件的详解

    使用此验证插件,我们只需要新建一个实例对象,同时传入一个json对象就行了,这里我们只传入了两个属性:checkItem和callback.下面的代码解析,我们就按照这个例子来. var verify ...