Springboot的缓存抽象

在Springboot中支持基于注解方式,实现缓存,这简化了缓存的使用,且可以支持多种缓存模式:ConcurrentMap、EhCache、Caffeine、JCache(JSR-107)

一、主要注解

@EenableCacheing
开启缓存支持
@CacheEvict
缓存清理
@CachePut
缓存设置
@Caching
可以对缓存清理、设置 等操作打包
@CacheConfig
缓存设置
@Cacheable
如果缓存在存在该值,则用缓存数据, 如果不在缓存,则存入缓存

二、基于redis的缓存

1、引入cache、redis 的jar

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
	</dependency>
	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
	</dependency>

2、properties中加入配置文件

#缓存类型
spring.cache.type=redis
#默认缓存名称
spring.cache.cache-names=coffee
spring.cache.redis.time-to-live=5000
#是否缓存null对象
spring.cache.redis.cache-null-values=false
#是否缓存null对象
spring.redis.host=192.168.2.45
spring.redis.port=6379
spring.redis.password=test123

3、配置启用缓存
在这里插入图片描述
备注:缓存是基于动态代理实现的,拦截的类
4、业务类中配置缓存
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36918149/article/details/92428401