zoukankan      html  css  js  c++  java
  • Spring缓存注解@Cacheable

    @Cacheable

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

    @Cacheable 作用和配置方法

    参数解释example
    value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:
    @Cacheable(value=”mycache”)
    @Cacheable(value={”cache1”,”cache2”}
    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")// 使用了一个缓存名叫 accountCache
    public Account getAccountByName(String userName) {
       // 方法内部实现不考虑缓存逻辑,直接实现业务
       System.out.println("real query account."+userName);
       return getFromDB(userName);
    }
  • 相关阅读:
    278.First Bad Version
    277. Find the Celebrity
    256.Paint House
    276. Paint Fence
    275. H-Index II
    274. H-Index
    273. Integer to English Words
    272. Closest Binary Search Tree Value II
    270. Closest Binary Search Tree Value
    271. Encode and Decode Strings
  • 原文地址:https://www.cnblogs.com/cocoxu1992/p/10676700.html
Copyright © 2011-2022 走看看