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

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

1、java

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

好比:redis

@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;


    }

2、@CachePut的  返回值 要和 @Cacheable 的返回值 同样,若是@Cacheable 返回的是一个对象,@CachePut 返回也要是对象,不然会报类型转换异常,如上代码 返回的都是 Uuser.数据库

相关文章
相关标签/搜索