網站首頁 編程語言 正文
區別于redis的分布式緩存,ehcache是純java進程內的單機緩存,根據不同的場景可選擇使用,以下內容主要為springboot整合ehcache以及注意事項
添加pom引用
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.9.2</version> </dependency>
啟動類添加開啟緩存注解:@EnableCaching
添加xml配置,注意,ehcache需要單獨的配置文件
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <!--默認緩存策略 --> <!-- external:是否永久存在,設置為true則不會被清除,此時與timeout沖突,通常設置為false--> <!-- diskPersistent:是否啟用磁盤持久化--> <!-- maxElementsInMemory:最大緩存數量--> <!-- overflowToDisk:超過最大緩存數量是否持久化到磁盤--> <!-- timeToIdleSeconds:最大不活動間隔,設置過長緩存容易溢出,設置過短無效果,可用于記錄時效性數據,例如驗證碼--> <!-- timeToLiveSeconds:最大存活時間--> <!-- memoryStoreEvictionPolicy:緩存清除策略--> <defaultCache eternal="false" diskPersistent="false" maxElementsInMemory="1000" overflowToDisk="false" timeToIdleSeconds="60" timeToLiveSeconds="60" memoryStoreEvictionPolicy="LRU" /> <cache name="cache1" eternal="false" diskPersistent="false" maxElementsInMemory="1000" overflowToDisk="false" timeToIdleSeconds="2" timeToLiveSeconds="2" memoryStoreEvictionPolicy="LRU" /> </ehcache>
這里我定義了一個緩存名字為cache1
修改項目的配置文件application.properties,添加spring緩存類型以及緩存配置文件路徑
spring.cache.ehcache.config=classpath:ehcache.xml
spring.cache.type=ehcache
上面的步驟做好之后,就可以使用了
給你需要加緩存的方法添加注解
@Configuration
public class TestConfig {
@Cacheable(value = "cache1",key = "#id")
public TestController.Person create(String id) {
return new TestController.Person();
}
}
這里的value跟xml配置文件里的一致即可
我們調用一下測試看看
@GetMapping("/testCache1")
public void testCache1(@Param("id") String id) throws InterruptedException {
Person obj1 = testConfig.create(id);
Person obj2 = testConfig.create(id);
Thread.sleep(3000);
Person obj3 = testConfig.create(id);
Person obj4 = testConfig.create(id);
log.info("test1:"+obj1.toString());
log.info("test2:"+obj2.toString());
log.info("test3:"+obj3.toString());
log.info("test4:"+obj4.toString());
System.out.println(obj1.equals(obj2));
}
執行一下結果看
可以看到,obj1跟obj2是同一個對象,當程序睡眠了三秒之后,再次調用方法,就會重新創建對象,緩存生效
注意事項:
@Cacheable修飾的方法必須是public并且不能是static,原理是因為使用了動態代理,需要重寫方法
xml里面的配置要寫全,要不然項目啟動報錯,就是下圖這些
xml里面配置的defaultCache沒看出有啥用,我也沒刪了試試
使用緩存的方法不能在@RestController修飾的類中,即不能在controller層,要不然緩存失效,可以在@Service、@Configuratin、@Component等類下面
原文鏈接:https://blog.csdn.net/cxclll/article/details/128974928
相關推薦
- 2022-11-29 redis配置文件詳解
- 2022-12-29 React引入css的三種方式小結_React
- 2022-06-13 nginx?location/區別詳解_nginx
- 2022-07-19 Redis中pop出隊列多個元素思考_實用技巧
- 2022-07-22 SpringBoot允許跨域訪問配置
- 2022-11-12 sass在react中的基本使用(實例詳解)_React
- 2022-12-29 Python?Setuptools的?setup.py實例詳解_python
- 2022-11-21 詳解如何使用Python實現刪除重復文件_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支