欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

注釋介紹

創(chuàng)新互聯(lián)專注于白沙黎族企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,電子商務(wù)商城網(wǎng)站建設(shè)。白沙黎族網(wǎng)站建設(shè)公司,為白沙黎族等地區(qū)提供建站服務(wù)。全流程按需求定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

@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”),這個注釋的意思是,當調(diào)用這個方法的時候,會從一個名叫 accountCache 的緩存中查詢,如果沒有,則執(zhí)行實際的方法(即查詢數(shù)據(jù)庫),并將執(zhí)行的結(jié)果存入緩存中,否則返回緩存中的對象。這里的緩存中的 key 就是參數(shù) userName,value 就是 Account 對象?!癮ccountCache”緩存是在 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,如果你在你的方法寫別的名字,那么依然以方法的名字為準。

@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組合多個注解標簽了。

@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對象當前被調(diào)用的方法名root.methodName
methodroot對象當前被調(diào)用的方法root.method.name
targetroot對象當前被調(diào)用的目標對象root.target
targetClassroot對象當前被調(diào)用的目標對象類root.targetClass
argsroot對象當前被調(diào)用的方法的參數(shù)列表root.args[0]
cachesroot對象當前方法調(diào)用使用的緩存列表(如@Cacheable(value={“cache1”, “cache2”})),則有兩個cacheroot.caches[0].name
argument name執(zhí)行上下文當前被調(diào)用的方法的參數(shù),如findById(Long id),我們可以通過#id拿到參數(shù)user.id
result執(zhí)行上下文方法執(zhí)行后的返回值(僅當方法執(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)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁名稱:詳解Spring緩存注解@Cacheable,@CachePut,@CacheEvict使用
URL分享:http://aaarwkj.com/article20/pjcoco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)站改版、企業(yè)建站、Google商城網(wǎng)站、App設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
国产精品亚洲欧美日韩综合| 日本一本一道高清不卡视频| 国产精品夜色一区二区三区不卡 | 国产一级二级三级大胆视频| 欧美私人影院—区二区日本| av中文字幕一区二区三区| 日本一区二区裸体视频| 日韩女同性一区二区三区| 婷婷不卡中文字幕三区| 久久夜色精品亚洲国产| 中文字幕不卡在线观看不卡| 亚洲成人黄色片在线观看| 人妻一区二区免费视频| 最新国产精品欧美激情| 日本精品视频免费网| 很黄很刺激的视频中文字幕| 亚洲av成人在线不卡| 不卡的av中文字幕在线播放| 人体蜜桃视频一区二区| 日日狠狠久久偷偷综合色| 国内精品久久久国产盗摄| 美女高潮呻吟免费观看久久久| 亚洲精品麻豆一区二区| 国产自拍免费在线观看视频| 日本啪啪精品一区二区三区| 中国毛片一区二区三区| 日韩av黄色大片在线播看| 日韩精品一区三区二区| 国产成人亚洲一区二区三区| 国产特级黄片免费观看| 久久超碰一区二区三区| 麻豆蜜桃精品视频在线观看| 加勒比视频在线观看一区| 亚洲国产欧美日韩久久| 激情一区二区三区视频| 国产亚洲一区二区视频| 97在线观看免费播放| 日韩精品一区二区在线天天狠天| 日本高清视频免费一区| 成年视频免费观看视频| 国产成人国产精品国产三级|