spring cache簡單使用

2021-07-15 13:07:18 字數 470 閱讀 1277

spring從3.1起自帶了cache功能。可以快取乙個方法的返回值,也就是說如果有快取,spring就會直接使用快取值,而不會再去執行這個方法;

cashe相關的功能是在spring-context.4.2.5.release.jar這個jar包中的。

<?xml version="1.0" encoding="utf-8"?>

然後,開啟cache註解,配置cachemanager:

在service中使用@cacheable註解:

@service

public class userservice

}

第一次呼叫getuserbyid方法會列印"執行getuserbyid",第二次呼叫不會列印。重啟服務後快取會被清空,因為是存在記憶體裡的。

Spring Cache抽象 使用SpEL表示式

在spring cache註解屬性中 比如key,condition和unless spring的快取抽象使用了spel表示式,從而提供了屬性值的動態生成及足夠的靈活性。下面的 根據使用者的usercode進行快取,對於key屬性,使用了表示式自定義鍵的生成。public class userser...

使用springCache快取的步驟

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

SpringCache自我學習

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