前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache、RedisCache、ConcurrentMapCache等。

这一节我们来看看Spring Cache使用EhCache。

一、EhCache使用演示

EhCache是一个纯Java的进程内缓存框架,具有快速、精干等特点,Hibernate中的默认Cache就是使用的EhCache。

本章节示例是在Spring Boot集成Spring Cache的源码基础上进行改造。源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

使用EhCache作为缓存,我们先引入相关依赖。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <!--ehcache依赖-->
  6. <dependency>
  7. <groupId>net.sf.ehcache</groupId>
  8. <artifactId>ehcache</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-cache</artifactId>
  13. </dependency>

然后创建EhCache的配置文件ehcache.xml。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
  4. updateCheck="false">
  5. <!--
  6. 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
  7. path:指定在硬盘上存储对象的路径
  8. path可以配置的目录有:
  9. user.home(用户的家目录)
  10. user.dir(用户当前的工作目录)
  11. java.io.tmpdir(默认的临时目录)
  12. ehcache.disk.store.dir(ehcache的配置目录)
  13. 绝对路径(如:d:\\ehcache)
  14. 查看路径方法:String tmpDir = System.getProperty("java.io.tmpdir");
  15. -->
  16. <diskStore path="java.io.tmpdir" />
  17. <!--
  18. defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
  19. maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
  20. eternal:代表对象是否永不过期 (指定true则下面两项配置需为0无限期)
  21. timeToIdleSeconds:最大的发呆时间 /秒
  22. timeToLiveSeconds:最大的存活时间 /秒
  23. overflowToDisk:是否允许对象被写入到磁盘
  24. 说明:下列配置自缓存建立起600秒(10分钟)有效 。
  25. 在有效的600秒(10分钟)内,如果连续120秒(2分钟)未访问缓存,则缓存失效。
  26. 就算有访问,也只会存活600秒。
  27. -->
  28. <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600"
  29. timeToLiveSeconds="600" overflowToDisk="true" />
  30. <cache name="cache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
  31. timeToLiveSeconds="600" overflowToDisk="true" />
  32. </ehcache>

然后SpringBoot配置文件中,指明缓存类型并声明Ehcache配置文件的位置。

  1. server:
  2. port: 10900
  3. spring:
  4. profiles:
  5. active: dev
  6. cache:
  7. type: ehcache
  8. ehcache:
  9. config: classpath:/ehcache.xml

这样就可以开始使用Ehcache了,测试代码与Spring Boot集成Spring Cache一致。

SpringBoot启动类,@EnableCaching开启Spring Cache缓存功能。

  1. @EnableCaching
  2. @SpringBootApplication
  3. public class SpringbootApplication {
  4. public static void main(String[] args) {
  5. String tmpDir = System.getProperty("java.io.tmpdir");
  6. System.out.println("临时路径:" + tmpDir);
  7. SpringApplication.run(SpringbootApplication.class, args);
  8. }
  9. }

CacheApi接口调用类,方便调用进行测试。

  1. @RestController
  2. @RequestMapping("cache")
  3. public class CacheApi {
  4. @Autowired
  5. private CacheService cacheService;
  6. @GetMapping("get")
  7. public User get(@RequestParam int id){
  8. return cacheService.get(id);
  9. }
  10. @PostMapping("set")
  11. public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
  12. User u = new User(code, name);
  13. return cacheService.set(id, u);
  14. }
  15. @DeleteMapping("del")
  16. public void del(@RequestParam int id){
  17. cacheService.del(id);
  18. }
  19. }

