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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-17 08:26:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-17 08:26:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-17 08:26:03       82 阅读
  4. Python语言-面向对象

    2024-06-17 08:26:03       91 阅读

热门阅读

  1. electron录制工具-视频保存、编辑页面

    2024-06-17 08:26:03       28 阅读
  2. Leetcode 3187. Peaks in Array

    2024-06-17 08:26:03       28 阅读
  3. CentOS下的miniconda3安装

    2024-06-17 08:26:03       20 阅读
  4. jvm工具-jps、jstat、jmap、jstack

    2024-06-17 08:26:03       26 阅读
  5. DAG(有向无环图)-入门基础

    2024-06-17 08:26:03       29 阅读
  6. PostgresSQL测评

    2024-06-17 08:26:03       31 阅读
  7. 苹果新型基于home app的骚扰

    2024-06-17 08:26:03       25 阅读