首先引入maven包:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

  然后在启动类上添加注解:@EnableCaching//开启缓存

配置application.properties

#server
server.servlet.context-path=/demo
server.port=8080
server.tomcat.uri-encoding=utf-8 #设置freemarker模板后缀
spring.freemarker.suffix=.html
#修改模板默认文件夹
spring.freemarker.template-loader-path=classpath:/web #datasource tomcat-jdbc
spring.datasource.url=jdbc:mysql://localhost:3306/boot?characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #jpa
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true #ehcache
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:/config/ehcache.xml

  配置ehcache.xml:

       <!--
设定具体的命名缓存的数据过期策略。每个命名缓存代表一个缓存区域
缓存区域(region):一个具有名称的缓存块,可以给每一个缓存块设置不同的缓存策略。
如果没有设置任何的缓存区域,则所有被缓存的对象,都将使用默认的缓存策略。即:<defaultCache.../>
Hibernate 在不同的缓存区域保存不同的类/集合。
对于类而言,区域的名称是类名。如:com.atguigu.domain.Customer
对于集合而言,区域的名称是类名加属性名。如com.atguigu.domain.Customer.orders
-->
<!--
name: 设置缓存的名字,它的取值为类的全限定名或类的集合的名字
maxElementsInMemory: 设置基于内存的缓存中可存放的对象最大数目
eternal: 设置对象是否为永久的, true表示永不过期, 此时将忽略timeToIdleSeconds 和 timeToLiveSeconds属性; 默认值是false
  timeToIdleSeconds:设置对象空闲最长时间,以秒为单位, 超过这个时间,对象过期。当对象过期时,EHCache会把它从缓存中清除。如果此值为0,表示对象可以无限期地处于空闲状态。
timeToLiveSeconds:设置对象生存最长时间,超过这个时间,对象过期。如果此值为0,表示对象可以无限期地存在于缓存中. 该属性值必须大于或等于 timeToIdleSeconds 属性值
overflowToDisk:设置基于内存的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中
-->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!--
指定一个目录:当 EHCache 把数据写到硬盘上时, 将把数据写到这个目录下.
-->
<diskStore path="d:\\cache"/> <!--设置缓存的默认数据过期策略 -->
<!-- <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> --> <cache name="Student" eternal="false" maxElementsInMemory="10000" timeToIdleSeconds="200" overflowToDisk="true" timeToLiveSeconds="200"/> </ehcache>

  这样,Ehcache已经整合到springboot中了。

简单测试缓存:

package com.example.demo.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo.entity.Student;

public interface StudentDao extends JpaRepository<Student, Integer> {
Student findStudentById(Integer id);
}

  

package com.example.demo.service;

import com.example.demo.entity.Student;

public interface StudentService {
public Student getStudentById(Integer id) ;
}

  

package com.example.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import com.example.demo.dao.StudentDao;
import com.example.demo.entity.Student;
import com.example.demo.service.StudentService; @Service
public class StudentServiceImpl implements StudentService { @Autowired
private StudentDao studentDao; @Cacheable(cacheNames= {"Student"})
public Student getStudentById(Integer id) {
return studentDao.findStudentById(id);
}
}

  

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.example.demo.entity.Student;
import com.example.demo.service.StudentService; @Controller
@RequestMapping("/student/")
public class StudentController {
@Autowired
private StudentService studentService; @RequestMapping("index")
public String index() { return "index";
} @RequestMapping("show")
public String getStudengById(Student student,Model model) {
student = studentService.getStudentById(student.getId());
model.addAttribute("student", student);
return "index";
} }

  

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>index</h1>
<a href="/demo/student/show?id=1">查询学生</a>
<#if student??>
用户名:${student.name}
</#if>
</body>
</html>

  第一次查询会产生SQL,第二次查询没有产生SQL,说明缓存已经失效,或者第一次查询完毕后,修改数据库数据,再次查询,数据未改变,说明是从缓存获取的数据。

springboot整合Ehcache的更多相关文章

  1. 【Springboot】Springboot整合Ehcache

    刚刚项目上线了,记录下使用的技术...... EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache的特点 ( ...

  2. SpringBoot整合EHcache学习笔记

    为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和SpringBoot整合配置 前言介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特 ...

  3. springboot 整合ehcache缓存

    1.CacheManager Spring Boot默认集成CacheManager,如下包所示: 可以看出springboot自动配置了 JcacheCacheConfiguration. EhCa ...

  4. 转载-Springboot整合ehcache缓存

    转载:https://www.cnblogs.com/xzmiyx/p/9897623.html EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统 ...

  5. 【spring-boot】spring-boot 整合 ehcache 实现缓存机制

    方式一:老 不推荐 参考:https://www.cnblogs.com/lic309/p/4072848.html /*************************第一种   引入 ehcach ...

  6. 【spring-boot】spring-boot整合ehcache实现缓存机制

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心 ...

  7. spring-boot整合ehcache实现缓存机制

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心 ...

  8. Springboot整合Ehcache缓存

    Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...

  9. Springboot整合Ehcache 解决Mybatis二级缓存数据脏读 -详细

    前面有写了一篇关于这个,但是这几天又改进了一点,就单独一篇在详细说明一下 配置 application.properties ,启用Ehcache # Ehcache缓存 spring.cache.t ...

随机推荐

  1. caffe 中如何打乱训练数据

    第一: 可以选择在将数据转换成lmdb格式时进行打乱: 设置参数--shuffle=1:(表示打乱训练数据) 默认为0,表示忽略,不打乱. 打乱的目的有两个:防止出现过分有规律的数据,导致过拟合或者不 ...

  2. LeetCode 75 Sort Colors(颜色排序)

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  3. 树莓派、 Arduino 、传统单片机开发板该如何选择?

    几十年前的电子爱好者,最喜欢的就是电烙铁.面包板和收音机:十几年前,出现了单片机,于是玩具就成了电烙铁.面包板和单片机:到了2015年,贴片技术的不断普及,让面包板不再那么有用武之地,经济的发展也让现 ...

  4. 推荐vim配置

    "设置编码,处理中文乱码,文件默认utf8编码set fileencodings=utf-8,ucs-bom,cp936,big5 "设置默认配色方案colorscheme def ...

  5. [转]成员函数指针与高性能的C++委托

    原文(作者:Don Clugston):Member Function Pointers and the Fastest Possible C++ Delegates 译文(作者:周翔): 成员函数指 ...

  6. Android WebView 常见问题

    1.为WebView自定义错误显示界面: /** * 显示自定义错误提示页面,用一个View覆盖在WebView */ protected void showErrorPage() { LinearL ...

  7. qt 4.8.5 vs 2012编译

    Download Qt 4.8.3 source code from http://qt-project.org/downloads Go to mkspecs\win32-msvc2010. Ope ...

  8. 大数据(3) - 高可用 HDFS HA

    HDFS HA高可用 1 HA概述 1)所谓HA(high available),即高可用(7*24小时不中断服务). 2)实现高可用最关键的策略是消除单点故障.HA严格来说应该分成各个组件的HA机制 ...

  9. (C#)System.Security.SecureString(表示应保密的文本)

    正常的String类型值,在脱离开作用域之后,其值在内存中并不会被立即销毁,这时如果有人恶意扫描你的内存,程序中所保存的机密信息就会暴露;于是就有了System.Security.SecureStri ...

  10. plsql programming 14 DML和事务管理

    我们可以把多个SQL语句集中在一起, 在逻辑上组成一个事务, 从而保证这些操作或者全部被保存到数据库(用sql的说法就是”提交”), 或者被整体驳回(用sql的说法是“回滚”). 事务: ACID 原 ...