使用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>下配 ...
随机推荐
- 遇到了IE10不能登录的问题,很早就有解决方案了
1..net 2.0 的程序,请打开项目,打开vs开发环境的工具菜单下的 Package Manager Console ,中文名:程序包管理控制台,在打开的控制台中输入如下命令:Install-P ...
- 三部曲搭建本地nuget服务器(图文版)
下载Demo: 1.新建web的空项目 2.引入nuget包 3.修改配置文件config(可以默认) 运行效果:
- C#语言各个版本特性(一)
一.c#版本中添加的功能: C#2.0 泛型 部分类型 匿名方法 迭代器 可空类型 Getter / setter单独可访问性 方法组转换(代表) Co- and Contra-variance fo ...
- EF框架下的双表查询
最近想使用ef做一些开发但是遇到了一些小问题就是如何实现多表的查询然后经过查资料终于找出了结果 我们知道ef中表的关系是一对一 一对多 多对多 接下来就讲一下一对一的关系下的栗子 先编写两个表 第 ...
- 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 ...
- css--一些基本属性
关于css各标签的属性: w3cschool一应俱全 设置固定的图片: body { background-image: url(bgimage.gif); background-attachment ...
- ClamAV学习【3】——scanmanager函数浏览
吃饱饭继续浏览Manager.c的scanmanager函数,这个函数的功能吧,暂时理解如下. 接收一个命令行参数(经过处理的optstruct结构指针). 然后根据选项判断文件类型种类,还有一些扫描 ...
- P4016 负载平衡问题 网络流
P4016 负载平衡问题 题目描述 GG 公司有 nn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 nn个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运 ...
- Mybatis 的输入参数学习
mybatis 的输入参数: 指得就是parameterType的参数 这个参数就代表的sql语句中的输入参数 sql语句中的参数使用 有两种方式 : 使用#{} 获取: 8个基本数据类型 + S ...
- verify验证插件的详解
使用此验证插件,我们只需要新建一个实例对象,同时传入一个json对象就行了,这里我们只传入了两个属性:checkItem和callback.下面的代码解析,我们就按照这个例子来. var verify ...