本地緩存其實(shí)項(xiàng)目中用得還比較多,常用的ehcache,現(xiàn)整合sPRing-boot搭建一個(gè)demo方便以后查閱。
下面2個(gè)依賴(lài)是spring-boot整合ehcache必須的依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${spring-boot.version}</version></dependency><dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.3</version></dependency>如果啟用spring-boot單元測(cè)試,還需要加入下面的依賴(lài)。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${spring-boot.version}</version> <scope>test</scope></dependency>application.properties中指明ehcache配置文件
spring.cache.jcache.config=ehcache.xmlehcache.xml配置ehcache緩存,具體配置項(xiàng)的意義請(qǐng)參考。 http://blog.csdn.net/clj198606061111/article/details/41121437
<?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"> <!--超過(guò)大小后持久化磁盤(pán)位置--> <diskStore path="java.io.tmpdir/ehcache"/> <!-- default cache --> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"/> <!--自定義緩存配置--> <cache name="countries" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"> </cache></ehcache>在spring-boot啟動(dòng)類(lèi)加上@EnableCaching注解,表明該項(xiàng)目啟用緩存。
使用緩存其實(shí)很簡(jiǎn)單,只需要在緩存的方法上加上@Cacheable注解即可,@CacheEvict、@CachePut等緩存相關(guān)注解另行查詢(xún)其他資料。
原文地址:http://www.itclj.com/blog/58bd2a4447508f786718d4f4 項(xiàng)目地址:https://github.com/clj198606061111/spring-boot-ehcache-demo
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注