Springboot整合整合ehcache3

來源:互聯網
上載者:User

1.pom.xml中添加依賴。spring-boot-starer-cache中已經有了ehcache和cache-api,可以不用再添加。

<!-- 本機快取依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- <dependency><groupId>org.ehcache</groupId><artifactId>ehcache</artifactId></dependency><dependency><groupId>javax.cache</groupId><artifactId>cache-api</artifactId></dependency> -->

2. application.properties檔案中添加配置位置。指定使用的cacheManger是jcache。

#-------------------ehcache----------------------spring.cache.jcache.config=classpath:/config/ehcache.xml

3. 在配置的目錄中建立添加ecache.xml檔案。更詳細的可以參照官方文檔

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://www.ehcache.org/v3"    xmlns:jsr107="http://www.ehcache.org/v3/jsr107"    xsi:schemaLocation="http://www.ehcache.org/v3                         http://www.ehcache.org/schema/ehcache-core-3.2.xsd                        http://www.ehcache.org/v3/jsr107                        http://www.ehcache.org/schema/ehcache-107-ext-3.2.xsd"><persistence directory="java.io.tmpdir"/><cache-template name="default">    <resources>      <heap unit="entries">2000</heap>       <offheap unit="MB">100</offheap>     </resources>    </cache-template>  <cache alias="SSAccountInfo" uses-template="default">    <expiry>      <ttl unit="seconds">600</ttl>    </expiry>  </cache> </config>

3.在配置類或者啟動類上面,添加啟用緩衝的註解

@EnableCaching

4 . 在需要緩衝的資料方法上面打上緩衝註解。幾個常用的緩衝註解可參照官網。

package com.caiwufei.module.cfsss.dao;import org.springframework.cache.annotation.Cacheable;import com.caiwufei.entity.base.BaseMapper;import com.caiwufei.entity.cfsss.SSAccountInfo;public interface SSAccountInfoDao extends BaseMapper<SSAccountInfo>{@Cacheable(cacheNames="SSAccountInfo", unless="#result==null")public SSAccountInfo queryAccountInfoById(SSAccountInfo a);}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.