@CachePut 更新数据库,更新缓存

关于更新缓存 ,要注意 得两点:

一、

@Cacheable 的key 要和 @CachePut 的key 一致

比如:

  @Cacheable(key = "'userCache'") //缓存,
    public Uuser findByEmail(String email) {

        System.err.println("执行这里,说明缓存中读取不到数据,直接读取数据库....");
        return redisMapper.findByEmail(email);
    }
  @CachePut(key = "'userCache'") //userCache要加‘’单引号,表示这是一个字符串
    public Uuser updateSelf(String nickname, String email) {
        System.err.println("执行这里,更新数据库,更新缓存....");
        uuserMapper.updateSelf(nickname, email);
        Uuser uuser = redisMapper.findByEmail(email);

        return uuser;


    }

二、@CachePut的  返回值 要和 @Cacheable 的返回值 一样,如果@Cacheable 返回的是一个对象,@CachePut 返回也要是对象,否则会报类型转换异常,如上代码 返回的都是 Uuser.

猜你喜欢

转载自blog.csdn.net/Joe_Wang1/article/details/81354586