国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

spring-boot-ehcache-demo

2019-11-06 06:33:36
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本地緩存其實(shí)項(xiàng)目中用得還比較多,常用的ehcache,現(xiàn)整合sPRing-boot搭建一個(gè)demo方便以后查閱。

依賴(lài)包

下面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.xml

ehcache.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)目啟用緩存。

@EnableCaching@EnableScheduling@SpringBootApplicationpublic class ItcljApplication { public static void main(String[] args) { new SpringApplicationBuilder().sources(ItcljApplication.class) .profiles("app").run(args); }}

使用緩存

使用緩存其實(shí)很簡(jiǎn)單,只需要在緩存的方法上加上@Cacheable注解即可,@CacheEvict@CachePut等緩存相關(guān)注解另行查詢(xún)其他資料。

@Component@CacheConfig(cacheNames = "countries")public class CountryRepository { @Cacheable(key = "'country'+#code") public Country findByCode(String code) { System.out.println("---> Loading country with code '" + code + "'"); return new Country(code); }}

原文地址:http://www.itclj.com/blog/58bd2a4447508f786718d4f4 項(xiàng)目地址:https://github.com/clj198606061111/spring-boot-ehcache-demo


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沙河市| 海城市| 苗栗市| 铁力市| 共和县| 克拉玛依市| 腾冲县| 崇左市| 井陉县| 静海县| 东台市| 永和县| 勃利县| 天津市| 新野县| 班戈县| 呼伦贝尔市| 山东省| 商洛市| 峨眉山市| 吕梁市| 承德市| 汽车| 津南区| 高台县| 久治县| 交口县| 大石桥市| 二手房| 津市市| 哈密市| 广州市| 南召县| 河西区| 阜南县| 荆门市| 鄱阳县| 安岳县| 策勒县| 新泰市| 盘锦市|