溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot如何整合ehcache實(shí)現(xiàn)緩存機(jī)制

發(fā)布時(shí)間:2021-07-08 11:14:38 來(lái)源:億速云 閱讀:237 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹springboot如何整合ehcache實(shí)現(xiàn)緩存機(jī)制,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

EhCache 是一個(gè)純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn),是Hibernate中默認(rèn)的CacheProvider。

ehcache提供了多種緩存策略,主要分為內(nèi)存和磁盤兩級(jí),所以無(wú)需擔(dān)心容量問(wèn)題。

spring-boot是一個(gè)快速的集成框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開(kāi)發(fā)人員不再需要定義樣板化的配置。

由于spring-boot無(wú)需任何樣板化的配置文件,所以spring-boot集成一些其他框架時(shí)會(huì)有略微的不同。

1.spring-boot是一個(gè)通過(guò)maven管理的jar包的框架,集成ehcache需要的依賴如下

 <dependency>
  <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
     <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache</artifactId>
     <version>2.8.3</version>
</dependency>

具體pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lclc.boot</groupId>
  <artifactId>boot-cache</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- Inherit defaults from Spring Boot -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.3.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>17.0</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.8.3</version>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>spring-snapshots</id>
      <url>http://repo.spring.io/snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>spring-milestones</id>
      <url>http://repo.spring.io/milestone</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>spring-snapshots</id>
      <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
      <id>spring-milestones</id>
      <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
  </pluginRepositories>
</project>

2.使用ehcache,我們需要一個(gè)ehcache.xml來(lái)定義一些cache的屬性。

<?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">
     <diskStore path="java.io.tmpdir/Tmp_EhCache" />
      <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"
  timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />

      <cache name="demo" eternal="false" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
  timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" />

</ehcache>

解釋下這個(gè)xml文件中的標(biāo)簽。

(1).diskStore: 為緩存路徑,ehcache分為內(nèi)存和磁盤兩級(jí),此屬性定義磁盤的緩存位置。參數(shù)解釋如下:    

  1. user.home – 用戶主目錄

  2. user.dir  – 用戶當(dāng)前工作目錄

  3. java.io.tmpdir – 默認(rèn)臨時(shí)文件路徑

(2).defaultCache:默認(rèn)緩存策略,當(dāng)ehcache找不到定義的緩存時(shí),則使用這個(gè)緩存策略。只能定義一個(gè)。

 (3).cache:自定緩存策略,為自定義的緩存策略。參數(shù)解釋如下:

  1. cache元素的屬性:   

  2. name:緩存名稱                  

  3. maxElementsInMemory:內(nèi)存中最大緩存對(duì)象數(shù)                  

  4. maxElementsOnDisk:硬盤中最大緩存對(duì)象數(shù),若是0表示無(wú)窮大                  

  5. eternal:true表示對(duì)象永不過(guò)期,此時(shí)會(huì)忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認(rèn)為false                

  6. overflowToDisk:true表示當(dāng)內(nèi)存緩存的對(duì)象數(shù)目達(dá)到了maxElementsInMemory界限后,會(huì)把溢出的對(duì)象寫到硬盤緩存中。注意:如果緩存的對(duì)象要寫入到硬盤中的話,則該對(duì)象必須實(shí)現(xiàn)了Serializable接口才行。                  

  7. diskSpoolBufferSizeMB:磁盤緩存區(qū)大小,默認(rèn)為30MB。每個(gè)Cache都應(yīng)該有自己的一個(gè)緩存區(qū)。               

  8. diskPersistent:是否緩存虛擬機(jī)重啟期數(shù)據(jù)                  

  9. diskExpiryThreadIntervalSeconds:磁盤失效線程運(yùn)行時(shí)間間隔,默認(rèn)為120秒     

  10. timeToIdleSeconds: 設(shè)定允許對(duì)象處于空閑狀態(tài)的最長(zhǎng)時(shí)間,以秒為單位。當(dāng)對(duì)象自從最近一次被訪問(wèn)后,如果處于空閑狀態(tài)的時(shí)間超過(guò)了timeToIdleSeconds屬性值,這個(gè)對(duì)象就會(huì)過(guò)期,EHCache將把它從緩存中清空。只有當(dāng)eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對(duì)象可以無(wú)限期地處于空閑狀態(tài)                  

  11. timeToLiveSeconds:設(shè)定對(duì)象允許存在于緩存中的最長(zhǎng)時(shí)間,以秒為單位。當(dāng)對(duì)象自從被存放到緩存中后,如果處于緩存中的時(shí)間超過(guò)了 timeToLiveSeconds屬性值,這個(gè)對(duì)象就會(huì)過(guò)期,EHCache將把它從緩存中清除。只有當(dāng)eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對(duì)象可以無(wú)限期地存在于緩存中。timeToLiveSeconds必須大于timeToIdleSeconds屬性,才有意義     

  12. memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時(shí),Ehcache將會(huì)根據(jù)指定的策略去清理內(nèi)存??蛇x策略有:LRU(最近最少使用,默認(rèn)策略)、FIFO(先進(jìn)先出)、LFU(最少訪問(wèn)次數(shù))。  

