Spring Boot 整合 Ehcache
 
修改 pom 文件
  1. <!-- Spring Boot 缓存支持启动器 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-cache</artifactId>
  5. </dependency>
  6. <!-- Ehcache 坐标 -->
  7. <dependency>
  8. <groupId>net.sf.ehcache</groupId>
  9. <artifactId>ehcache</artifactId>
  10. </dependency>
创建 Ehcache 的配置文件
 
文件名:ehcache.xml
位置:src/main/resources/ehcache.xml
 
  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  2. <diskStore path="java.io.tmpdir"/> <!--defaultCache:echcache 的默认缓存策略 -->
  3. <defaultCache maxElementsInMemory="10000"
  4. eternal="false"
  5. timeToIdleSeconds="120"
  6. timeToLiveSeconds="120"
  7. maxElementsOnDisk="10000000"
  8. diskExpiryThreadIntervalSeconds="120"
  9. memoryStoreEvictionPolicy="LRU">
  10. <persistence strategy="localTempSwap"/>
  11. </defaultCache> <!-- 自定义缓存策略 -->
  12. <cache name="users"
  13. maxElementsInMemory="10000"
  14. eternal="false"
  15. timeToIdleSeconds="120"
  16. timeToLiveSeconds="120"
  17. maxElementsOnDisk="10000000"
  18. diskExpiryThreadIntervalSeconds="120"
  19. memoryStoreEvictionPolicy="LRU">
  20. <persistence strategy="localTempSwap"/>
  21. </cache>
  22. </ehcache>
修改 application.properties 文件
 
  1. #项目端口配置
  2. server.port=8080
  3. server.address=0.0.0.0
  4. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  5. spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF8&useSSL=false&serverTimezone=UTC
  6. spring.datasource.username=root
  7. spring.datasource.password=root
  8. spring.datasouce.type=com.alibaba.druid.pool.DruidDataSource
  9. spring.jpa.hibernate.ddl-auto=update
  10. spring.jpa.show-sql=true
  11. spring.devtools.restart.enabled=true
  12. spring.cache.ehcache.config=classpath:ehcache.xml
 
修改启动类
  1. package com.bjsxt;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cache.annotation.EnableCaching;
  5. @SpringBootApplication
  6. @EnableCaching//启用缓存机制
  7. public class BootDataApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(BootDataApplication.class, args);
  10. }
  11. }
 
 
 
创建业务层
  1. package com.bjsxt.service.impl;
  2. import com.bjsxt.dao.UserDao;
  3. import com.bjsxt.pojo.Users;
  4. import com.bjsxt.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.cache.annotation.CacheEvict;
  7. import org.springframework.cache.annotation.Cacheable;
  8. import org.springframework.data.domain.Page;
  9. import org.springframework.data.domain.Pageable;
  10. import org.springframework.stereotype.Service;
  11. import java.util.List;
  12. @Service
  13. public class UserServiceImpl implements UserService {
  14. @Autowired
  15. UserDao ud;
  16. @Override
  17. @CacheEvict(value = "users",allEntries = true)//清空缓存
  18. public void addUser(Users users) {
  19. ud.save(users);
  20. }
  21. @Override
  22. @Cacheable(value = "users")//配置缓存,查找缓存文件
  23. public List<Users> findall() {
  24. List<Users> usersList = ud.findAll();
  25. return usersList;
  26. }
  27. @Override
  28. @Cacheable(value = "users",key = "#pageable.pageSize")//配置缓存,配置缓存的key
  29. public Page<Users> findUserByPage(Pageable pageable) {
  30. Page<Users> usersPage = ud.findAll(pageable);
  31. return usersPage;
  32. }
  33. }
 
 
修改实体类 Users
需要实现序列化接口
  1. package com.bjsxt.pojo;
  2. import lombok.Data;
  3. import javax.persistence.*;
  4. import java.io.Serializable;
  5. @Entity
  6. @Table(name = "users")
  7. @Data
  8. public class Users implements Serializable {
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. @Column(name = "userid")
  12. private int userid;
  13. @Column(name = "username")
  14. private String username;
  15. @Column(name = "userage")
  16. private int userage;
  17. public Users(){}
  18. public Users(String username, int userage) {
  19. this.username = username;
  20. this.userage = userage;
  21. }
  22. }
 
 
 
 
