亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

SpringBoot EhCaching

EhCache

EhCache是一種基于Java的開源緩存,用于提高性能。 Ehcache的當(dāng)前版本為 3 。它提供了 JSR-107 緩存管理器的實(shí)現(xiàn)。我們可以直接使用它。

EhCache的功能

快速輕巧,可擴(kuò)展靈活。 它允許我們執(zhí)行可序列化對(duì)象 它提供諸如 LRU,LFU,F(xiàn)IFO等的緩存逐出策略。 它將緩存存儲(chǔ)在內(nèi)存磁盤(SSD)中。 它依賴于 SLF4J 進(jìn)行記錄。 它已完全實(shí)現(xiàn) JSR-107 Jcache 它支持通過 JGroups JMS RMI 進(jìn)行分布式緩存。 它使用流利的查詢語言進(jìn)行分布式搜索。

EhCache使用模式

緩存使用多種訪問模式。 EhCache使用以下模式:

Cache-aside Cache-as-SoR (system-of-record) Read-through Write-through Write-behind

Cache-aside

備用緩存模式中,首先,應(yīng)用程序查詢緩存。如果找到數(shù)據(jù),它將直接返回?cái)?shù)據(jù)。在相反的情況下,它從SoR中獲取數(shù)據(jù),將其存儲(chǔ)到緩存中,然后返回。

Cache-as-SoR

cache-as-SoR 模式代表SoR對(duì)緩存的讀寫操作。它減少了應(yīng)用程序的責(zé)任。它使用讀寫模式的組合,包括 直讀,直寫,后寫。 它減少了應(yīng)用程序的難度。它允許緩存解決雷電問題

Read-through

Read-through模式還復(fù)制了緩存-從高速緩存中讀取數(shù)據(jù)時(shí)預(yù)留模式。讀取和緩存保留之間的區(qū)別在于,讀取模式實(shí)現(xiàn)了 CacheEntryFactory 接口。它指導(dǎo)緩存如何從緩存中讀取對(duì)象。最好在使用通讀模式時(shí)將EhCache實(shí)例包裝為 SelfPopulatingCache 實(shí)例。

Write-through

Write-through模式還可以在將數(shù)據(jù)寫入緩存時(shí)復(fù)制備用緩存模式。直寫模式和備用緩存模式之間的區(qū)別在于,直寫模式實(shí)現(xiàn) CacheWriter 接口。它為直寫和后寫模式配置高速緩存。它在同一執(zhí)行線程中將數(shù)據(jù)寫入SoR。

Write-behind

Write-behind模式的形式不同其他三種模式。在 可配置的延遲之后,它會(huì)修改緩存條目。延遲可能會(huì)在 秒,分鐘,一天,一周,很長時(shí)間。同時(shí),它還將數(shù)據(jù)排隊(duì),以便稍后在同一執(zhí)行線程中寫入。

使用后寫模式進(jìn)行數(shù)據(jù)寫入發(fā)生在事務(wù)范圍之外。這意味著它創(chuàng)建了一個(gè)新事務(wù)以在SoR中提交與主事務(wù)不同的數(shù)據(jù)。

EhCaching存儲(chǔ)層

EhCache允許我們使用各種數(shù)據(jù)存儲(chǔ)區(qū)域,例如堆,磁盤和群集。我們可以配置一個(gè)多存儲(chǔ)緩存(使用多個(gè)存儲(chǔ)區(qū)域)??梢詫⑵浒才艦?層。

這些層是按順序組織的。最底層是 授權(quán)層,另一層是 緩存層。也稱為 nearer near cache。 緩存層可以具有多個(gè)存儲(chǔ)區(qū)域。最熱的數(shù)據(jù)保留在緩存層中,因?yàn)樗仁跈?quán)層更快。與高速緩存層相比,其他數(shù)據(jù)保留在權(quán)限層中,速度較慢但較豐富。

EhCache支持的數(shù)據(jù)存儲(chǔ)類型有 四種:

On-Heap Store Off-Heap Store Disk Store Clustered Store

On-Heap Store

它將緩存條目存儲(chǔ)在Java堆內(nèi)存中。它與 Java 應(yīng)用程序共享存儲(chǔ)。這是快速的,因?yàn)樗褂枚?,但存?chǔ)空間有限。垃圾收集器還會(huì)掃描堆上存儲(chǔ)。

