介绍

这里介绍Ehcache 2.X 缓存

添加基本的web项目

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mbNARxlj-1592666394129)(https://www.iming.info/wp-content/uploads/2020/06/wp_editor_md_a364d1423e3bdfc4066b7266a02a2393.jpg)]

添加Ehcache 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cache --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId><version>2.3.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache --><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.10.6</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

添加配置文件

添加ehcache 配置缓存文件

<ehcache><diskStore path="java.io.tmpdir/shiro-spring-sample"/><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"/><cache name="book_cache"maxElementsInMemory="10000"eternal="true"overflowToDisk="true"diskPersistent="true"diskExpiryThreadIntervalSeconds="600"/>
</ehcache>

注解上开启缓存

在启动类上添加相关的注解

package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

使用缓存

创建实体类和BookService

创建实体类

package com.example.demo;import java.io.Serializable;public class Book implements Serializable {private Integer id;private String name;private String author;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}
}

添加相关的service

package com.example.demo;import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;@Repository
// 使用缓存的名称
@CacheConfig(cacheNames = "book_cache")
public class BookDao {// 对该方法进行缓存@Cacheablepublic Book getBookById(Integer id){System.out.println("调用Service方法");Book book = new Book();book.setAuthor("ming");book.setId(id);book.setName("ming");return book;}@CachePut(key = "#book.id")// 用在更新方法上,数据库数据更新,缓存数据也要更新,方法的返回值自动更新在已经保存的key上public Book updateBookById(Book book){System.out.println("更新ById");book.setName("ming2");return book;}@CacheEvict(key = "#id")// 用在删除方法上,数据删除以后,其方法也会删除public void deleteBookById(Integer id){System.out.println("id删除");}
}

创建测试类

创建测试类,对Service方法进行测试

package com.example.demo;import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;import static org.junit.jupiter.api.Assertions.*;@ExtendWith(SpringExtension.class) //导入spring测试框架[2]
@SpringBootTest//提供spring依赖注入
class BookDaoTest {@Autowiredprivate BookDao bookDao;@Testpublic void test(){bookDao.getBookById(1);bookDao.getBookById(1);bookDao.deleteBookById(1);Book book = bookDao.getBookById(1);System.out.println(book);book.setName("mingming");book.setAuthor("mingming");book.setId(1);bookDao.updateBookById(book);Book book1 = bookDao.getBookById(1);System.out.println(book1);}
}

查看结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V1Tw3ceL-1592666394166)(https://www.iming.info/wp-content/uploads/2020/06/wp_editor_md_4935782fec7d7ce419a89ccfe187ebe9.jpg)]

微信公众号

解释SpringBoot之Ehcache 2.x缓存相关推荐

  1. ehcache怎么删除缓存_解释SpringBoot之Ehcache 2.x缓存

    介绍 这里介绍Ehcache 2.X 缓存 添加基本的web项目 添加Ehcache 依赖 xml version="1.0" encoding="UTF-8" ...

  2. springboot整合ehcache+redis实现双缓存

    在一些对并发业务要求较高的场景下,对页面数据的响应是个急需解决的问题,对后端来说,ehcache+redis实现双缓存是解决这一问题的不错思路,而且在不少的电商项目中得到了很好的验证,但我在网上搜寻资 ...

  3. 8分钟带你学会SpringBoot整合Redis来实现缓存技术

    1.概述 随着互联网技术的发展,对技术要求也越来越高,所以在当期情况下项目的开发中对数据访问的效率也有了很高的要求,所以在项目开发中缓存技术使用的也越来越多,因为它可以极大的提高系统的访问速度,关于缓 ...

  4. springboot整合ehcache使用

    springboot整合ehcache使用 其实本地缓存的解决方案也有很多种,像Ehcache,GuavaCache,JCache等目前Ehcache 是现在最流行的纯Java开源缓存框架,配置简单. ...

  5. 搭建第一个SpringBoot工程;SpringBoot整合mybatis;SpringBoot整合Redis-cluster集群;SpringBoot整合EhCache;

    写在前头,以下内容主要是为了自己复习之用,如果你有幸看到这篇文章,请不要嫌弃某些地方有所跳跃或省略. 1. 搭建第一个SpringBoot工程 1.1 创建工程 1.2 初始pom.xml文件内容 1 ...

  6. (12) Hibernate+EhCache配置二级缓存

    转载地址 http://jyao.iteye.com/blog/1315726 (有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 ) 本文主 ...

  7. java 项目做多级缓存_【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)...

    一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...

  8. java+cache使用方法_java相关:springboot使用GuavaCache做简单缓存处理的方法

    java相关:springboot使用GuavaCache做简单缓存处理的方法 发布于 2020-3-29| 复制链接 摘记: 问题背景 实际项目碰到一个上游服务商接口有10秒的查询限制(同个账号). ...

  9. Hibernate EHCache - Hibernate二级缓存

    Hibernate EHCache - Hibernate二级缓存 欢迎使用Hibernate二级缓存示例教程.今天我们将研究Hibernate EHCache,它是最受欢迎的Hibernate二级缓 ...

最新文章

  1. 5 修改request对象变量_【总结】前端5大常见设计模式,代码一看你就懂!
  2. Nature方法 | 三代长读长宏基因组组装软件metaFlye
  3. 几种典型的软件自动化测试框架
  4. 访华为5G首席科学家童文:针尖战略引领5G突破
  5. PHP读取excel表格内容 PHP-ExcelReader
  6. python flask 路由_python框架之Flask(2)-路由和视图Session
  7. web服务器软件_概述
  8. Rule-Guided Compositional Representation Learning on Knowledge Graphs-学习笔记
  9. java定义一个静态类_Java中的静态类
  10. 为什么你拼命学,却一无所成?
  11. bzoj 1078 [SCOI2008]斜堆 —— 斜堆
  12. 电商金额计算的 4 个坑,千万注意了!
  13. python 成语库_Python“Every Other Element”成语
  14. java 自定义 转换器_自定义类型转换器
  15. C语言典型例题四——斐波那契数列
  16. inflect java_Python lemminflect包_程序模块 - PyPI - Python中文网
  17. rgb的颜色转换以及十六进制转为十进制的那些事
  18. 桌面程序聊天窗口的自动输入内容及自动发送
  19. Python3,9行批量提取PDF文件的指定内容,这种操作,保证人见人爱....
  20. 先用总分升序再用计算机降序,怎样用升序降序给EXCEL排名

热门文章

  1. 2016全球可再生能源投资额为2416亿美元
  2. 自动安装虚拟机之网络安装和pxe安装
  3. iOS小技巧---改变uisearchbar中的cancel按钮的文字、取消clearButton
  4. 项目管理修炼之道之估算工作
  5. 大屏监控系统实战(10)-大屏展示前20个博主的排名、票数及名次相对于前一日的升降情况
  6. 安装mysql 5.1 详细步骤
  7. 取消搜索状态_百度搜索引擎全网推广通常有哪些账户?
  8. Backpack II 0-1背包
  9. IT兄弟连 JavaWeb教程 使用Servlet实现在页面中显示随机数
  10. 通过建站学运维1901-08任务