当前位置: 首页 > wzjs >正文

网站类型定义全球最大的磁力搜索引擎

网站类型定义,全球最大的磁力搜索引擎,工商网上注册营业执照,政务公开及网站建设意见首先,要明确一点,就是EhCache2和EhCache3可以说是完全不同的两个框架了。EhCache2有自己的一套缓存标准API,springboot2的cache部分整合了该API。但是到了EhCache3,并没有自己的API,而是实现了JCache。springboot3中&a…

首先,要明确一点,就是EhCache2和EhCache3可以说是完全不同的两个框架了。EhCache2有自己的一套缓存标准API,springboot2的cache部分整合了该API。但是到了EhCache3,并没有自己的API,而是实现了JCache。springboot3中,也不再有EhCacheCacheManager这个可以自动注入的类了。

 Spring Boot 中使用 Ehcache 作为缓存时的 2 种配置方法,分别是 基于 XML 配置 和 基于编程式(代码)配置,下面详细说明:

一、基于 XML 配置(简单方案)

核心思路

通过在类路径下添加 ehcache.xml 配置文件,并在 Spring Boot 中指定该配置文件的位置,让框架自动加载并创建缓存。

具体步骤
  1. 添加依赖
    在 Maven 或 Gradle 中引入 Spring Boot 缓存启动器:

    <!-- Spring Boot 缓存启动器 -->
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
    </dependency><!-- Ehcache 3 与 JCache (JSR-107) 的集成依赖 -->
    <dependency><groupId>org.ehcache</groupId><artifactId>ehcache</artifactId>
    </dependency>
  2. 创建 ehcache.xml 配置文件
    在 src/main/resources 下添加该文件,定义缓存名称和策略:

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd"><!--    name="缓存名称"--><cache name="book"maxEntriesLocalHeap="1000"timeToLiveSeconds="300"eternal="false"memoryStoreEvictionPolicy="LRU"></cache></ehcache>
    1. 在 application.yml 中指定配置文件

      spring:cache:# springboot3中type不能写为ehcache,缓存类型不再是EhCache了,EhCache3是实现JCache的一种缓存type: jcachejcache:# xml配置文件地址config: classpath:ehcache.xml
      
    特点
    • 优点:配置集中、直观,适合策略固定的简单场景,无需编写代码。
    • 缺点:灵活性低,无法动态调整配置(如根据环境变量修改缓存大小)。

    二、基于编程式(代码)配置(灵活方案)

    核心思路

    保持和上面的依赖一致,通过 Java 代码手动创建 CacheManager Bean,定义缓存策略并注册到 Spring 容器中。

    代码实现(如用户提供的 CacheConfig.java
    @Configuration
    @EnableCaching
    public class CacheConfig {@Beanpublic CacheManager ehCacheManager() {// 1. 定义缓存配置(键类型、值类型、堆大小等)CacheConfiguration<SimpleKey, String> cacheConfig = CacheConfigurationBuilder.newCacheConfigurationBuilder(SimpleKey.class, String.class, ResourcePoolsBuilder.heap(10)).build();// 2. 获取 Ehcache 的 JCache 缓存管理器javax.cache.CacheManager cacheManager = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider").getCacheManager();// 3. 创建或替换名为 "myCache" 的缓存String cacheName = "myCache";cacheManager.destroyCache(cacheName);  // 先销毁旧缓存(若存在)cacheManager.createCache(cacheName, Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfig));// 4. 包装成 Spring 可用的缓存管理器return new JCacheCacheManager(cacheManager);}
    }
    

    特点
    • 优点:灵活性高,可在代码中动态调整配置(如根据条件设置不同的堆大小、淘汰策略),适合复杂场景。
    • 缺点:代码量稍多,配置逻辑分散在代码中,维护成本略高。

    三、两种方案的对比与选择

    维度XML 配置编程式配置
    实现方式写配置文件,框架自动加载写 Java 代码,手动创建缓存管理器
    灵活性低(静态配置)高(可动态调整策略)
    适用场景简单场景、策略固定不变复杂场景、需要动态调整缓存策略
    维护成本低(配置集中)中(代码逻辑分散)
    与 Spring Boot 版本的兼容性Spring Boot 2/3 均支持(需结合 JCache)Spring Boot 3 推荐(因 Ehcache 3 基于 JCache)

    四、注意事项

    1. 避免配置冲突
      不要同时使用 XML 和编程式配置同一名称的缓存(如同时在 ehcache.xml 和代码中定义 myCache),否则会导致冲突或覆盖。

    2. Spring Boot 3 的变化

      • Spring Boot 3 中,Ehcache 3 基于 JCache 规范,不再支持 Spring Boot 2 中的 EhCacheCacheManager,需通过 JCacheCacheManager 整合(如编程式配置中的实现)。
      • XML 配置时,需通过 spring.cache.jcache.config 指向配置文件,而非直接使用 Ehcache 2 的配置方式。
    3. 缓存未创建的异常
      无论使用哪种配置方式,必须先创建缓存(如 XML 中定义或代码中 createCache),否则 @Cacheable("myCache") 会因找不到缓存而抛出 IllegalArgumentException。

    错误示例:Ehcache -找不到Builder的缓存名称-腾讯云开发者社区-腾讯云

    在文章下面有这样一个回答:@Cacheable(value = "myCache")不会在Ehcache中创建名为myCache的缓存。在运行时,如果一个名为myCache的缓存在Ehcache中可用,它将使用该缓存进行缓存。如果没有,则在运行时尝试缓存时,将引发异常java.lang.IllegalArgumentException: Cannot find cache named 'myCache'。为了让@Cacheable(value = "myCache")使用Ehcache作为后端,需要在某个地方创建缓存,并且需要让Spring知道这个缓存。最简单的方法是包含spring-boot-starter-cache依赖项,将带有Ehcache配置的ehcache.xml添加到类路径中,并在application.yml中设置配置spring.cache.jcache.config: classpath:ehcache.xml。您可以在github上找到这样做的示例应用程序。

    相反,如果您确实希望以编程方式配置Ehcache,则需要一个org.springframework.cache.CacheManager bean来初始化Ehcache配置并将其链接到Spring。bean定义如下所示:

    import javax.cache.Caching;import org.ehcache.config.CacheConfiguration;
    import org.ehcache.config.builders.CacheConfigurationBuilder;
    import org.ehcache.config.builders.ResourcePoolsBuilder;
    import org.ehcache.jsr107.Eh107Configuration;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cache.interceptor.SimpleKey;
    import org.springframework.cache.jcache.JCacheCacheManager;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;@Configuration
    @EnableCaching
    public class CacheConfig {@Beanpublic CacheManager ehCacheManager() {CacheConfiguration<SimpleKey, String> cacheConfig = CacheConfigurationBuilder.newCacheConfigurationBuilder(SimpleKey.class, String.class, ResourcePoolsBuilder.heap(10)).build();javax.cache.CacheManager cacheManager = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider").getCacheManager();String cacheName = "myCache";cacheManager.destroyCache(cacheName);cacheManager.createCache(cacheName, Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfig));return new JCacheCacheManager(cacheManager);}
    }

    总结

    • 想快速上手、配置简单:选 XML 配置,通过 ehcache.xml 和 application.yml 完成。
    • 想灵活控制、动态配置:选编程式配置,通过代码自定义缓存策略。
    • Spring Boot 3 + Ehcache 3:推荐编程式配置,更符合 JCache 规范的整合方式。
    http://www.dtcms.com/wzjs/471822.html

    相关文章:

  3. 虎门英文网站建设seo怎么推广
  4. 苏州哪个公司做门户网站抖音关键词排名
  5. 开一个网站建设公司什么是网店推广
  6. 长沙好的网站建设公司哪家好小广告公司如何起步
  7. 威海设计网站的网站建设是什么工作
  8. 口碑好的网站建设加工即刻搜索引擎入口
  9. 吉安网站开发广西壮族自治区
  10. seo少女找seo外包公司需要注意什么
  11. 贵州企业网站建设设计线上推广100种方式
  12. 推广型网站如何建站外包公司软件开发
  13. 做网站seo优化服务营销的七个要素
  14. 自己怎么做外贸英文网站网络推广怎么做
  15. 高古楼网站找活做查询seo
  16. 怎么利用网站上的图片重庆森林粤语完整版在线观看免费
  17. seo网站提交提交百度推广开户费用标准
  18. 做的网站有广告泸州网站优化推广
  19. 深圳美容网站建企业seo网络营销
  20. 最好的餐饮设计网站建设如何注册域名
  21. 网站建设赚钱百度推广费用怎么算
  22. 百度优化只做移动网站没有pc站抖音广告推广
  23. 黔南seo衡水seo培训
  24. 酒店网站设计的毕业论文网站制作专业
  25. 舟山高端网站设计阜新网络推广
  26. 萍乡海绵城市建设官方网站网络营销有什么特点
  27. aspcms分类信息网站计算机培训
  28. 建设网站用什么时候开始域名注册人查询
  29. 汕头网页网站制作免费的关键词优化工具
  30. web前端网站开发实例短视频优化
  31. 电脑如何做网站空间在线培训app
  32. 外贸网站推广招聘专业软文平台