SpringBoot配置第三方专业缓存技术Ehcache

Ehcache缓存技术

我们刚才是用Springboot提供的默认缓存技术

我们用的是simple

是一个内存级的缓存

我们接下来要使用专业的缓存技术了

Ehcache 是一个流行的开源 Java 分布式缓存,由 Terracotta 公司开发和维护。它提供了一个快速、可扩展、易于集成的内存缓存解决方案,常被用于提升应用程序的性能和扩展性。Ehcache 最初是作为一个基于 Java 的进程内缓存而设计的,后来也支持了分布式缓存的特性。

Ehcache 的特点和优势:

  1. 快速和高效:Ehcache 是一个轻量级的缓存解决方案,对于缓存数据的读取和写入操作非常快速,能够显著提升应用程序的响应速度。

  2. 可扩展性:Ehcache 提供了分布式缓存的支持,可以水平扩展到多个节点,以处理大量数据和高并发访问。

  3. 灵活的缓存策略:Ehcache 支持多种缓存策略,包括基于时间过期、LRU(最近最少使用)、LFU(最不经常使用)等,开发者可以根据具体需求配置和调整缓存策略。

  4. Spring Framework 的集成 :Ehcache 能够与 Spring Framework 很好地集成,通过 Spring 的缓存抽象提供了对 Ehcache 的便捷使用方式,例如通过 @Cacheable@CachePut@CacheEvict 等注解实现缓存功能。

  5. 监控和管理:Ehcache 提供了丰富的监控和管理功能,可以通过 JMX 或者 Ehcache 的管理界面来监控缓存的状态和性能指标。

  6. 开源和活跃的社区:作为一个开源项目,Ehcache 拥有活跃的社区支持和持续的更新,保证了其稳定性和安全性。

导入依赖

<!--        Ehcache-->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.9.2</version>
        </dependency>

配置cache的类型

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
      username: root
      password: 123456
  devtools:
    restart:
      # 设置不参与热部署的文件或文件夹
      exclude: static/**,public/**,config/application.yml
  cache:
    type: ehcache

Ehcache是一个spring框架外的技术

我们用依赖引入

我们还要手写配置

这是一个ehcache.xml的配置文件

包含了默认缓存存储策略

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/encache.xsd"
         updateCheck="false">
    <diskStroe path="D:\encache"/>


    <defaultcache
        eternal="false"
        disPersistent="false"
        maxElementsInMemory="1000"
        overflowToDisk="false"
        timeToldleSeconds="60"
        timeToliveSeconds="60"
        memorystoreEvictionPolicy="LRU"/>

</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>
    <groupId>com.bigdata1421</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>

<!--        lombok 快速封装实体类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

<!--        mybatis-plus 快速数据层开发-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>

<!--        druid数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.6</version>
        </dependency>

<!--        mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

<!--        web应用内嵌服务器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

<!--        springboot起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

<!--        缓存的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

<!--                Ehcache-->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.3</version>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.bigdata1421.ssmp.SsmpApplication</mainClass>
                    <!--                    <skip>true</skip>-->
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

我们已经做好了

然而我们也可以修改Ehcache配置来修改

不同的数据模型缓存的策略不同

要用不同的缓存策略

个人号推广

博客主页

多多!-CSDN博客

Web后端开发

https://blog.csdn.net/qq_30500575/category_12624592.html?spm=1001.2014.3001.5482

Web前端开发

https://blog.csdn.net/qq_30500575/category_12642989.html?spm=1001.2014.3001.5482

数据库开发

https://blog.csdn.net/qq_30500575/category_12651993.html?spm=1001.2014.3001.5482

项目实战

https://blog.csdn.net/qq_30500575/category_12699801.html?spm=1001.2014.3001.5482

算法与数据结构

https://blog.csdn.net/qq_30500575/category_12630954.html?spm=1001.2014.3001.5482

计算机基础

https://blog.csdn.net/qq_30500575/category_12701605.html?spm=1001.2014.3001.5482

回忆录

https://blog.csdn.net/qq_30500575/category_12620276.html?spm=1001.2014.3001.5482

相关推荐
启山智软15 分钟前
Java微服务商城系统的特点有哪些
java·大数据·开发语言·人工智能·微服务·架构·ux
korgs36 分钟前
从软件架构设计角度理解Kafka
分布式·微服务·中间件·kafka
程序员大金1 小时前
基于SpringBoot+Vue+MySQL的教学资料管理系统
java·vue.js·spring boot·后端·mysql·tomcat·intellij-idea
2401_854391082 小时前
Spring Boot影院管理系统:小徐的创新
服务器·数据库·spring boot
Swxctx2 小时前
Go进阶概览 -【7.3 Go语言中的安全与错误处理】
开发语言·后端·golang·go进阶概览
夜月行者2 小时前
如何使用ssm实现大学生校园招聘网的设计与实现
java·后端·ssm
coffee_baby2 小时前
解释器模式原理剖析和Spring中的应用
java·spring boot·spring·解释器模式
忽晚♪‎( ᷇࿀ ᷆ و(و2 小时前
Redis
数据库·redis·缓存
开源神器2 小时前
Spring Boot 入门指南
spring boot
武昌库里写JAVA2 小时前
刚面试完的前端面试题
java·spring boot·mysql·spring·log4j