SpringBoot手動使用EhCache的方法樣本,springbootehcache

來源:互聯網
上載者:User

SpringBoot手動使用EhCache的方法樣本,springbootehcache

SpringBoot在annotation的層面實現了資料緩衝的功能,基於Spring的AOP技術。所有的緩衝配置只是在annotation層面配置,像聲明式事務一樣。

Spring定義了CacheManager和Cache介面統一不同的緩衝技術。其中CacheManager是Spring提供的各種緩衝技術的抽象介面。而Cache介面包含緩衝的各種操作。

CacheManger

針對不同的緩衝技術,需要實現不同的cacheManager,Spring定義了如下的cacheManger實現。

CacheManger 描述
SimpleCacheManager 使用簡單的Collection來儲存緩衝,主要用於測試
ConcurrentMapCacheManager 使用ConcurrentMap作為緩衝技術(預設)
NoOpCacheManager 測試用
EhCacheCacheManager 使用EhCache作為緩衝技術,以前在hibernate的時候經常用
GuavaCacheManager 使用google guava的GuavaCache作為緩衝技術
HazelcastCacheManager 使用Hazelcast作為緩衝技術
JCacheCacheManager 使用JCache標準的實現作為緩衝技術,如Apache Commons JCS
RedisCacheManager 使用Redis作為緩衝技術

常規的SpringBoot已經為我們自動設定了EhCache、Collection、Guava、ConcurrentMap等緩衝,預設使用ConcurrentMapCacheManager。SpringBoot的application.properties設定檔,使用spring.cache首碼的屬性進行配置。

application配置

spring.cache.type=#緩衝的技術類型spring.cache.cache-names=應用程式啟動建立緩衝的名稱spring.cache.ehcache.config=ehcache的設定檔位置spring.cache.infinispan.config=infinispan的設定檔位置spring.cache.jcache.config=jcache設定檔位置spring.cache.jcache.provider=當多個jcache實作類別時,指定選擇jcache的實作類別

入口類配置

加入註解 @EnableCaching

緩衝註解

註解 描述
@Cacheable 在調用方法之前,首先應該在緩衝中尋找方法的傳回值,如果這個值能夠找到,就會返回緩衝的值。否則,這個方法就會被調用,傳回值會放到緩衝之中。
@CachePut 將方法的傳回值放到緩衝中。在方法的調用前並不會檢查緩衝,方法始終都會被調用。
@CacheEvict 在緩衝中清除一個或多個條目。
@Caching 分組的註解,能夠同時應用多個其他的緩衝註解。

手動使用EhCache

在實際開發過程中,存在不使用註解,需要自己添加緩衝的情況。下面就以Ehcache為例,簡單寫一下配置過程。

1. 添加依賴

引入springboot-cache和ehcache。需要注意,EhCache不需要配置version,SpringBoot的根pom已經整合了。

  <!-- 緩衝 -->  <dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-cache</artifactId>  </dependency>  <!-- ehcache -->  <dependency>   <groupId>net.sf.ehcache</groupId>   <artifactId>ehcache</artifactId>  </dependency>

2. 入口類配置

加入註解 @EnableCaching

@SpringBootApplication@EnableCachingpublic class DemoApplication {}

3. EhCache配置

在src\main\resources目錄下,添加ehcache.xml檔案,內容見文末。

4. application.application配置

# 配置ehcache緩衝spring.cache.type=ehcache# 指定ehcache設定檔路徑spring.cache.ehcache.config=classpath:/ehcache.xml

5. 使用Cache

注入SpringBoot自動設定的bean,org.springframework.cache.CacheManager。

一個簡單的測試類別:

package com.bbf.frame.test;import com.bbf.frame.Application;import org.apache.commons.lang3.StringUtils;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.cache.Cache;import org.springframework.cache.CacheManager;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import javax.annotation.Resource;@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)public class TestCache { @Resource private CacheManager cacheManager; @Test public void cacheTest() {  // 顯示所有的Cache空間  System.out.println(StringUtils.join(cacheManager.getCacheNames(), ","));  Cache cache = cacheManager.getCache("userCache");  cache.put("key", "123");  System.out.println("緩衝成功");  String res = cache.get("key", String.class);  System.out.println(res); }}

附錄 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"> <!-- 指定一個檔案目錄,當EHCache把資料寫到硬碟上時,將把資料寫到這個檔案目錄下 --> <diskStore path = "java.io.tmpdir"/> <!-- 預設的管理原則 --> <defaultCache   eternal = "false"   maxElementsInMemory = "10000"   overflowToDisk = "true"   diskPersistent = "false"   timeToIdleSeconds = "120"   timeToLiveSeconds = "120"   diskExpiryThreadIntervalSeconds = "120"   memoryStoreEvictionPolicy = "LRU"/> <!-- 此緩衝最多可以存活timeToLiveSeconds秒,如果期間超過timeToIdleSeconds秒未訪問,緩衝失效 --> <cache   name = "userCache"   eternal = "false"   maxElementsInMemory = "100"   overflowToDisk = "false"   diskPersistent = "false"   timeToIdleSeconds = "120"   timeToLiveSeconds = "180"   memoryStoreEvictionPolicy = "LRU"/> <!-- maxElementsInMemory 記憶體中最大緩衝對象數,看著自己的heap大小來搞 --> <!-- eternal:true表示對象永不到期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,預設為false --> <!-- maxElementsOnDisk:硬碟中最大緩衝對象數,若是0表示無窮大 --> <!-- overflowToDisk:true表示當記憶體緩衝的對象數目達到了maxElementsInMemory界限後, 會把溢出的對象寫到硬碟緩衝中。注意:如果緩衝的對象要寫入到硬碟中的話,則該對象必須實現了Serializable介面才行。--> <!-- diskSpoolBufferSizeMB:磁碟緩衝區大小,預設為30MB。每個Cache都應該有自己的一個緩衝區。--> <!-- diskPersistent:是否緩衝虛擬機器重啟期資料 --> <!-- diskExpiryThreadIntervalSeconds:磁碟失效線程已耗用時間間隔,預設為120秒 --> <!-- timeToIdleSeconds: 設定允許對象處於空閑狀態的最長時間,以秒為單位。當對象自從最近一次被訪問後, 如果處於空閑狀態的時間超過了timeToIdleSeconds屬性值,這個對象就會到期, EHCache將把它從緩衝中清空。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0, 則表示對象可以無限期地處於空閑狀態 --> <!-- timeToLiveSeconds:設定對象允許存在於緩衝中的最長時間,以秒為單位。當對象自從被存放到緩衝中後, 如果處於緩衝中的時間超過了 timeToLiveSeconds屬性值,這個對象就會到期, EHCache將把它從緩衝中清除。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0, 則表示對象可以無限期地存在於緩衝中。timeToLiveSeconds必須大於timeToIdleSeconds屬性,才有意義 --> <!-- memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時, Ehcache將會根據指定的策略去清理記憶體。可選策略有:LRU(最近最少使用,預設策略)、 FIFO(先進先出)、LFU(最少訪問次數)。--></ehcache>

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.