Spring Cache 优雅的方式使用缓存

一、介绍

1、@Cacheable

@Cacheable 根据方法对其返回结果进行缓存,下次请求时,如果缓存存在,则直接读取缓存数据返回;如果缓存不存在,则执行方法,并把返回的结果存入缓存中。一般用在查询方法上。

常用属性

属性名称描述
value缓存名,必填,它指定了你的缓存存放在哪块命名空间
cacheNames与 value 差不多,二选一即可
key可选属性,可以使用 SpEL 标签自定义缓存的key
condition缓存条件,为 true 时,使其 @Cacheable 注解生效
unless为 true 时,将结果存入缓存。

2、@CachePut

@CachePut 标注的方法,每次都会执行该方法,并将结果结果存入指定的缓存中。一般用在新增方法上。

常用属性

属性名称描述
value缓存名,必填,它指定了你的缓存存放在哪块命名空间
cacheNames与 value 差不多,二选一即可
key可选属性,可以使用 SpEL 标签自定义缓存的key
condition缓存条件,为 true 时,使其 @Cacheable 注解生效
unless为 true 时,将结果存入缓存。

3、@CacheEvict

@CacheEvict 是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作,使用该注解标志的方法,会清空指定的缓存。一般用在更新或者删除方法上,如果方法因为抛出异常而未能成功返回时也不会触发清除操作。

常用属性

属性名称描述
value缓存名,必填,它指定了你的缓存存放在哪块命名空间
cacheNames与 value 差不多,二选一即可
key可选属性,可以使用 SpEL 标签自定义缓存的key
condition缓存条件,为 true 时,使其 @Cacheable 注解生效
allEntries是否清空所有缓存,默认为 false。如果指定为 true,则方法调用后将立即清空所有的缓存
beforeInvocation是否在方法执行前就清空,默认为 false。如果指定为 true,则在方法执行前就会清空缓存

二、配置

1、导入依赖

	 <!-- redis -->
	  <dependency>
	      <groupId>org.springframework.boot</groupId>
	      <artifactId>spring-boot-starter-data-redis</artifactId>
	  </dependency>
	
	  <!-- spring2.X集成redis所需common-pool2-->
	  <dependency>
	      <groupId>org.apache.commons</groupId>
	      <artifactId>commons-pool2</artifactId>
	      <version>2.6.0</version>
	  </dependency>

2、开启缓存

在启动类或配置类上田间 @EnableCaching 注解,开启缓存
在这里插入图片描述

3、配置生成 key 策略(选填)

Spring cache 的 key 默认是通过 KeyGenerator 生成的。也可以自己去配置 key 的生成策略。

在配置类中创建 Bean:

@Bean
public KeyGenerator keyGenerator() {
    return new KeyGenerator() {
        @Override
        public Object generate(Object target, Method method, Object... params) {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
                sb.append(obj.toString());
            }
            return sb.toString();
        }
    };
}

三、使用

@Cacheable

// 缓存名为 userCache,key 为方法名:id,结果不为 null 时存入缓存
@Cacheable(value= "userCache", key = "#root.methodName + ':'+ #id", unless = "#result == null")
public User getUserById(Long id) {
    ...
    return getById(id);
}

@CachePut

// 缓存名为 userCache,key 使用配置的生成策略,结果不为 null 时存入缓存
@CachePut(value= "userCache", unless = "#result == null")
public User addUser(User user) {
    ...
    User user = save(user);
    return user;
}

@CacheEvict

// 在执行此方法前,删除缓存名为 userCache 的所有key
@CacheEvict(value= "userCache", allEntries = true, beforeInvocation = true)
public boolean deleteUser(Long id) {
    ...
    return deleteById(id);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一支帆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值