redis缓存-注解-CacheConfig-Caching-CachePut-CacheEvict

@CacheConfig(cacheNames = {"cache-Cacheconfig"})  //设置全局缓存名称。
public class cacheConfigServiceImpl implements cacheConfigService {

    /**
     * @CachePut: 这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,实现缓存与数据库的同步更新。
     * @Cacheable(value=”accountCache”),这个注释的意思是,当调用这个方法的时候,会从一个名叫 accountCache 的缓存中查询,如果没有,则执行实际的方法(即查询数据库),并将执行的结果存入缓存中,否则返回缓存中的对象
     * @CachEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空
     *
     *   @Cacheable(key = "#str",condition="#str.length()>2")
     *   condition,相当于条件判断是否,需要加入缓存·
     *
     * @Caching(put = {
     * @CachePut(value = "user", key = "#user.id"),
     * @CachePut(value = "user", key = "#user.username"),
     * @CachePut(value = "user", key = "#user.email")
     * })
     *    有时候我们可能组合多个Cache注解使用;比如用户新增成功后,我们要添加id–>user;username—>user;email—>user的缓存;此时就需要@Caching组合多个注解标签了
     *
     * @Cacheable(key = "#str",condition="#str.length()>2")  因为这个注解在进入方法前去检测condition    condition = "#result != null" , 而这时还没有result,会造成result为null的情况。
     *
     * allEntries=true,清除该当前value值空间下的所有缓存数据
     *
     * beforeInvocation = true  先执行缓存,在执行方法
     * @param str
     * @return
     */



    @Cacheable(key = "#str",condition="#str.length()>2")  //设置缓存 key值
    @Override
    public String test1Config(String str) {
        return str;
    }

    @CacheEvict(key = "#str", beforeInvocation = true)  //通过key 移除redis缓存 , beforeInvocation = true, 执行方法前执行缓存。  但是:默认在当前注解方法成功执行之后再清除。
    @Override
    public String test2Config(String str) {
        return str;
    }
    /**
     * unless的作用时机: 目标方法运行后
     *                  注: 如果(因为直接从缓存中获取到了数据,而导致)目标方法没有被执行,那么unless字段不生效
     *
     * unless的功能: 是否  令注解(在方法执行后的功能)不生效;
     *              注:unless的结果为true,则(方法执行后的功能)不生效;
     *                 unless的结果为false,则(方法执行后的)功能生效.
     *
     *  举例说明一: 对于@Cacheable注解,在执行目标方法前,如果从缓存中查询到了数据,那么直接返回缓存中的数据;
     *            如果从 缓存中没有查询到数据,那么执行目标方法,目标方法执行完毕之后,判断unless的结果,
     *            若unless的结果为true,那么不缓存方法的返回值;
     *            若unless的结果为false,那么缓存方法的返回值。
     *
     *  举例说明二: 对于@CachePut注解,在目标方法执行完毕之后,判断unless的结果,
     *            若unless的结果为true,那么不缓存方法的返回值;
     *            若unless的结果为false,那么缓存方法的返回值。
     *
     * 注:unless默认为"",即相当于默认为false.
     *
     * 注:因为unless的作用时机是在方法运行完毕后,所以我们可以用SpEL表达式   #result   来获取方法的返回值
     */
    @CachePut( key = "#i + '*****' ", unless = "#result < 5000")
    @Override
    public Integer test3Config(Integer i) {
        return i;
    }


    /**
     * #a0+1   ,,,如果传的参数是  1开始,   那么加一就是从2开始
     *例如:传参数是 1,2,3,4,5,6,7,8,9    那么参数级别加起来是  2,3,4,5,6,7,8,9,10
     * #a0 代表第一个参数
     *
     * @param i
     * @return
     */


    @CacheUser.UserSaveCache2
    @Override
    public Integer test4Config(int i) {
        return null;
    }

    /**
     *
     * 多命名空间,,当进行缓存读取时,,会按照命名空间的顺讯,挨个从命名空间中查询对应的key,
     * 如果在某个命名空间找到缓存,就不会查询排在后面的命名空间,也不对执行对应的方法
     * @param str
     * @return
     */
    @Cacheable(cacheNames = {"TestConditionSpaceB", "TestConditionSpaceA"}, key = "'abcd'+#str")
    @Override
    public String test5Config(String str) {
        System.out.println("说明(指定cacheNames下存在对应key的缓存)为false!");
        return str;
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值