• 【redis-03】redis注解的使用解析


    (1)redis的spring注解简介

    spring缓存注解@Cacheable,@CachePut ,@CacheEvict,@EnableCaching

    (2)@Cacheable注解:查数据

    使用规则

    作用是主要针对方法配置,能够根据方法的请求参数对其结果进行缓存

    参数解释例子
    value缓存的名称,在 spring 配置文件中定义,必须指定至少一个例如:@Cacheable(value=”mycache”),@Cacheable(value={”cache1”,”cache2”} ,@Cacheable(value = “my-redis-cache1”,key = “‘book’+#bid”,condition = “#bid>10”)
    key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合@Cacheable(value=”testcache”,key=”#userName”)
    condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

    实例

    //@Cacheable(value=”accountCache”),这个注释的意思是,当调用这个方法的时候,会从一个名叫 accountCache 的缓存中查询,如果没有,则执行实际的方法(即查询数据库),
    //并将执行的结果存入缓存中,否则返回缓存中的对象。这里的缓存中的 key 就是参数 userName,value 就是 Account 对象。“accountCache”缓存是在 spring*.xml 中定义的名称。
    @Cacheable(value="accountCache")
    public Account getAccountByName(String userName) {
        // 方法内部实现不考虑缓存逻辑,直接实现业务
        System.out.println("real query account."+userName);
        return getFromDB(userName);
    }
    
    //表示使用my-redis-cache1缓存空间,key的生成策略为book+bid,当bid>10的时候才会使用缓存
    @Cacheable(value = "my-redis-cache1",key = "'book'+#bid",condition = "#bid>10")
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    (3)@CachePut:存数据

    使用规则

    • @CachePut 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用
    • @CachePut 作用和配置方法

    例子

    参数解释例子
    value缓存的名称,在 spring 配置文件中定义,必须指定至少一个@CachePut(value=”my cache”)
    key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合@CachePut(value=”testcache”,key=”#userName”)
    condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@CachePut(value=”testcache”,condition=”#userName.length()>2”)

    实例

    //@CachePut 注释,这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,实现缓存与数据库的同步更新。
    @CachePut(value="accountCache",key="#account.getName()")// 更新accountCache 缓存
    public Account updateAccount(Account account) { 
      return updateDB(account); 
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (4)@CacheEvict:删数据

    使用规则

    • @CachEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空
    • @CacheEvict 作用和配置方法

    例子

    参数解释例子
    value缓存的名称,在 spring 配置文件中定义,必须指定至少一个@CacheEvict(value=”my cache”)
    key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合@CacheEvict(value=”testcache”,key=”#userName”)
    condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@CacheEvict(value=”testcache”,condition=”#userName.length()>2”)
    allEntries是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存@CachEvict(value=”testcache”,allEntries=true)
    beforeInvocation是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存@CachEvict(value=”testcache”,beforeInvocation=true)

    实例

    //@CacheEvict(value="accountCache",key="#account.getName()")// 清空accountCache 缓存 
    public void updateAccount(Account account) {
       updateDB(account); 
    } 
     
    //@CacheEvict(value="accountCache",allEntries=true)// 清空accountCache 缓存
    public void reload() {
       reloadAll()
    }
     
    //@Cacheable(value="accountCache",condition="#userName.length() <=4")// 缓存名叫 accountCache 
    public Account getAccountByName(String userName) { 
     // 方法内部实现不考虑缓存逻辑,直接实现业务
     return getFromDB(userName); 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    (5)@CacheConfig

    使用规则

    所有的@Cacheable()里面都有一个value=“xxx”的属性,这显然如果方法多了,写起来也是挺累的,如果可以一次性声明完 那就省事了, 所以,有了@CacheConfig这个配置,一个类中可能会有多个缓存操作,而这些缓存操作可能是重复的。这个时候可以使用@CacheConfig。

    实例

    @CacheConfig("books")
    public class BookRepositoryImpl implements BookRepository {
      @Cacheable
      public Book findBook(ISBN isbn) {...}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (6)条件缓存

    一些常用的条件缓存

    //@Cacheable将在执行方法之前( #result还拿不到返回值)判断condition,如果返回true,则查缓存; 
    @Cacheable(value = "user", key = "#id", condition = "#id lt 10")
    public User conditionFindById(final Long id) 
     
    //@CachePut将在执行完方法后(#result就能拿到返回值了)判断condition,如果返回true,则放入缓存; 
    @CachePut(value = "user", key = "#id", condition = "#result.username ne 'zhang'") 
    public User conditionSave(final User user)  
     
    //@CachePut将在执行完方法后(#result就能拿到返回值了)判断unless,如果返回false,则放入缓存;(即跟condition相反)
    @CachePut(value = "user", key = "#user.id", unless = "#result.username eq 'zhang'")
    public User conditionSave2(final User user)  
     
    //@CacheEvict, beforeInvocation=false表示在方法执行之后调用(#result能拿到返回值了);且判断condition,如果返回true,则移除缓存;
    @CacheEvict(value = "user", key = "#user.id", beforeInvocation = false, condition = "#result.username ne 'zhang'") 
    public User conditionDelete(final User user) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    (7) @Caching

    使用规则

    有时候我们可能组合多个Cache注解使用;比如用户新增成功后,我们要添加id–>user;username—>user;email—>user的缓存;此时就需要@Caching组合多个注解标签了。

    实例

        @Caching(put = {
        @CachePut(value = "user", key = "#user.id"),
        @CachePut(value = "user", key = "#user.username"),
        @CachePut(value = "user", key = "#user.email")
        })
        public User save(User user) {
         
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (8)序列化

    user实体类:这里必须要序列化,不然进行缓存时会出错。
    public class User implements Serializable

  • 相关阅读:
    2023江西财经大学计算机考研信息汇总
    flutter 状态栏 AppBar 设置透明和半透明
    编程开发中的的字符编码与解码-原理篇
    springboot整合thymeleaf
    python最常用的三种输出格式
    前端面试题日常练-day54 【面试题】
    Linux之多线程
    如何实现企业软件的“超级 App 化”?
    在博客园写第一篇博客,附带极简设置
    图片翻译软件哪个好用?这些软件值得收藏
  • 原文地址:https://blog.csdn.net/weixin_44823875/article/details/125454409