欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring緩存注解@Cacheable,@CachePut , @CacheEvict使用

 更新時間:2017年05月03日 16:45:56   作者:whatlookingfor  
這篇文章主要介紹了詳解Spring緩存注解@Cacheable,@CachePut , @CacheEvict使用,非常具有實用價值,需要的朋友可以參考下

注釋介紹

@Cacheable

@Cacheable 的作用 主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其結(jié)果進行緩存

@Cacheable 作用和配置方法

參數(shù)解釋example
value緩存的名稱,在 spring 配置文件中定義,必須指定至少一個例如:
@Cacheable(value=”mycache”)
@Cacheable(value={”cache1”,”cache2”}
key緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數(shù)進行組合@Cacheable(value=”testcache”,key=”#userName”)
condition緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進行緩存@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

實例

@Cacheable(value=”accountCache”),這個注釋的意思是,當(dāng)調(diào)用這個方法的時候,會從一個名叫 accountCache 的緩存中查詢,如果沒有,則執(zhí)行實際的方法(即查詢數(shù)據(jù)庫),并將執(zhí)行的結(jié)果存入緩存中,否則返回緩存中的對象。這里的緩存中的 key 就是參數(shù) userName,value 就是 Account 對象。“accountCache”緩存是在 spring*.xml 中定義的名稱。

@Cacheable(value="accountCache")// 使用了一個緩存名叫 accountCache 
public Account getAccountByName(String userName) {
   // 方法內(nèi)部實現(xiàn)不考慮緩存邏輯,直接實現(xiàn)業(yè)務(wù)
   System.out.println("real query account."+userName); 
   return getFromDB(userName); 
} 

@CachePut

@CachePut 的作用 主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其結(jié)果進行緩存,和 @Cacheable 不同的是,它每次都會觸發(fā)真實方法的調(diào)用

@CachePut 作用和配置方法

參數(shù)解釋example
value緩存的名稱,在 spring 配置文件中定義,必須指定至少一個@CachePut(value=”my cache”)
key緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數(shù)進行組合@CachePut(value=”testcache”,key=”#userName”)
condition緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進行緩存@CachePut(value=”testcache”,condition=”#userName.length()>2”)

實例

@CachePut 注釋,這個注釋可以確保方法被執(zhí)行,同時方法的返回值也被記錄到緩存中,實現(xiàn)緩存與數(shù)據(jù)庫的同步更新。

@CachePut(value="accountCache",key="#account.getName()")// 更新accountCache 緩存
public Account updateAccount(Account account) { 
  return updateDB(account); 
} 

@CacheEvict

@CachEvict 的作用 主要針對方法配置,能夠根據(jù)一定的條件對緩存進行清空

@CacheEvict 作用和配置方法

參數(shù)解釋example
value緩存的名稱,在 spring 配置文件中定義,必須指定至少一個@CacheEvict(value=”my cache”)
key緩存的 key,可以為空,如果指定要按照 SpEL 表達式編寫,如果不指定,則缺省按照方法的所有參數(shù)進行組合@CacheEvict(value=”testcache”,key=”#userName”)
condition緩存的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,只有為 true 才進行緩存@CacheEvict(value=”testcache”,condition=”#userName.length()>2”)
allEntries是否清空所有緩存內(nèi)容,缺省為 false,如果指定為 true,則方法調(diào)用后將立即清空所有緩存@CachEvict(value=”testcache”,allEntries=true)
beforeInvocation是否在方法執(zhí)行前就清空,缺省為 false,如果指定為 true,則在方法還沒有執(zhí)行的時候就清空緩存,缺省情況下,如果方法執(zhí)行拋出異常,則不會清空緩存@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) { 
 // 方法內(nèi)部實現(xiàn)不考慮緩存邏輯,直接實現(xiàn)業(yè)務(wù)
 return getFromDB(userName); 
}

@CacheConfig

所有的@Cacheable()里面都有一個value=“xxx”的屬性,這顯然如果方法多了,寫起來也是挺累的,如果可以一次性聲明完 那就省事了, 所以,有了@CacheConfig這個配置,@CacheConfig is a class-level annotation that allows to share the cache names,如果你在你的方法寫別的名字,那么依然以方法的名字為準(zhǔn)。

@CacheConfig("books")
public class BookRepositoryImpl implements BookRepository {

  @Cacheable
  public Book findBook(ISBN isbn) {...}
}

條件緩存

下面提供一些常用的條件緩存

//@Cacheable將在執(zhí)行方法之前( #result還拿不到返回值)判斷condition,如果返回true,則查緩存; 
@Cacheable(value = "user", key = "#id", condition = "#id lt 10")
public User conditionFindById(final Long id) 

//@CachePut將在執(zhí)行完方法后(#result就能拿到返回值了)判斷condition,如果返回true,則放入緩存; 
@CachePut(value = "user", key = "#id", condition = "#result.username ne 'zhang'") 
public User conditionSave(final User user)  

//@CachePut將在執(zhí)行完方法后(#result就能拿到返回值了)判斷unless,如果返回false,則放入緩存;(即跟condition相反)
@CachePut(value = "user", key = "#user.id", unless = "#result.username eq 'zhang'")
public User conditionSave2(final User user)  

//@CacheEvict, beforeInvocation=false表示在方法執(zhí)行之后調(diào)用(#result能拿到返回值了);且判斷condition,如果返回true,則移除緩存;
@CacheEvict(value = "user", key = "#user.id", beforeInvocation = false, condition = "#result.username ne 'zhang'") 
public User conditionDelete(final User user)  

@Caching

有時候我們可能組合多個Cache注解使用;比如用戶新增成功后,我們要添加id–>user;username—>user;email—>user的緩存;此時就需要@Caching組合多個注解標(biāo)簽了。

@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) {

自定義緩存注解

比如之前的那個@Caching組合,會讓方法上的注解顯得整個代碼比較亂,此時可以使用自定義注解把這些注解組合到一個注解中,如:

@Caching(put = {
@CachePut(value = "user", key = "#user.id"),
@CachePut(value = "user", key = "#user.username"),
@CachePut(value = "user", key = "#user.email")
})
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface UserSaveCache {
}

這樣我們在方法上使用如下代碼即可,整個代碼顯得比較干凈。

@UserSaveCache
public User save(User user)

擴展

比如findByUsername時,不應(yīng)該只放username–>user,應(yīng)該連同id—>user和email—>user一起放入;這樣下次如果按照id查找直接從緩存中就命中了

@Caching(
  cacheable = {
    @Cacheable(value = "user", key = "#username")
  },
  put = {
    @CachePut(value = "user", key = "#result.id", condition = "#result != null"),
    @CachePut(value = "user", key = "#result.email", condition = "#result != null")
  }
)
public User findByUsername(final String username) {
  System.out.println("cache miss, invoke find by username, username:" + username);
  for (User user : users) {
    if (user.getUsername().equals(username)) {
      return user;
    }
  }
  return null;
}

其實對于:id—>user;username—->user;email—>user;更好的方式可能是:id—>user;username—>id;email—>id;保證user只存一份;如:

@CachePut(value="cacheName", key="#user.username", cacheValue="#user.username") 
public void save(User user)  


@Cacheable(value="cacheName", key="#user.username", cacheValue="#caches[0].get(#caches[0].get(#username).get())") 
public User findByUsername(String username) 

SpEL上下文數(shù)據(jù)

Spring Cache提供了一些供我們使用的SpEL上下文數(shù)據(jù),下表直接摘自Spring官方文檔:

名稱位置描述示例
methodNameroot對象當(dāng)前被調(diào)用的方法名root.methodName
methodroot對象當(dāng)前被調(diào)用的方法root.method.name
targetroot對象當(dāng)前被調(diào)用的目標(biāo)對象root.target
targetClassroot對象當(dāng)前被調(diào)用的目標(biāo)對象類root.targetClass
argsroot對象當(dāng)前被調(diào)用的方法的參數(shù)列表root.args[0]
cachesroot對象當(dāng)前方法調(diào)用使用的緩存列表(如@Cacheable(value={“cache1”, “cache2”})),則有兩個cacheroot.caches[0].name
argument name執(zhí)行上下文當(dāng)前被調(diào)用的方法的參數(shù),如findById(Long id),我們可以通過#id拿到參數(shù)user.id
result執(zhí)行上下文方法執(zhí)行后的返回值(僅當(dāng)方法執(zhí)行之后的判斷有效,如‘unless’,’cache evict’的beforeInvocation=false)result
@CacheEvict(value = "user", key = "#user.id", condition = "#root.target.canCache() and #root.caches[0].get(#user.id).get().username ne #user.username", beforeInvocation = true) 
public void conditionUpdate(User user) 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • servlet之web路徑問題_動力節(jié)點Java學(xué)院整理

    servlet之web路徑問題_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細介紹了servlet之web路徑問題的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • java中aop實現(xiàn)接口訪問頻率限制

    java中aop實現(xiàn)接口訪問頻率限制

    本文主要介紹了java中aop實現(xiàn)接口訪問頻率限制,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • SpringBoot結(jié)合mybatis-plus實現(xiàn)分頁的項目實踐

    SpringBoot結(jié)合mybatis-plus實現(xiàn)分頁的項目實踐

    本文主要介紹了SpringBoot結(jié)合mybatis-plus實現(xiàn)分頁的項目實踐,主要基于MyBatis-Plus 自帶的分頁插件 PaginationInterceptor,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Spring負載均衡LoadBalancer使用詳解

    Spring負載均衡LoadBalancer使用詳解

    這篇文章主要介紹了Spring負載均衡LoadBalancer使用詳解,Spring Cloud LoadBalancer是Spring Cloud官方自己提供的客戶端負載均衡器, 用來替代Ribbon,Spring官方提供了兩種客戶端都可以使用loadbalancer,需要的朋友可以參考下
    2023-11-11
  • Java中的源文件、字節(jié)碼文件解讀

    Java中的源文件、字節(jié)碼文件解讀

    這篇文章主要介紹了Java中的源文件、字節(jié)碼文件,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java正則表達式之全量匹配和部分匹配

    Java正則表達式之全量匹配和部分匹配

    正則表達式異常強大,一直理解不深,用的也不深,這次項目中嘗試,體會到了它的強大之處,這篇文章主要給大家介紹了關(guān)于Java正則表達式之全量匹配和部分匹配的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • 自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問題

    自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問題

    這篇文章主要介紹了自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Spring Boot 定制與優(yōu)化內(nèi)置的Tomcat容器實例詳解

    Spring Boot 定制與優(yōu)化內(nèi)置的Tomcat容器實例詳解

    本文主要記錄對內(nèi)置容器優(yōu)化和定制的方式,用于自己加深對SpringBoot理解。本文給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友參考下吧
    2017-12-12
  • 淺談TreeSet中的兩種排序方式

    淺談TreeSet中的兩種排序方式

    下面小編就為大家?guī)硪黄獪\談TreeSet中的兩種排序方式。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java中保證多線程間的數(shù)據(jù)共享的方法詳解

    Java中保證多線程間的數(shù)據(jù)共享的方法詳解

    這篇文章詳解的發(fā)給大家介紹了Java中是如何保證多線程間的數(shù)據(jù)共享的,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-11-11

最新評論