首先引入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. sqlserver利用链接服务器查询或同步本地数据库和远程数据库

    这个实际上是SQLserver的分布式查询:如果一个项目需要二至多台服务器,而我们又必须从几台服务器中将数据取出来,这就必须用分布式查询!在这里有两个概念:本地数据源.远程数据源!本地数据源指的是单个 ...

  2. [k8s]openshiftv1.5.1安装笔记

    centos7安装 net.ifnames=0 biosdevname=0 初始化系统 yum install wget -y wget -O /etc/yum.repos.d/CentOS-Base ...

  3. Java配置文件读取和路径设置

    记录几种读取配置文件的方法,以及配置文件的放置路径. 1.使用PropertiesLoaderUtils工具类(springframework包提供) 优点:实时加载配置文件,修改后立即生效,不必重启 ...

  4. springboot开启事务管理

    spring中开启事务管理需要在xml配置文件中配置,springboot中采取java config的配置方式. 核心是@EnableTransactionManager注解,该注解即为开启事务管理 ...

  5. [原创]超强C#图片上传,加水印,自动生成缩略图源代码

    <%@ Page Language=“C#“ AutoEventWireup=“true“ %> <%@ Import Namespace=“System“ %> <%@ ...

  6. 通过主机名(域名)获取IP地址,主机别名等信息

    一.所用API函数介绍 struct hostent FAR*gethostbyname( const char FAR* name ); 传入參数:const char FAR* name.主机名或 ...

  7. MFC使用自带的MSXML6.dll解析xml(开发环境vc2010)

    程序是win32控制台程序 // msxml.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> ...

  8. 常用校验算法CRC、MD5、SHA_转

    1.算法概述 数据摘要算法是密码学算法中非常重要的一个分支,它通过对所有数据提取指纹信息以实现数据签名.数据完整性校验等功能,由于其不可逆性,有时候会被用做敏感信息的加密.数据摘要算法也被称为哈希(H ...

  9. 如何创建Windows定时任务

    我们经常使用电脑,有没有那么一个瞬间想着要是电脑可以每隔一段时间,自动处理一件事情就好了呢? 其实Windows还真有这样的功能,很多软件检测更新就是通过这个方法实现的. 这次我们来做一个简易的喝水提 ...

  10. PPT常用操作笔记

    1. 组合就是框成一组 2. 页与页之间的播放运作及效果设置“切换”,页内的元素过滤设置“动画” 3. 转PDF:PDF打印机或直接来个金山WPS 4. 讲义打印,要点:灰度.去深色背景.多页,弄了个 ...