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