CacheService缓存业务处理类,添加缓存,更新缓存和删除。

  1. @Slf4j
  2. @Service
  3. public class CacheService {
  4. private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
  5. {
  6. for (int i = 1; i < 100 ; i++) {
  7. User u = new User("code" + i, "name" + i);
  8. put(i, u);
  9. }
  10. }
  11. };
  12. // 获取数据
  13. @Cacheable(value = "cache", key = "'user:' + #id")
  14. public User get(int id){
  15. log.info("通过id{}查询获取", id);
  16. return dataMap.get(id);
  17. }
  18. // 更新数据
  19. @CachePut(value = "cache", key = "'user:' + #id")
  20. public User set(int id, User u){
  21. log.info("更新id{}数据", id);
  22. dataMap.put(id, u);
  23. return u;
  24. }
  25. //删除数据
  26. @CacheEvict(value = "cache", key = "'user:' + #id")
  27. public void del(int id){
  28. log.info("删除id{}数据", id);
  29. dataMap.remove(id);
  30. }
  31. }

源码地址:https://github.com/imyanger/springboot-project/tree/master/p22-springboot-cache-ehcache

SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache的更多相关文章

  1. SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  2. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  3. Spring boot集成spring session实现session共享

    最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...

  4. Spring Boot 集成spring security4

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  5. Spring Boot 集成 Spring Security 实现权限认证模块

    作者:王帅@CodeSheep   写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...

  6. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  7. SpringBoot系列:Spring Boot集成Spring Cache

    一.关于Spring Cache 缓存在现在的应用中越来越重要, Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework. ...

  8. Spring Boot 集成 Spring Security

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. Spring Boot 集成 Spring Security 入门案例教程

    前言 本文作为入门级的DEMO,完全按照官网实例演示: 项目目录结构 Maven 依赖 <parent> <groupId>org.springframework.boot&l ...

随机推荐

  1. Java 教程 (Java 对象和类)

    Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 本节我们重点研究对象和类的概念. 对象:对象是类的一个实例(对象不是找个女朋友 ...

  2. stm32 新建工程

    先新建六个文件夹. Consis:启动文件等 Fwlib:inc.src文件夹 Hardware:存放驱动 Mdk:工程文件 User:main函数等

  3. JS 延时函数

    function sleep(delay) { var start = (new Date()).getTime(); while((new Date()).getTime() - start < ...

  4. AppScan工具介绍与安装

    本文仅供个人参考学习,如做商业用途,请购买正版,谢谢! 介绍 AppScan是IBM公司出的一款Web应用安全测试工具,采用黑盒测试的方式,可以扫描常见的web应用安全漏洞.其工作原理,首先是根据起始 ...

  5. 深入理解Three.js中线条Line,LinLoop,LineSegments

    前言 在可视化开发中,无论是2d(canvas)开发还是3d开发,线条的绘制应用都是比较普遍的.比如绘制城市之间的迁徙图,运行轨迹图等.本文主要讲解的是Three.js中三种线条Line,LineLo ...

  6. mysql5.6.27压缩版安装配置指南【个人总结】

      1..下载准备压缩包   360云盘下载地址: https://yunpan.cn/cPKyugkUcDEmP  访问密码 375b   2.解压缩,将压缩版解压到D盘 D:\mysql-5.6. ...

  7. 在Docker中启动Cloudera

    写在前面 记录一下,一个简单的cloudera处理平台的构建过程和一些基本组件的使用 前置说明 需要一台安装有Docker的机器 docker常用命令: docker ps docker ps -a ...

  8. Spring boot 梳理 - 代码结构(Main类的位置)

    Spring boot 对代码结构无特殊要求,但有个套最佳实践的推荐 不要使用没有包名的类.没有包名时,@ComponentScan, @EntityScan, or @SpringBootAppli ...

  9. Linux端口占用情况查看

    1,查看8010端口是否被占用[root@cloud ~]# netstat -an|grep 8010tcp 0 0 0.0.0.0:8010 0.0.0.0:* LISTEN 2,查看8010是被 ...

  10. 痞子衡嵌入式:史上最强i.MX RT学习资源汇总(持续更新中...)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MX RT学习资源. 类别 资源 简介 官方汇总 i.MXRT产品主页 恩智浦官方i.MXRT产品主页,最权威的资料都在这里,参考手 ...