1. 程式人生 > 程式設計 >Spring Boot整合EhCache的步驟詳解

Spring Boot整合EhCache的步驟詳解

本文講解Spring Boot與EhCache的整合。

1 EhCache簡介

EhCache 是一個純Java的程序內快取框架,具有快速、精幹等特點,是Hibernate中預設CacheProvider。Ehcache是一種廣泛使用的開源Java分散式快取。主要面向通用快取,Java EE和輕量級容器。它具有記憶體和磁碟儲存,快取載入器,快取擴充套件,快取異常處理程式,一個gzip快取servlet過濾器,支援REST和SOAP api等特點。

2 Spring Boot整合EhCache步驟 2.1 建立專案,匯入依賴

<?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.yiidian</groupId>
 <artifactId>ch03_10_springboot_ehcache</artifactId>
 <version>1.0-SNAPSHOT</version>
 <!-- 匯入springboot父工程. 注意:任何的SpringBoot工程都必須有的!!! -->
 <!-- 父工程的作用:鎖定起步的依賴的版本號,並沒有真正到依賴 -->
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.11.RELEASE</version>
 </parent>
 <dependencies>
  <!--web起步依賴-->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--springboot 整合 junit 起步依賴-->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <version>2.1.6.RELEASE</version>
   <scope>test</scope>
  </dependency>
  <!-- 快取座標 -->
  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cache -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-cache</artifactId>
   <version>2.1.11.RELEASE</version>
  </dependency>
  <!-- Ehcache支援 -->
  <dependency>
   <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache</artifactId>
   <version>2.10.6</version>
  </dependency>

 </dependencies>
</project>

2.2 配置ehcache.xml

在resources目錄下建立ehcache.xml,內容如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
 <diskStore path="java.io.tmpdir"/>
 <!-- defaultCache: 預設配置 -->
 <defaultCache
   maxElementsInMemory="10000"
   eternal="false"
   timeToIdleSeconds="120"
   timeToLiveSeconds="120"
   maxElementsOnDisk="10000000"
   diskExpiryThreadIntervalSeconds="120"
   memoryStoreEvictionPolicy="LRU">
  <persistence strategy="localTempSwap"/>
 </defaultCache>
 <!-- 快取名稱為customer的配置 -->
 <cache name="customer"
   maxElementsInMemory="10000"
   eternal="false"
   timeToIdleSeconds="120"
   timeToLiveSeconds="120"
   maxElementsOnDisk="10000000"
   diskExpiryThreadIntervalSeconds="120"
   memoryStoreEvictionPolicy="LRU">
  <persistence strategy="localTempSwap"/>
 </cache>
</ehcache>

引數說明

  • name 快取名稱
  • maxElementsInMemory 快取最大個數
  • eternal 物件是否永久有效,一但設定了,timeout將不起作用
  • timeToIdleSeconds 設定物件在失效前的允許閒置時間(單位:秒)。僅當eternal=false物件不是永久有效時使用,可選屬性,預設值是0,也就是可閒置時間無窮大
  • timeToLiveSeconds 設定物件在失效前允許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。僅當eternal=false物件不是永久有效時使用,預設是0.,也就是物件存活時間無窮大
  • overflowToDisk 當記憶體中物件數量達到maxElementsInMemory時,Ehcache將會物件寫到磁碟中
  • diskSpoolBufferSizeMB 這個引數設定DiskStore(磁碟快取)的快取區大小。預設是30MB。每個Cache都應該有自己的一個緩衝區
  • maxElementsOnDisk 硬碟最大快取個數
  • diskPersistent 是否快取虛擬機器重啟期資料
  • diskExpiryThreadIntervalSeconds 磁碟失效執行緒執行時間間隔,預設是120秒。
  • memoryStoreEvictionPolicy 當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理記憶體。預設策略是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用)
  • clearOnFlush 記憶體數量最大時是否清除

2.3 編寫application.yml

#配置EhCache的配置
spring:
 cache:
 ehcache:
  config: ehcache.xml # 讀取ehcache.xml配置

2.4 編寫引導類

package com.yiidian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
/**
 * Spring Boot引導類
 * 一點教程網 - www.yiidian.com
 */
@SpringBootApplication
@EnableCaching // 開啟快取
public class MyBootApplication {
 public static void main(String[] args) {
  SpringApplication.run(MyBootApplication.class,args);
 }
}

引導類中需要新增@EnableCaching註解,開啟快取功能

2.5 編寫Service類

package com.yiidian.service;
import com.yiidian.domain.Customer;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
 * 業務層
 *一點教程網 - www.yiidian.com
 */
@Service
public class CustomerService {
  @Cacheable(value = "customer",key = "#id")
  public Customer findById(Integer id){
    System.out.println("執行了UserService獲取User");
    Customer customer = new Customer();
    customer.setId(1);
    customer.setName("小明");
    customer.setGender("男");
    customer.setTelephone("13244445555");
    return customer;
  }
}

