Spring Boot如何使用EhCache演示
一、EhCache使用演示
EhCache是一個純Java的進程內緩存框架,具有快速、精干等特點,Hibernate中的默認Cache就是使用的EhCache。
本章節(jié)示例是在Spring Boot集成Spring Cache的源碼基礎上進行改造。源碼地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache
使用EhCache作為緩存,我們先引入相關依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--ehcache依賴--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
然后創(chuàng)建EhCache的配置文件ehcache.xml。
<?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"> <!-- 磁盤存儲:將緩存中暫時不使用的對象,轉移到硬盤,類似于Windows系統(tǒng)的虛擬內存 path:指定在硬盤上存儲對象的路徑 path可以配置的目錄有: user.home(用戶的家目錄) user.dir(用戶當前的工作目錄) java.io.tmpdir(默認的臨時目錄) ehcache.disk.store.dir(ehcache的配置目錄) 絕對路徑(如:d:\\ehcache) 查看路徑方法:String tmpDir = System.getProperty("java.io.tmpdir"); --> <diskStore path="java.io.tmpdir" /> <!-- defaultCache:默認的緩存配置信息,如果不加特殊說明,則所有對象按照此配置項處理 maxElementsInMemory:設置了緩存的上限,最多存儲多少個記錄對象 eternal:代表對象是否永不過期 (指定true則下面兩項配置需為0無限期) timeToIdleSeconds:最大的發(fā)呆時間 /秒 timeToLiveSeconds:最大的存活時間 /秒 overflowToDisk:是否允許對象被寫入到磁盤 說明:下列配置自緩存建立起600秒(10分鐘)有效 。 在有效的600秒(10分鐘)內,如果連續(xù)120秒(2分鐘)未訪問緩存,則緩存失效。 就算有訪問,也只會存活600秒。 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" /> <cache name="cache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="600" overflowToDisk="true" /> </ehcache>
然后SpringBoot配置文件中,指明緩存類型并聲明Ehcache配置文件的位置。
server: port: 10900 spring: profiles: active: dev cache: type: ehcache ehcache: config: classpath:/ehcache.xml
這樣就可以開始使用Ehcache了,測試代碼與Spring Boot集成Spring Cache一致。
SpringBoot啟動類,@EnableCaching開啟Spring Cache緩存功能。
@EnableCaching @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { String tmpDir = System.getProperty("java.io.tmpdir"); System.out.println("臨時路徑:" + tmpDir); SpringApplication.run(SpringbootApplication.class, args); } }
CacheApi接口調用類,方便調用進行測試。
@RestController @RequestMapping("cache") public class CacheApi { @Autowired private CacheService cacheService; @GetMapping("get") public User get(@RequestParam int id){ return cacheService.get(id); } @PostMapping("set") public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){ User u = new User(code, name); return cacheService.set(id, u); } @DeleteMapping("del") public void del(@RequestParam int id){ cacheService.del(id); } }
CacheService緩存業(yè)務處理類,添加緩存,更新緩存和刪除。
@Slf4j @Service public class CacheService { private Map<Integer, User> dataMap = new HashMap <Integer, User>(){ { for (int i = 1; i < 100 ; i++) { User u = new User("code" + i, "name" + i); put(i, u); } } }; // 獲取數(shù)據 @Cacheable(value = "cache", key = "'user:' + #id") public User get(int id){ log.info("通過id{}查詢獲取", id); return dataMap.get(id); } // 更新數(shù)據 @CachePut(value = "cache", key = "'user:' + #id") public User set(int id, User u){ log.info("更新id{}數(shù)據", id); dataMap.put(id, u); return u; } //刪除數(shù)據 @CacheEvict(value = "cache", key = "'user:' + #id") public void del(int id){ log.info("刪除id{}數(shù)據", id); dataMap.remove(id); } }
源碼地址:https://github.com/imyanger/springboot-project/tree/master/p22-springboot-cache-ehcache
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
MyBatis-Plus+達夢數(shù)據庫實現(xiàn)高效數(shù)據持久化的示例
這篇文章主要介紹了MyBatis-Plus和達夢數(shù)據庫實現(xiàn)高效數(shù)據持久化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08SpringBoot整合WebService服務的實現(xiàn)代碼
WebService是一個SOA(面向服務的編程)的架構,它是不依賴于語言,不依賴于平臺,可以實現(xiàn)不同的語言間的相互調用,通過Internet進行基于Http協(xié)議的網絡應用間的交互,這篇文章主要介紹了SpringBoot整合WebService服務的實例代碼,需要的朋友可以參考下2022-02-02SpringBoot沒有讀取到application.yml問題及解決
這篇文章主要介紹了SpringBoot沒有讀取到application.yml問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12SpringBoot SpEL語法掃盲與查詢手冊的實現(xiàn)
這篇文章主要介紹了SpringBoot SpEL語法掃盲與查詢手冊的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05