SpringBoot支持很多種緩存方式:redis、guava、ehcahe、jcache等等。

 說(shuō)明下redis和ehcache的區(qū)別:

Redis:屬于獨(dú)立的運(yùn)行程序,需要單獨(dú)安裝后,使用Java中的Jedis來(lái)操縱。因?yàn)樗仟?dú)立,所以如果你寫個(gè)單元測(cè)試程序,放一些數(shù)據(jù)在Redis中,然后又寫一個(gè)程序去拿數(shù)據(jù),那么是可以拿到這個(gè)數(shù)據(jù)的。,
ehcache:與Redis明顯不同,它與java程序是綁在一起的,java程序活著,它就活著。譬如,寫一個(gè)獨(dú)立程序放數(shù)據(jù),再寫一個(gè)獨(dú)立程序拿數(shù)據(jù),那么是拿不到數(shù)據(jù)的。只能在獨(dú)立程序中才能拿到數(shù)據(jù)。

3.將ehcache的管理器暴露給spring的上下文容器,

@Configuration
// 標(biāo)注啟動(dòng)了緩存
@EnableCaching
public class CacheConfiguration {

  /*
   * ehcache 主要的管理器
   */
  @Bean(name = "appEhCacheCacheManager")
  public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
    return new EhCacheCacheManager (bean.getObject ());
  }

  /*
   * 據(jù)shared與否的設(shè)置,Spring分別通過(guò)CacheManager.create()或new CacheManager()方式來(lái)創(chuàng)建一個(gè)ehcache基地.
   */
  @Bean
  public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
    EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
    cacheManagerFactoryBean.setConfigLocation (new ClassPathResource ("conf/ehcache-app.xml"));
    cacheManagerFactoryBean.setShared (true);
    return cacheManagerFactoryBean;
  }
}

@Configuration:為spring-boot注解,主要標(biāo)注此為配置類,優(yōu)先掃描。

@Bean:向spring容器中加入bean。

至此所有的配置都做好了,通過(guò)spring-boot進(jìn)行集成框架就是這么簡(jiǎn)單。

4.使用ehcache

使用ehcache主要通過(guò)spring的緩存機(jī)制,上面我們將spring的緩存機(jī)制使用了ehcache進(jìn)行實(shí)現(xiàn),所以使用方面就完全使用spring緩存機(jī)制就行了。

具體牽扯到幾個(gè)注解:

@Cacheable:負(fù)責(zé)將方法的返回值加入到緩存中,參數(shù)3
@CacheEvict:負(fù)責(zé)清除緩存,參數(shù)4

參數(shù)解釋:

  1. value:緩存位置名稱,不能為空,如果使用EHCache,就是ehcache.xml中聲明的cache的name

  2. key:緩存的key,默認(rèn)為空,既表示使用方法的參數(shù)類型及參數(shù)值作為key,支持SpEL

  3. condition:觸發(fā)條件,只有滿足條件的情況才會(huì)加入緩存,默認(rèn)為空,既表示全部都加入緩存,支持SpEL

  4. allEntries:CacheEvict參數(shù),true表示清除value中的全部緩存,默認(rèn)為false

不多說(shuō),直接上代碼:

@Service
public class CacheDemoServiceImpl implements CacheDemoService {
  /**
   * 緩存的key
   */
  public static final String THING_ALL_KEY  = "\"thing_all\"";
  /**
   * value屬性表示使用哪個(gè)緩存策略,緩存策略在ehcache.xml
   */
  public static final String DEMO_CACHE_NAME = "demo";  
  @CacheEvict(value = DEMO_CACHE_NAME,key = THING_ALL_KEY)
  @Override
  public void create(Thing thing){
    Long id = getNextId ();
    thing.setId (id);
    data.put (id, thing);
  } 
   
   @Cacheable(value = DEMO_CACHE_NAME,key = "#thing.getId()+'thing'")
  @Override
  public Thing findById(Long id){
    System.err.println ("沒(méi)有走緩存!" + id);
    return data.get (id);
  }

  @Cacheable(value = DEMO_CACHE_NAME,key = THING_ALL_KEY)
  @Override
  public List<Thing> findAll(){
    return Lists.newArrayList (data.values ());
  }
   
  @Override
  @CachePut(value = DEMO_CACHE_NAME,key = "#thing.getId()+'thing'")
  @CacheEvict(value = DEMO_CACHE_NAME,key = THING_ALL_KEY)
  public Thing update(Thing thing){
    System.out.println (thing);
    data.put (thing.getId (), thing);
    return thing;
  }

