日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring-Boot——Cache

發布時間:2023/12/10 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-Boot——Cache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡單使用

1. maven 依賴

2. 開啟緩存配置

在啟動類上開啟緩存 @EnableCaching

3. 使用緩存

@Cacheable 是將方法的返回值保存到緩存中
@CachePut 是根據key更新緩存中的數據
@CacheEvict 是根據key刪除緩存數據

@Cacheable(cacheNames = {"emp"}, key = "#id") public Employee getEmp(Integer id) {Employee employeeId = employeeMapper.getEmployeeId(id);return employeeId; } @CachePut(cacheNames = {"emp"}, key = "#result.id") public Employee updateEmp(Employee employee) {employeeMapper.updateEmp(employee);return employee; } @CacheEvict(cacheNames = {"emp"}, key = "#id") public boolean delete(Integer id) {employeeMapper.delete(id);return true; }

自動配置原理

1. 默認的緩存配置器

* 直接搜索類 ```CacheAutoConfiguration``` 找到 ```CacheConfigurationImportSelector.selectImports```方法,該方法會返回所有的自動配置類: ```org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration``` `org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration` `org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration` `org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration` `org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration` `org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration` `org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration` `org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration` `org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration` `org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration` * 在 `application.yml` 中 設置 `debug: true`,在控制臺可以看到默認使用的自動配置類 ``` SimpleCacheConfiguration matched:- Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration automatic cache type (CacheCondition)- @ConditionalOnMissingBean (types: org.springframework.cache.CacheManager; SearchStrategy: all) did not find any beans (OnBeanCondition) ```

2. @Cacheable 不能的key不能使用result,因為@Cacheable在方法執行前調用的。

3. @CachePut 可以使用result中的數據,因為@CachePut在方法執行后調用。

  • @CacheEvict 默認在方法執行之后執行(如果方法執行出錯,將不會執行),可以通過 beforeInvocation=true 設置為在方法之前執行。
  • 轉載于:https://www.cnblogs.com/Godfunc/p/9316563.html

    總結

    以上是生活随笔為你收集整理的Spring-Boot——Cache的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。