SpringBoot 整合Ehcache3
SpringBootLean 是对springboot学习与研究项目,是依据实际项目的形式对进行配置与处理,欢迎star与fork。
[oschina 地址]
http://git.oschina.net/cmlbeliever/SpringBootLearning
[github 地址]
https://github.com/cmlbeliever/SpringBootLearning
近期研究了下server端缓存处理。并整合到SpringBoot中。已提交到branch-ehcache3分支。
网上使用的大部分是ehcache2的版本号,groupId为net.sf.ehcache,升级到3以后groupId改成了org.ehcache,所以代码改变还是比較大的,依据官网上的博客地址
http://www.ehcache.org/blog/2016/05/18/ehcache3_jsr107_spring.html
依照官网的博客进行整合就可以。总结过程例如以下:
1、导入pom依赖
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.9</version>
</dependency>
2、导入ehcache配置文件
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3' xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>
<service>
<jsr107:defaults>
<jsr107:cache name="people" template="heap-cache" />
</jsr107:defaults>
</service>
<cache-template name="heap-cache">
<listeners>
<listener>
<class>com.cml.springboot.framework.cache3.EventLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>UPDATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>REMOVED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap>
</resources>
</cache-template>
</config>
3、加入log监听类
package com.cml.springboot.framework.cache3;
import org.ehcache.event.CacheEvent;
import org.ehcache.event.CacheEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author GGIB
*/
public class EventLogger implements CacheEventListener<Object, Object> {
private static final Logger LOGGER = LoggerFactory.getLogger(EventLogger.class);
@Override
public void onEvent(CacheEvent<?
extends Object, ? extends Object> event) {
LOGGER.info("Event: " + event.getType() + " Key: " + event.getKey() + " old value: " + event.getOldValue()
+ " new value: " + event.getNewValue());
}
}
4、加入cache配置类。这里加入cacheName为people
package com.cml.springboot.framework.cache3;
import java.util.concurrent.TimeUnit;
import javax.cache.CacheManager;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.Duration;
import javax.cache.expiry.TouchedExpiryPolicy;
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
@Component
public class Ehcache3Config implements JCacheManagerCustomizer {
private static final String NAME_CACHE = "people";
@Override
public void customize(CacheManager cacheManager) {
cacheManager.createCache(NAME_CACHE,
new MutableConfiguration<>()
.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 10)))
.setStoreByValue(true).setStatisticsEnabled(true));
}
}
依照上述步骤配置就可以,然后加入单元測试。能够从log上看出缓存是否使用到了。
单元測试类 com.cml.springboot.cache.Ehcache3Test
測试结果:
====================================================
2017-01-28 16:30:05.732 INFO 41240 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : FrameworkServlet '': initialization completed in 1120 ms
2017-01-28 16:30:06.213 INFO 41240 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2017-01-28 16:30:06.562 INFO 41240 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2017-01-28 16:30:06.563 INFO 41240 --- [ main] o.s.i.endpoint.EventDrivenConsumer : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2017-01-28 16:30:06.563 INFO 41240 --- [ main] o.s.i.channel.PublishSubscribeChannel : Channel 'application:-1.errorChannel' has 1 subscriber(s).
2017-01-28 16:30:06.563 INFO 41240 --- [ main] o.s.i.endpoint.EventDrivenConsumer : started _org.springframework.integration.errorLogger
2017-01-28 16:30:06.579 INFO 41240 --- [ main] com.cml.springboot.cache3.Ehcache3Test : Started Ehcache3Test in 7.565 seconds (JVM running for 8.36)
2017-01-28 16:30:06.886 INFO 41240 --- [ main] c.c.s.s.service.impl.UserServiceImpl : ====================read user from db=========
2017-01-28 16:30:06.938 INFO 41240 --- [ main] c.c.s.sample.controller.CacheController : read data token=C78CE23552BC46328959C8C0AE391886,user:User [username=null, password=null, token=C78CE23552BC46328959C8C0AE391886, newToken=null, userId=1, birthday=1987-02-27T00:00:00.000+08:00, nickName=小明22]
2017-01-28 16:30:06.938 INFO 41240 --- [hcache [null]-0] c.c.s.framework.cache3.EventLogger : Event: CREATED Key: C78CE23552BC46328959C8C0AE391886 old value: null new value: User [username=null, password=null, token=C78CE23552BC46328959C8C0AE391886, newToken=null, userId=1, birthday=1987-02-27T00:00:00.000+08:00, nickName=小明22]
==============================
{"code":1,"user":{"token":"C78CE23552BC46328959C8C0AE391886","userId":1,"birthday":"19870227000000","nickName":"小明22"}}
=====================read data second==========================
2017-01-28 16:30:07.022 INFO 41240 --- [ main] c.c.s.sample.controller.CacheController : read data token=C78CE23552BC46328959C8C0AE391886,user:User [username=null, password=null, token=C78CE23552BC46328959C8C0AE391886, newToken=null, userId=1, birthday=1987-02-27T00:00:00.000+08:00, nickName=小明22]
==============================
{"code":1,"user":{"token":"C78CE23552BC46328959C8C0AE391886","userId":1,"birthday":"19870227000000","nickName":"小明22"}}
2017-01-28 16:30:07.031 INFO 41240 --- [ Thread-2] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@17f62e33: startup date [Sat Jan 28 16:29:59 CST 2017]; root of context hierarchy
2017-01-28 16:30:07.035 INFO 41240 --- [ Thread-2] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0
2017-01-28 16:30:07.036 INFO 41240 --- [ Thread-2] o.s.i.endpoint.EventDrivenConsumer : Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2017-01-28 16:30:07.036 INFO 41240 --- [ Thread-2] o.s.i.channel.PublishSubscribeChannel : Channel 'application:-1.errorChannel' has 0 subscriber(s).
2017-01-28 16:30:07.037 INFO 41240 --- [ Thread-2] o.s.i.endpoint.EventDrivenConsumer : stopped _org.springframework.integration.errorLogger
2017-01-28 16:30:07.038 INFO 41240 --- [ Thread-2] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService 'taskScheduler'
2017-01-28 16:30:07.061 INFO 41240 --- [ Thread-2] org.ehcache.core.EhcacheManager : Cache 'people' removed from EhcacheManager.
注:project上分支branch-ehcache为ehcache2版本号的配置。
步骤4仅仅配置了内存缓存,至于文件缓存以及配置须要年后再研究,欢迎补充!
SpringBoot 整合Ehcache3的更多相关文章
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
- SpringBoot整合RabbitMQ-整合演示
本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...
随机推荐
- Android 开发笔记___SD卡基本操作__图片读取写入
package com.example.alimjan.hello_world.Utils; import android.graphics.Bitmap; import android.graphi ...
- lvs学习笔记
本人身为一个网工,最近一直在工作中学习linux的相关知识.前短时间通过自查资料学习了lvs的相关内容,摘录部分整理后和大家分享,内容较多,较琐碎,望见谅!!! LVS 从Linux内核版本2.6起, ...
- 详细图解window环境mongodb下载、安装、配置与使用
到官网下载最新版面mongodb安装包,(32位版本的已经取消了,只有64位的) 官网地址: https://www.mongodb.com/download-center#community 下载完 ...
- Linux下打造全方位立体监控系统
前言 本文主要介绍如何使用Grafana和Prometheus以及node_exporter对Linux服务器性能进行监控.下面两张图分别是两台服务器: 服务器A 服务器B 概述 Prometheus ...
- SqlServer 数据库附加问题:不是主数据库文件
一.前言 今天公司要切换数据库服务器,数据库文件大于2G,结果再附加到另一服务器的数据库里面,就产生了一个问题.如下: 标题:Microsoft SQL Server Management Studi ...
- php 常用 常量集合
DIRECTORY_SEPARATOR 常量 DIRECTORY_SEPARATOR 目录分割符
- 能自学成为WEB前端工程师吗?
自学是大家学习一门it技术的时候,都会首先考虑的一种学习方式,web前端开发学习也是一样,但是自学web前端也是让大家充满疑问的一种学习方 自学是大家学习一门it技术的时候,都会首先考虑的一种学习方式 ...
- 制作代码模板的 LaTex 模板
Tex 真的是一个用起来非常舒服的排版工具(对于排版要求高的人来说),去比赛前一天放弃了markdown转pdf来生成代码模板,现学Tex(其实美赛已经用过了:P). 推荐一个链接:TeX - Bea ...
- 【手记】让Fiddler抓取入站请求,或者叫用Fiddler做反向代理
注意:本文不涉及HTTPS的场景 最近在弄公众号开发,除了主动去调公众号接口,还存在公众号后台要反过来调你的情形,攻受转换一线间.对于回调的情况,想要知道对方是怎样来请求的很有必要.此前经常用Fidd ...
- Chapter 7:Linking
概述: 在linux上,从c源码到可执行文件主要需要经历translator(compiler.assembler)生成object file,再经由linker连接成executable objec ...