Spring Cache抽象 使用SpEL表示式

2021-08-08 23:33:15 字數 1183 閱讀 7614

在spring cache註解屬性中(比如key,condition和unless),spring的快取抽象使用了spel表示式,從而提供了屬性值的動態生成及足夠的靈活性。

下面的**根據使用者的usercode進行快取,對於key屬性,使用了表示式自定義鍵的生成。

public

class userservice

@cacheable(value = "users", key = "#user.usercode" condition = "#user.age < 35")

public user getuser(user user)

spel表示式可基於上下文並通過使用快取抽象,提供與root獨享相關聯的快取特定的內建引數。 名稱

位置描述

示例methodname

root物件

當前被呼叫的方法名

#root.methodname

method

root物件

當前被呼叫的方法

#root.method.name

target

root物件

當前被呼叫的目標物件例項

#root.target

targetclass

root物件

當前被呼叫的目標物件的類

#root.targetclass

args

root物件

當前被呼叫的方法的引數列表

#root.args[0]

caches

root物件

當前方法呼叫使用的快取列表

#root.caches[0].name

argument name

執行上下文

當前被呼叫的方法的引數,如findartisan(artisan artisan),可以通過#artsian.id獲得引數

#artsian.id

result

執行上下文

方法執行後的返回值(僅當方法執行後的判斷有效,如 unless cacheevict的beforeinvocation=false)

#result

spring cache簡單使用

spring從3.1起自帶了cache功能。可以快取乙個方法的返回值,也就是說如果有快取,spring就會直接使用快取值,而不會再去執行這個方法 cashe相關的功能是在spring context.4.2.5.release.jar這個jar包中的。然後,開啟cache註解,配置cachemana...

使用springCache快取的步驟

org.springframework.cache.concurrent.concurrentmapcachemanager加入ioc容器,用 cachemanager會報錯.spring專案因為沒有concurrentmapcachemanage快取報錯,網上我一般沒看到有文章會說明。在配置類上加...

SpringCache自我學習

cacheable 把查詢出來的資料放到快取 cacheevict 更新 刪除模式 cacheput 更新 雙寫模式 cacheable value是分割槽名 key是快取名 configuration enablecaching 開啟快取 enableconfigurationproperties...