Off-Heap Store

它使用主內(nèi)存(RAM)來存儲(chǔ)緩存條目。垃圾收集器不會(huì)對(duì)其進(jìn)行掃描。它比堆上存儲(chǔ)慢,因?yàn)榫彺鏃l目在使用前移到了堆上存儲(chǔ)。它的大小受到限制。

Disk Store

它使用磁盤來存儲(chǔ)高速緩存條目。它比基于 RAM 的存儲(chǔ)(存儲(chǔ)上和存儲(chǔ)下)要慢得多。如果使用磁盤存儲(chǔ)模式,最好使用專用磁盤。

Clustered Store

它將緩存條目存儲(chǔ)在遠(yuǎn)程服務(wù)器上。它比堆外存儲(chǔ)慢。它可能具有提供高可用性的故障轉(zhuǎn)移服務(wù)器。

Spring Boot EhCaching

上圖顯示了

一個(gè)應(yīng)用程序可能具有多個(gè)緩存管理器。 許多緩存可以由緩存管理器處理。 緩存可以使用多個(gè)層來存儲(chǔ)緩存條目。 EhCache將最近使用或經(jīng)常使用的數(shù)據(jù)放在更快的層(緩存層)中。

配置EhCache

EhCache jar放在類路徑中。配置 xml 并將其放在類路徑中。創(chuàng)建一個(gè)引用緩存。

EhCache示例

在以下示例中,我們將在應(yīng)用程序中配置EhCache。

步驟1: 打開 Spring Initializr https://start.spring.io/。

步驟2: 選擇Spring Boot版本 2.3.0 M2 。

步驟3: 提供 Group名稱。我們提供了組名 com.nhooo。

步驟4: 提供 Artifact。我們提供了Artifact spring-boot-ehcache-example。

步驟5: 添加 Spring Web 依賴項(xiàng)。

步驟6: 單擊 Generate (生成)按鈕。當(dāng)我們單擊"生成"按鈕時(shí),它將與應(yīng)用程序相關(guān)的所有規(guī)范包裝到一個(gè) Jar 文件中,并將其下載到本地系統(tǒng)。

步驟7: 提取 jar文件。

步驟8: 復(fù)制文件夾并將其粘貼到STS工作區(qū)中。

第9步: 導(dǎo)入項(xiàng)目。

文件->導(dǎo)入->現(xiàn)有 Maven 項(xiàng)目->下一步->瀏覽->選擇文件夾spring-boot-ehcache-example->選擇文件夾->完成

導(dǎo)入項(xiàng)目需要時(shí)間。

步驟10: 從 Maven 存儲(chǔ)庫 https://mvnrepository.com/并將其粘貼到 pom.xml中文件。

spring-boot-starter-cache ehcache 3 cache API。

注意: 請(qǐng)勿使用包net.sf.ehcache的ehcache。

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.M2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nhooo</groupId>
<artifactId>spring-boot-ehcache-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-ehcache-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>

現(xiàn)在,我們需要配置 ehcache.xml 文件。

步驟11: 打開 application.properties 文件,并使用以下屬性配置EhCache。

application.properties

#配置 ehcache.xml
spring.cache.jcache.config=classpath:ehcache.xml

步驟12: 打開 SpringBootEhcacheExampleApplication.java 文件,并使用注解 @EnableCaching 啟用緩存。

SpringBootEhcacheExampleApplication.java

package com.nhooo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
//啟用緩存管理功能
@EnableCaching
public class SpringBootEhcacheExampleApplication 
{
    public static void main(String[] args) 
    {
        SpringApplication.run(SpringBootEhcacheExampleApplication.class, args);
    }
}
注意: 如果我們不想在主應(yīng)用程序文件中使用注解@EnableCaching,則可以創(chuàng)建一個(gè)單獨(dú)的CacheConfig類,并使用注解對(duì)調(diào)用進(jìn)行注解。
package com.nhooo;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
@Configuration
//enable caching
@EnableCaching
public class CacheConfig 
{
}

步驟13: 創(chuàng)建類。我們已經(jīng)在包 com.nhooo 中創(chuàng)建了名稱為 Student的類。 在類中,執(zhí)行以下操作:

創(chuàng)建五個(gè)變量id, name, gender, 生成使用Constructor
右鍵單擊文件->源->使用字段生成構(gòu)建器->全選->生成
生成Getters and Setters。
右鍵單擊文件->源->生成Getter和設(shè)置器->全選->生成
生成 toString() 右鍵單擊文件->源->生成toString()->生成

完成上述所有步驟后,類如下所示。

Student.java

package com.nhooo;
public class Student 
{
    private int id;
    private String name;
    private String gender;
    private String city;
    public Student(int id, String name, String gender, String city) 
    {
        super();
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.city = city;
    }
    public int getId() 
    {
    return id;
    }
    public void setId(int id) 
    {
    this.id = id;
    }
    public String getName() 
    {
    return name;
    }
    public void setName(String name) 
    {
    this.name = name;
    }
    public String getGender() 
    {
    return gender;
    }
    public void setGender(String gender) 
    {
    this.gender = gender;
    }
    public String getCity() 
    {
    return city;
    }
    public void setCity(String city) 
    {
    this.city = city;
    }
    @Override
    public String toString() 
    {
        return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", city=" + city + "]";
    }
}

步驟14: 創(chuàng)建用于管理學(xué)生的 服務(wù)類。我們已經(jīng)創(chuàng)建了名稱為 StudentManager的服務(wù)類。 在本課程中,我們完成了以下操作:

使用注解 @Service注解類。 創(chuàng)建 HashMap 的實(shí)例。 在靜態(tài)塊中,我們已在地圖中添加了學(xué)生數(shù)據(jù)。 通過使用注解 @Cacheable ,我們定義了緩存的名稱所有數(shù)據(jù)都將保存在此緩存中。我們已經(jīng)在注解的屬性中定義了 id 。緩存根據(jù) id 來搜索學(xué)生。 我們創(chuàng)建了一種方法 getStudentById(),該方法將id解析為參數(shù)。它返回學(xué)生的 id 。

StudentManager.java

StudentManager.java
package com.nhooo;
import java.util.HashMap;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class StudentManager 
{
    static HashMap<Integer, Student> student = new HashMap<>();
    static 
    {
        student.put(1, new Student(100, "Alex", "Male", "Berlin"));
        student.put(2, new Student(101, "Tony", "Male", "Maxico"));
        student.put(3, new Student(102, "Andrew", "Male", "Chicago"));
        student.put(4, new Student(103, "Alexa", "Female", "Brussels"));
        student.put(5, new Student(104, "Maria", "Female", "Houston"));
    }
    @Cacheable(cacheNames="demoCache", key="#id")public Student getStudentById(Integer id) 
    {
        System.out.println("Fetching student data from cache");
        return student.get(id);
    }
}

現(xiàn)在,我們需要?jiǎng)?chuàng)建 ehcache.xml 文件。它包含與高速緩存相關(guān)的信息,例如高速緩存的名稱,內(nèi)存中的元素?cái)?shù)量,高速緩存中的實(shí)時(shí)數(shù)據(jù)生存時(shí)間等。

第15步: src/main/resources 文件夾中緩存名為 ehcache.xml 的配置文件。

ehcahe.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'>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="2000" 
            eternal="true"
            overflowToDisk="false" 
            timeToLiveSeconds="1200" />
    <cache name="demoCache" 
            maxElementsInMemory="2000"
            eternal="false" 
            overflowToDisk="false" 
            timeToLiveSeconds="10000" />
</ehcache>
</config>

現(xiàn)在,我們已經(jīng)創(chuàng)建了所有必需的文件。創(chuàng)建所有文件后,項(xiàng)目目錄如下所示:

Spring Boot EhCaching

讓我們運(yùn)行該應(yīng)用程序。

步驟16: 打開 SpringBootEhcacheExampleApplication.java 文件,并以 Java 應(yīng)用程序。

它顯示以下輸出:

Getting Students from Cache
[id=100, name=Alex, gender=Male, city=Berlin]
[id=101, name=Tony, gender=Male, city=Mexico]
[id=102, name=Andrew, gender=Male, city=Chicago]
[id=103, name=Alexa, gender=Female, city=Brussels]
[id=104, name=Maria, gender=Female, city=Houston]