最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot整合EhCache的步驟詳解

 更新時間:2020年02月09日 10:53:39   作者:eric  
EhCache 是一個純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn),是Hibernate中默認(rèn)CacheProvider。這篇文章主要介紹了Spring Boot整合EhCache的步驟詳解,需要的朋友可以參考下

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

1 EhCache簡介

EhCache 是一個純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn),是Hibernate中默認(rèn)CacheProvider。Ehcache是一種廣泛使用的開源Java分布式緩存。主要面向通用緩存,Java EE和輕量級容器。它具有內(nèi)存和磁盤存儲,緩存加載器,緩存擴(kuò)展,緩存異常處理程序,一個gzip緩存servlet過濾器,支持REST和SOAP api等特點(diǎn)。

2 Spring Boot整合EhCache步驟 2.1 創(chuàng)建項(xiàng)目,導(dǎo)入依賴

<?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>
 <!-- 導(dǎo)入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>
  <!-- 緩存坐標(biāo) -->
  <!-- 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,內(nèi)容如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
 <diskStore path="java.io.tmpdir"/>
 <!-- defaultCache: 默認(rèn)配置 -->
 <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>

參數(shù)說明

  • name 緩存名稱
  • maxElementsInMemory 緩存最大個數(shù)
  • eternal 對象是否永久有效,一但設(shè)置了,timeout將不起作用
  • timeToIdleSeconds 設(shè)置對象在失效前的允許閑置時間(單位:秒)。僅當(dāng)eternal=false對象不是永久有效時使用,可選屬性,默認(rèn)值是0,也就是可閑置時間無窮大
  • timeToLiveSeconds 設(shè)置對象在失效前允許存活時間(單位:秒)。最大時間介于創(chuàng)建時間和失效時間之間。僅當(dāng)eternal=false對象不是永久有效時使用,默認(rèn)是0.,也就是對象存活時間無窮大
  • overflowToDisk 當(dāng)內(nèi)存中對象數(shù)量達(dá)到maxElementsInMemory時,Ehcache將會對象寫到磁盤中
  • diskSpoolBufferSizeMB 這個參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認(rèn)是30MB。每個Cache都應(yīng)該有自己的一個緩沖區(qū)
  • maxElementsOnDisk 硬盤最大緩存?zhèn)€數(shù)
  • diskPersistent 是否緩存虛擬機(jī)重啟期數(shù)據(jù)
  • diskExpiryThreadIntervalSeconds 磁盤失效線程運(yùn)行時間間隔,默認(rèn)是120秒。
  • memoryStoreEvictionPolicy 當(dāng)達(dá)到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存。默認(rèn)策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進(jìn)先出)或是LFU(較少使用)
  • clearOnFlush 內(nèi)存數(shù)量最大時是否清除

2.3 編寫application.yml

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

2.4 編寫引導(dǎo)類

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引導(dǎo)類
 * 一點(diǎn)教程網(wǎng) - www.yiidian.com
 */
@SpringBootApplication
@EnableCaching // 開啟緩存
public class MyBootApplication {
 public static void main(String[] args) {
  SpringApplication.run(MyBootApplication.class,args);
 }
}

引導(dǎo)類中需要添加@EnableCaching注解,開啟緩存功能

2.5 編寫Service類

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

@Cacheable的屬性:

  • value:對應(yīng)ehcache.xml的緩存配置名稱(name屬性值)
  • key:給緩存值起個key,便于Spring內(nèi)部檢索不同的緩存數(shù)據(jù)。#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
 * 一點(diǎn)教程網(wǎng) - 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 運(yùn)行測試

從結(jié)果可以看出,第一次調(diào)用Service的時候,到Service內(nèi)部獲取數(shù)據(jù)。但是第二次調(diào)用Service時已經(jīng)不需要從Service獲取數(shù)據(jù),證明第一次查詢的時候已經(jīng)把Customer對象緩存到EhCache中。

3 EhCache常用注解 @Cacheable: 主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其進(jìn)行緩存 @CacheConfig: 統(tǒng)一配置本類的緩存注解的屬性 @CachePut:保證方法被調(diào)用,又希望結(jié)果被緩存。與@Cacheable區(qū)別在于是否每次都調(diào)用方法,常用于更新 @CacheEvict :清空緩存 @Cacheable/@CachePut/@CacheEvict 主要的參數(shù): value:緩存的名稱,在 spring 配置文件中定義,必須指定至少一個

例如:

@Cacheable(value=”mycache”) 或者

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

key:緩存的 key,可以為空,如果指定要按照 SpEL 表達(dá)式編寫,

如果不指定,則缺省按照方法的所有參數(shù)進(jìn)行組合

例如:

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

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

只有為 true 才進(jìn)行緩存/清除緩存

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

unless 否定緩存。當(dāng)條件結(jié)果為TRUE時,就不會緩存。

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

allEntries

(@CacheEvict ): 是否清空所有緩存內(nèi)容,缺省為 false,如果指定為 true,

則方法調(diào)用后將立即清空所有緩存

例如:

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

