第九章 SpringBoot缓存-方法的缓存❤❤

概述

在这里插入图片描述

1. Ehcache 2.x 缓存

1.1 SpringBoot整合Ehcache缓存

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

 spring.cache.ehcache.config=classpath:config/another-config.xml

在这里插入图片描述

1.2 应用:缓存方法

缓存方法注解的使用 ❤❤❤

@Repository
@CacheConfig(cacheNames ="book_cache")
public class BookDao {
	@Cacheable
	public Book getBookById(Integer id){
		System.out.println("getBookById");
		Book book = new Book();
		book.setId(id);
		book.setName("三国演义");
		book.setAuthor("罗贯中");
		return book;
	}
	
	@CachePut(key ="#book.id")
	public Book updateBookById(Book book){
		System.out.println("updateBookById");
		book.setName("三国演义2");
		return book;
	}
	
	@CacheEvict(key ="#id")
	public void deleteBookById(Integer id){
		System.out.println("deleteBookById");
	}
}

在这里插入图片描述
在这里插入图片描述
自定义缓存key生成器
如果这些key不能够满足开发需求,开发者也可以自定义缓存key的生成器KeyGenerator,
代码如下:

@Component
public class MyKeyGenerator implements KeyGenerator {
	@Override
	public Object generate(Object target,Method method,object... params){
		return Arrays.tostring(params);
	}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. Redis实现

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

***************************************

相关推荐

  1. 4.12 SpringBoot整合AOP

    2024-04-28 06:54:02       14 阅读
  2. vue实际使用

    2024-04-28 06:54:02       34 阅读
  3. 23.2 微服务SpringCloud基础实战()

    2024-04-28 06:54:02       23 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-28 06:54:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-28 06:54:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-28 06:54:02       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-28 06:54:02       20 阅读

热门阅读

  1. android layout 的文件夹可以创建子文件夹吗

    2024-04-28 06:54:02       14 阅读
  2. Android 生成二维码

    2024-04-28 06:54:02       11 阅读
  3. 基于单片机的配电网故障自适应监控系统设计

    2024-04-28 06:54:02       13 阅读
  4. Redis面试题超详细(2024最新)

    2024-04-28 06:54:02       21 阅读
  5. Ubuntu22.04安装Nvidia 550驱动和CUDA toolkit 12.4.1

    2024-04-28 06:54:02       15 阅读
  6. 练习题(2024/4/27)

    2024-04-28 06:54:02       13 阅读
  7. iptables动作

    2024-04-28 06:54:02       24 阅读
  8. 【Kotlin】select简介

    2024-04-28 06:54:02       15 阅读
  9. 打地鼠游戏(python期中)

    2024-04-28 06:54:02       17 阅读
  10. 常用设计模式简介

    2024-04-28 06:54:02       9 阅读
  11. opencv 采样照片

    2024-04-28 06:54:02       10 阅读