@Cacheable的屬性:

  • value:對應ehcache.xml的快取配置名稱(name屬性值)
  • key:給快取值起個key,便於Spring內部檢索不同的快取資料。#id這個語法代表把方法形參作為key。

2.6 編寫測試類

package com.yiidian.test;

import com.yiidian.MyBootApplication;
import com.yiidian.service.CustomerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * SpringBoot整合EhCache
 * 一點教程網 - www.yiidian.com
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyBootApplication.class)
public class EhCacheDemo {
  @Autowired
  private CustomerService customerService;

  @Test
  public void test1(){
    //查詢第一次
    System.out.println(customerService.findById(1));
    //查詢第二次
    System.out.println(customerService.findById(1));
  }
}

2.7 執行測試

Spring Boot整合EhCache的步驟詳解

從結果可以看出,第一次呼叫Service的時候,到Service內部獲取資料。但是第二次呼叫Service時已經不需要從Service獲取資料,證明第一次查詢的時候已經把Customer物件快取到EhCache中。

3 EhCache常用註解 @Cacheable: 主要針對方法配置,能夠根據方法的請求引數對其進行快取 @CacheConfig: 統一配置本類的快取註解的屬性 @CachePut:保證方法被呼叫,又希望結果被快取。與@Cacheable區別在於是否每次都呼叫方法,常用於更新 @CacheEvict :清空快取 @Cacheable/@CachePut/@CacheEvict 主要的引數: value:快取的名稱,在 spring 配置檔案中定義,必須指定至少一個

例如:

@Cacheable(value=”mycache”) 或者

@Cacheable(value={”cache1”,”cache2”}

key:快取的 key,可以為空,如果指定要按照 SpEL 表示式編寫,

如果不指定,則預設按照方法的所有引數進行組合

例如:

@Cacheable(value=”testcache”,key=”#id”)

condition:快取的條件,可以為空,使用 SpEL 編寫,返回 true 或者 false,

只有為 true 才進行快取/清除快取

例如:@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

unless 否定快取。當條件結果為TRUE時,就不會快取。

@Cacheable(value=”testcache”,unless=”#userName.length()>2”)

allEntries

(@CacheEvict ): 是否清空所有快取內容,預設為 false,如果指定為 true,

則方法呼叫後將立即清空所有快取

例如:

@CachEvict(value=”testcache”,allEntries=true)

beforeInvocation

(@CacheEvict): 是否在方法執行前就清空,預設為 false,如果指定為 true,

則在方法還沒有執行的時候就清空快取,預設情況下,如果方法

執行丟擲異常,則不會清空快取

例如:

@CachEvict(value=”testcache”,beforeInvocation=true)

3.1 @Cacheable

@Cacheable註解會先查詢是否已經有快取,有會使用快取,沒有則會執行方法並快取。

@Cacheable(value = "customer",key = "targetClass + methodName +#p0")
public List<Customer> queryAll(Customer cust) {
  return customerDao.findAllByUid(cust);
}

3.2 @CacheConfig

當我們需要快取的地方越來越多,你可以使用@CacheConfig(cacheNames = {"myCache"})註解來統一指定value的值,這時可省略value,如果你在你的方法依舊寫上了value,那麼依然以方法的value值為準。

使用方法如下:

@CacheConfig(cacheNames = {"myCache"})
public class UserServiceImpl implements UserService {
  @Override
  @Cacheable(key = "targetClass + methodName +#p0")//此處沒寫value
  public List<BotRelation> findUsers(int num) {
    return userDao.findUsers(num);
  }
  .....
}

3.3 @CachePut

@CachePut註解的作用 主要針對方法配置,能夠根據方法的請求引數對其結果進行快取,和 @Cacheable 不同的是,它每次都會觸發真實方法的呼叫 。簡單來說就是使用者更新快取資料。但需要注意的是該註解的value 和 key 必須與要更新的快取相同,也就是與@Cacheable 相同。示例:

@CachePut(value = "customer",key = "targetClass + #p0")
public Customer updata(Customer cust) {
  Customer customer = customerDao.findAllById(cust.getId());
  customer.updata(cust);
  return customer ;
}

@Cacheable(value = "customer",key = "targetClass +#p0")//清空快取
public Customer save(Customer cust) {
  customerDao.save(cust);
  return cust;
}

3.4 @CacheEvict

@CachEvict 的作用 主要針對方法配置,能夠根據一定的條件對快取進行清空 。

@Cacheable(value = "customer",key = "#p0.id")
public Customer save(Customer cust) {
  customerDao.save(cust);
  return job;
}

//清除一條快取,key為要清空的資料
@CacheEvict(value="customer",key="#id")
public void delect(int id) {
  customerDao.deleteAllById(id);
}

//方法呼叫後清空所有快取
@CacheEvict(value="customerCache",allEntries=true)
public void delectAll() {
  customerDao.deleteAll();
}

//方法呼叫前清空所有快取
@CacheEvict(value="customerCache",beforeInvocation=true)
public void delectAll() {
  customerDao.deleteAll();
}

總結

以上所述是小編給大家介紹的Spring Boot整合EhCache的步驟詳解,希望對大家有所幫助!