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、业务类中配置缓存
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值