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

作者 : admin 本文共2363个字,预计阅读时间需要6分钟 发布时间: 2024-06-17 共1人阅读

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 拥有活跃的社区支持和持续的更新,保证了其稳定性和安全性。

导入依赖SpringBoot配置第三方专业缓存技术Ehcache插图


        
            net.sf.ehcache
            ehcache
            2.10.9.2
        
        

配置cache的类型

SpringBoot配置第三方专业缓存技术Ehcache插图(1)

SpringBoot配置第三方专业缓存技术Ehcache插图(2)

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的配置文件

包含了默认缓存存储策略

SpringBoot配置第三方专业缓存技术Ehcache插图(3)

SpringBoot配置第三方专业缓存技术Ehcache插图(4)



    


    

之前的程序不用改

我们仅仅是换了缓存的方式

进行测试

依然成立

SpringBoot配置第三方专业缓存技术Ehcache插图(5)

依赖



    4.0.0
    com.bigdata1421
    demo
    0.0.1-SNAPSHOT
    demo
    demo
    
        1.8
        UTF-8
        UTF-8
        2.6.13
    
    


        
            org.projectlombok
            lombok
        


        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.3
        


        
            com.alibaba
            druid-spring-boot-starter
            1.2.6
        


        
            mysql
            mysql-connector-java
        


        
            org.springframework.boot
            spring-boot-starter-web
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
            org.springframework.boot
            spring-boot-starter-cache
        


        
            net.sf.ehcache
            ehcache
            2.10.3
        

    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
                
                    com.bigdata1421.ssmp.SsmpApplication
                    <!--                    true-->
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    

我们已经做好了

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

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

要用不同的缓存策略

SpringBoot配置第三方专业缓存技术Ehcache插图(6)

SpringBoot配置第三方专业缓存技术Ehcache插图(7)

个人号推广

博客主页

多多!-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

本站无任何商业行为
个人在线分享-虚灵IT资料分享 » SpringBoot配置第三方专业缓存技术Ehcache
E-->