  @CacheEvict(value = DEMO_CACHE_NAME)
  @Override
  public void delete(Long id){
    data.remove (id);
  }  
}

 5.只需要通過(guò)注解在service層方法上打注解便可以使用緩存,在find**上存入緩存,在delete**,update**上清除緩存。

Cache注解詳解

@CacheConfig:主要用于配置該類中會(huì)用到的一些共用的緩存配置。在這里@CacheConfig(cacheNames = "users"):配置了該數(shù)據(jù)訪問(wèn)對(duì)象中返回的內(nèi)容將存儲(chǔ)于名為users的緩存對(duì)象中,我們也可以不使用該注解,直接通過(guò)@Cacheable自己配置緩存集的名字來(lái)定義。

@Cacheable:配置了findByName函數(shù)的返回值將被加入緩存。同時(shí)在查詢時(shí),會(huì)先從緩存中獲取,若不存在才再發(fā)起對(duì)數(shù)據(jù)庫(kù)的訪問(wèn)。該注解主要有下面幾個(gè)參數(shù):

  1. value、cacheNames:兩個(gè)等同的參數(shù)(cacheNames為Spring 4新增,作為value的別名),用于指定緩存存儲(chǔ)的集合名。由于Spring 4中新增了@CacheConfig,因此在Spring 3中原本必須有的value屬性,也成為非必需項(xiàng)了

  2. key:緩存對(duì)象存儲(chǔ)在Map集合中的key值,非必需,缺省按照函數(shù)的所有參數(shù)組合作為key值,若自己配置需使用SpEL表達(dá)式,比如:@Cacheable(key = "#p0"):使用函數(shù)第一個(gè)參數(shù)作為緩存的key值,更多關(guān)于SpEL表達(dá)式的詳細(xì)內(nèi)容可參考官方文檔

  3. condition:緩存對(duì)象的條件,非必需,也需使用SpEL表達(dá)式,只有滿足表達(dá)式條件的內(nèi)容才會(huì)被緩存,比如:@Cacheable(key = "#p0", condition = "#p0.length() < 3"),表示只有當(dāng)?shù)谝粋€(gè)參數(shù)的長(zhǎng)度小于3的時(shí)候才會(huì)被緩存,若做此配置上面的AAA用戶就不會(huì)被緩存,讀者可自行實(shí)驗(yàn)嘗試。

  4. unless:另外一個(gè)緩存條件參數(shù),非必需,需使用SpEL表達(dá)式。它不同于condition參數(shù)的地方在于它的判斷時(shí)機(jī),該條件是在函數(shù)被調(diào)用之后才做判斷的,所以它可以通過(guò)對(duì)result進(jìn)行判斷。

  5. keyGenerator:用于指定key生成器,非必需。若需要指定一個(gè)自定義的key生成器,我們需要去實(shí)現(xiàn)org.springframework.cache.interceptor.KeyGenerator接口,并使用該參數(shù)來(lái)指定。需要注意的是:該參數(shù)與key是互斥的

  6. cacheManager:用于指定使用哪個(gè)緩存管理器,非必需。只有當(dāng)有多個(gè)時(shí)才需要使用

  7. cacheResolver:用于指定使用那個(gè)緩存解析器,非必需。需通過(guò)org.springframework.cache.interceptor.CacheResolver接口來(lái)實(shí)現(xiàn)自己的緩存解析器,并用該參數(shù)指定。

除了這里用到的兩個(gè)注解之外,還有下面幾個(gè)核心注解:

@CachePut:配置于函數(shù)上,能夠根據(jù)參數(shù)定義條件來(lái)進(jìn)行緩存,它與@Cacheable不同的是,它每次都會(huì)真是調(diào)用函數(shù),所以主要用于數(shù)據(jù)新增和修改操作上。它的參數(shù)與@Cacheable類似,具體功能可參考上面對(duì)@Cacheable參數(shù)的解析

@CacheEvict:配置于函數(shù)上,通常用在刪除方法上,用來(lái)從緩存中移除相應(yīng)數(shù)據(jù)。除了同@Cacheable一樣的參數(shù)之外,它還有下面兩個(gè)參數(shù):

  1.  allEntries:非必需,默認(rèn)為false。當(dāng)為true時(shí),會(huì)移除所有數(shù)據(jù)

  2. beforeInvocation:非必需,默認(rèn)為false,會(huì)在調(diào)用方法之后移除數(shù)據(jù)。當(dāng)為true時(shí),會(huì)在調(diào)用方法之前移除數(shù)據(jù)。

以上是“springboot如何整合ehcache實(shí)現(xiàn)緩存機(jī)制”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI