解释SpringBoot之Ehcache 2.x缓存

介绍

这里介绍Ehcache 2.X 缓存

添加基本的web项目

解释SpringBoot之Ehcache 2.x缓存

添加Ehcache 依赖

<?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.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 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.3.1.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

</project>

添加配置文件

添加ehcache 配置缓存文件
解释SpringBoot之Ehcache 2.x缓存

<ehcache>
    <diskStore path="java.io.tmpdir/shiro-spring-sample"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
    />
    <cache name="book_cache"
           maxElementsInMemory="10000"
           eternal="true"
           overflowToDisk="true"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="600"/>
</ehcache>

注解上开启缓存

在启动类上添加相关的注解

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

使用缓存

创建实体类和BookService

创建实体类

package com.example.demo;

import java.io.Serializable;

public class Book implements Serializable {
    private Integer id;
    private String name;
    private String author;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

解释SpringBoot之Ehcache 2.x缓存

添加相关的service

package com.example.demo;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

@Repository
// 使用缓存的名称
@CacheConfig(cacheNames = "book_cache")
public class BookDao {
    // 对该方法进行缓存
    @Cacheable
    public Book getBookById(Integer id){
        System.out.println("调用Service方法");
        Book book = new Book();
        book.setAuthor("ming");
        book.setId(id);
        book.setName("ming");
        return book;
    }

    @CachePut(key = "#book.id")
    // 用在更新方法上,数据库数据更新,缓存数据也要更新,方法的返回值自动更新在已经保存的key上
    public Book updateBookById(Book book){
        System.out.println("更新ById");
        book.setName("ming2");
        return book;
    }
    
    @CacheEvict(key = "#id")
    // 用在删除方法上,数据删除以后,其方法也会删除
    public void deleteBookById(Integer id){
        System.out.println("id删除");
    }
}

创建测试类

创建测试类,对Service方法进行测试

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(SpringExtension.class) //导入spring测试框架[2]
@SpringBootTest
        //提供spring依赖注入
class BookDaoTest {
    @Autowired
    private BookDao bookDao;
    @Test
    public void test(){
        bookDao.getBookById(1);
        bookDao.getBookById(1);
        bookDao.deleteBookById(1);
        Book book = bookDao.getBookById(1);
        System.out.println(book);
        book.setName("mingming");
        book.setAuthor("mingming");
        book.setId(1);
        bookDao.updateBookById(book);
        Book book1 = bookDao.getBookById(1);
        System.out.println(book1);
    }
}

查看结果

解释SpringBoot之Ehcache 2.x缓存

微信公众号

解释SpringBoot之Ehcache 2.x缓存

上一篇:一文了解Docker核心概念和安装配置


下一篇:Spring Security 基于数据库的认证