测试
 
  1. package com.bjsxt.test;
  2. import com.bjsxt.BootDataApplication;
  3. import com.bjsxt.dao.UserDao;
  4. import com.bjsxt.pojo.Users;
  5. import com.bjsxt.service.UserService;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.data.domain.Page;
  11. import org.springframework.data.domain.PageRequest;
  12. import org.springframework.data.domain.Pageable;
  13. import org.springframework.data.jpa.domain.Specification;
  14. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  15. import javax.persistence.criteria.CriteriaBuilder;
  16. import javax.persistence.criteria.CriteriaQuery;
  17. import javax.persistence.criteria.Predicate;
  18. import javax.persistence.criteria.Root;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. @RunWith(SpringJUnit4ClassRunner.class)
  22. @SpringBootTest(classes = BootDataApplication.class)
  23. public class UserTest {
  24. @Autowired
  25. UserService us;
  26. @Autowired
  27. UserDao ud;
  28. /**
  29. * 添加用户
  30. */
  31. @Test
  32. public void TestAddUser(){
  33. Users users=new Users();
  34. users.setUsername("杨彪");
  35. users.setUserage(27);
  36. us.addUser(users);
  37. }
  38. @Test
  39. public void findByNameAndAge(){
  40. Specification<Users> spec=new Specification<Users>() {
  41. @Override
  42. public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
  43. /*List<Predicate> predicates=new ArrayList<>();
  44. predicates.add(criteriaBuilder.equal(root.get("username"),"asd"));
  45. predicates.add(criteriaBuilder.equal(root.get("userage"),"123"));
  46. Predicate[] arr=new Predicate[predicates.size()];
  47. return criteriaBuilder.and(predicates.toArray(arr));*/
  48. return criteriaBuilder.or(criteriaBuilder.equal(root.get("username"),"asd"),criteriaBuilder.equal(root.get("userage"),"22"));
  49. }
  50. };
  51. List<Users> users = ud.findAll(spec);
  52. for (Users user : users) {
  53. System.out.println(user);
  54. }
  55. }
  56. /**
  57. * 测试使用缓存查询所有,第二次不会打印sql语句
  58. */
  59. @Test
  60. public void TestFindAll(){
  61. System.out.println("第一次查询:");
  62. List<Users> users = us.findall();
  63. for (Users user : users) {
  64. System.out.println("First:"+user);
  65. }
  66. Users use=new Users();
  67. use.setUsername("杨彪3");
  68. use.setUserage(22);
  69. us.addUser(use);
  70. System.out.println("\n第二次查询:");
  71. List<Users> u = us.findall();
  72. for (Users user : u) {
  73. System.out.println("Second:"+user);
  74. }
  75. }
  76. @Test
  77. public void findUserByPage(){
  78. Pageable pageable=new PageRequest(0,2);
  79. System.out.println("第一次查询:");
  80. Page<Users> userByPage = us.findUserByPage(pageable);
  81. long totalElements = userByPage.getTotalElements();
  82. System.out.println("First总条数:"+totalElements);
  83. System.out.println("\n第二次查询:");
  84. Page<Users> userByPage2 = us.findUserByPage(pageable);
  85. long totalElements2 = userByPage2.getTotalElements();
  86. System.out.println("Second总条数:"+totalElements2);
  87. System.out.println("\n第三次查询:");
  88. pageable=new PageRequest(1,2);
  89. Page<Users> userByPage3 = us.findUserByPage(pageable);
  90. long totalElements3 = userByPage3.getTotalElements();
  91. System.out.println("Third总条数:"+totalElements3);
  92. }
  93. }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Spring Boot缓存Ehcache的更多相关文章

  1. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  2. Spring Boot集成EHCache实现缓存机制

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  3. 另一种缓存,Spring Boot 整合 Ehcache

    用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...

  4. Spring Boot 集成 Ehcache 缓存,三步搞定!

    作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...

  5. spring boot 使用Ehcache

    1-引入maven依赖: 2-增加ehcache.xml 3-bootstrap.yml配置ehcache.xml的路径 4-启动类加注解@EnableCaching 5-使用处加注解@Cacheab ...

  6. Spring Boot2 系列教程(三十)Spring Boot 整合 Ehcache

    用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...

  7. 3步轻松搞定Spring Boot缓存

    作者:谭朝红 前言 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能.在Spring Boot应用程序中,我们可以通过Spring Caching来快速 ...

  8. Spring Boot整合EhCache

    本文讲解Spring Boot与EhCache的整合. 1 EhCache简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvid ...

  9. Spring boot缓存初体验

    spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...

随机推荐

  1. css3的过渡和动画的属性介绍

    一.过渡 什么是过渡? 过渡是指:某元素的css属性值在一段时间内,平滑过渡到另外一个值,过渡主要观察的是过程和结果. 设置能够过渡的属性: 支持过渡的样式属性,颜色的属性,取值为数值,transfo ...

  2. jsp页面时间的转换js

    /**                            * 日期 转换为 Unix时间戳              * @param <string> 2014-01-01 20:2 ...

  3. pat 1084 Broken Keyboard(20 分)

    1084 Broken Keyboard(20 分) On a broken keyboard, some of the keys are worn out. So when you type som ...

  4. nyoj 36-最长公共子序列 (动态规划,DP, LCS)

    36-最长公共子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:18 submit:38 题目描述: 咱们就不拐弯抹角了,如题,需要你做的就是写 ...

  5. nyoj 803-A/B Problem

    803-A/B Problem 内存限制:64MB 时间限制:1000ms 特判: No 通过数:2 提交数:4 难度:3 题目描述: 做了A+B Problem,A/B Problem不是什么问题了 ...

  6. nyoj 773-开方数 (pow)

    773-开方数 内存限制:64MB 时间限制:1000ms 特判: No 通过数:3 提交数:8 难度:3 题目描述: 现在给你两个数 n 和 p ,让你求出 p 的开 n 次方. 输入描述: 每组数 ...

  7. C++程序的耦合性设计

    声明:本文部分采用和参考<代码里的世界观-通往架构师之路>中内容,可以说是该书中耦合性一章的读后感,感谢该书的作者余叶老师的无私分享. 1.什么是耦合? 耦合其实就是程序之间的相关性. 程 ...

  8. php中 continue break exit return 的区别

    php 中的循环有 for foreache while do{} whlie这几种. 1.continue continue是用来在循环结构中,控制程序放弃本次循环continue: 之后的语句,并 ...

  9. 扛把子组20191017-5 alpha week 2/2 Scrum立会报告+燃尽图 04

    此作业要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/9801] 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 ...

  10. 30L,17L,13L容器分油,python递归,深度优先算法

    伪代码: 全部代码: a=[] b=[] def f(x,y,z): b.append([x,y,z]) if x==15 and y==15: print(x,y,z) i=0; for x in ...