beforeInvocation

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

則在方法還沒有執(zhí)行的時候就清空緩存,缺省情況下,如果方法

執(zhí)行拋出異常,則不會清空緩存

例如:

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

3.1 @Cacheable

@Cacheable注解會先查詢是否已經(jīng)有緩存,有會使用緩存,沒有則會執(zhí)行方法并緩存。

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

3.2 @CacheConfig

當(dāng)我們需要緩存的地方越來越多,你可以使用@CacheConfig(cacheNames = {"myCache"})注解來統(tǒng)一指定value的值,這時可省略value,如果你在你的方法依舊寫上了value,那么依然以方法的value值為準(zhǔn)。

使用方法如下:

@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注解的作用 主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對其結(jié)果進(jìn)行緩存,和 @Cacheable 不同的是,它每次都會觸發(fā)真實(shí)方法的調(diào)用 。簡單來說就是用戶更新緩存數(shù)據(jù)。但需要注意的是該注解的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 的作用 主要針對方法配置,能夠根據(jù)一定的條件對緩存進(jìn)行清空 。

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

//清除一條緩存,key為要清空的數(shù)據(jù)
@CacheEvict(value="customer",key="#id")
public void delect(int id) {
  customerDao.deleteAllById(id);
}

//方法調(diào)用后清空所有緩存
@CacheEvict(value="customerCache",allEntries=true)
public void delectAll() {
  customerDao.deleteAll();
}

//方法調(diào)用前清空所有緩存
@CacheEvict(value="customerCache",beforeInvocation=true)
public void delectAll() {
  customerDao.deleteAll();
}

總結(jié)

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

相關(guān)文章

  • Mybatis?Lombok使用方法與復(fù)雜查詢介紹

    Mybatis?Lombok使用方法與復(fù)雜查詢介紹

    Lombok是一種Java實(shí)用工具,可用來幫助開發(fā)人員消除Java的冗長,尤其是對于簡單的Java對象(POJO),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • Java8中Stream?API的peek()方法詳解及需要注意的坑

    Java8中Stream?API的peek()方法詳解及需要注意的坑

    這篇文章主要給大家介紹了關(guān)于Java8中Stream?API的peek()方法詳解及需要注意的坑,Java 中的 peek 方法是 Java 8 中的 Stream API 中的一個方法,它屬于中間操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • Java的中l(wèi)ombok下的@Builder注解用法詳解

    Java的中l(wèi)ombok下的@Builder注解用法詳解

    這篇文章主要介紹了Java的中l(wèi)ombok下的@Builder注解用法詳解,lombok注解在java進(jìn)行編譯時進(jìn)行代碼的構(gòu)建,對于java對象的創(chuàng)建工作它可以更優(yōu)雅,不需要寫多余的重復(fù)的代碼,在出現(xiàn)lombok之后,對象的創(chuàng)建工作更提供Builder方法,需要的朋友可以參考下
    2023-11-11
  • Java 發(fā)送http請求上傳文件功能實(shí)例

    Java 發(fā)送http請求上傳文件功能實(shí)例

    本文通過實(shí)例代碼給大家介紹了Java 發(fā)送http請求上傳文件功能,需要的朋友參考下吧
    2017-06-06
  • Spring的Bean容器介紹

    Spring的Bean容器介紹

    今天小編就為大家分享一篇關(guān)于Spring的Bean容器介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • SpringBoot異步方法捕捉異常詳解

    SpringBoot異步方法捕捉異常詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot異步方法捕捉異常,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Mybatis的插件運(yùn)行原理及如何編寫一個插件

    Mybatis的插件運(yùn)行原理及如何編寫一個插件

    這篇文章主要介紹了Mybatis的插件運(yùn)行原理及如何編寫一個插件 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • 一文解決springboot打包成jar文件無法正常運(yùn)行的問題

    一文解決springboot打包成jar文件無法正常運(yùn)行的問題

    這篇文章主要介紹了一文解決springboot打包成jar文件無法正常運(yùn)行的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 詳解Spring boot使用Redis集群替換mybatis二級緩存

    詳解Spring boot使用Redis集群替換mybatis二級緩存

    本篇文章主要介紹了詳解Spring boot使用Redis集群替換mybatis二級緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Spring mvc攔截器實(shí)現(xiàn)原理解析

    Spring mvc攔截器實(shí)現(xiàn)原理解析

    這篇文章主要介紹了Spring mvc攔截器實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03

最新評論

高唐县| 会昌县| 凤凰县| 博野县| 玛沁县| 资源县| 绥阳县| 兴隆县| 承德市| 五台县| 睢宁县| 聂拉木县| 大余县| 沂水县| 黎平县| 元江| 仙游县| 南川市| 鄂伦春自治旗| 磐石市| 吉木萨尔县| 新野县| 肇庆市| 台湾省| 青海省| 长葛市| 临武县| 宁德市| 南漳县| 台东县| 梁河县| 稻城县| 始兴县| 石柱| 南川市| 金堂县| 津南区| 盐池县| 连平县| 汤原县| 延吉市|