ehcache 相关问题

Ehcache是​​一个开源的,符合标准的基于Java的缓存,用于提高性能,卸载数据库和简化可伸缩性。

如果 Guava 缓存绑定到 MeterRegistry,EhCache 指标不会出现在 Prometheus 中

隐式创建的EhCache缓存 我有一个 Spring 组件,它使用具有以下配置的文件 ehcache.xml 声明一些缓存: 隐式创建的EhCache缓存 我有一个 Spring 组件,它使用具有以下配置的文件 ehcache.xml 声明一些缓存: <?xml version="1.0" encoding="UTF-8"?> <eh:config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:eh="http://www.ehcache.org/v3" xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.3.xsd"> <eh:cache alias="Cache1"> <eh:expiry> <eh:ttl unit="minutes">1</eh:ttl> </eh:expiry> <eh:resources> <eh:heap>10000</eh:heap> </eh:resources> </eh:cache> <!-- [...] --> </eh:config> 然后使用类似于以下内容: import org.springframework.cache.annotation.Cacheable; public class ThingDoer { @Cacheable("Cache1") public Integer doSomethingCached(int value) { return value + 3; } // ... } 这样,我可以看到 Prometheus 中出现不同的缓存指标: bash> 2>&1 curl -v --silent 'http://localhost:9000/actuator/prometheus' | grep cache_gets | sort cache_gets_total{cache="Cache1",cache_manager="cacheManager",hostname="localhost",name="Cache1",result="hit",} 0.0 cache_gets_total{cache="Cache1",cache_manager="cacheManager",hostname="localhost",name="Cache1",result="miss",} 0.0 cache_gets_total{cache="Cache2",cache_manager="cacheManager",hostname="localhost",name="Cache2",result="hit",} 0.0 cache_gets_total{cache="Cache2",cache_manager="cacheManager",hostname="localhost",name="Cache2",result="miss",} 0.0 cache_gets_total{cache="Cache3",cache_manager="cacheManager",hostname="localhost",name="Cache3",result="hit",} 0.0 cache_gets_total{cache="Cache3",cache_manager="cacheManager",hostname="localhost",name="Cache3",result="miss",} 0.0 [...] 我在文件中定义的所有缓存ehcache.xml都会自动公开它们的指标。 添加自制缓存 现在,我创建了一个自制的缓存,它在查询时执行复杂的操作,并为此使用了 com.google.common.cache.LoadingCache 接口,因为它的目的是替换此类缓存的另一个实例,并对其余代码进行尽可能少的更改。因此,我创建了一个自定义缓存,具有以下签名: import com.google.common.cache.AbstractLoadingCache; public class CustomCache extends AbstractLoadingCache<String, Object> { public CustomCache(Function<String, Object> valueLoader) { // ... } // ... } 它正确声明了一个 stats() 方法,该方法返回包含缓存统计信息的 com.google.common.cache.CacheStats。其构造函数的参数 valueLoader 用于在缓存中缺少新值时加载新值。 现在,这是如何使用此缓存的草图: import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.function.Function; public class CustomThingDoer { private final CustomCache customCache; public CustomThingDoer() { this.customCache = new CustomCache(this::loadNewObject); } public Optional<Object> doSomethingCached(String value) { try { return Optional.of(customCache.get(value)); } catch (ExecutionException e) { System.out.println("Something wrong happened"); return Optional.empty(); } } private Object loadNewObject(String value) { // Do something non-static } } 由于缓存旨在直接调用其方法的地方使用(即没有 @Cacheable 注释来使用此缓存,而是使用对 get 的调用),并且由于加载缺失值的方法取决于在定义它的类的实例上(即,所讨论的方法是提取数据库,其代码取决于CustomThingDoer实例中定义的内容),我希望将其手动注册到MeterRegistry中。 为此,我将 MeterRegistry 的 bean 传递给构造函数中的 CustomThingDoer,并执行以下操作: GuavaCacheMetrics.monitor(meterRegistry, this.customCache, "MyCustomCache"); 消失的缓存指标 但是现在这已经完成了,这就是我在 Prometheus 中获得的缓存指标: bash> 2>&1 curl -v --silent 'http://localhost:9000/actuator/prometheus' | grep cache_gets | sort cache_gets_total{cache="MyCustomCache",hostname="localhost",result="hit",} 0.0 cache_gets_total{cache="MyCustomCache",hostname="localhost",result="miss",} 0.0 现在,仅显示我的自定义缓存的指标。此外,字段 cache_manager 不存在。 我的怀疑是,由于我手动注册缓存,一些谓词被 automagic 破坏了,并且它永远不会将自动生成的 EhCache 缓存添加到 MeterRegistry。 我想要同时拥有自定义缓存的指标以及隐式创建的 EhCache 缓存的指标。如果可能的话,我希望不必更改所使用的接口(即`com.google.common.cache.LoadingCache),以便对代码(并且老实说,对单元测试)进行最少的更改。有人知道问题可能是什么,或者我可能有什么解决方案吗? 这似乎与 Micrometer 中的一个长期错误有关:https://github.com/micrometer-metrics/micrometer/issues/877。 它在几年前就被修复了,但它仍然有副作用,其中每个指标名称必须具有相同的标签键集,否则有些不被考虑在内。 https://github.com/micrometer-metrics/micrometer/issues/877#issuecomment-944894069

回答 1 投票 0

Java 17 + Spring boot 3 迁移 - EhCache 与 Hazelcast 冲突

我正在将我的应用程序从 Java 8 和 Spring Boot 2.7.18 迁移到 Java 17 和 Spring Boot 3.2.3。 我正在尝试迁移 EhCache(我使用此链接进行迁移,因为我之前的配置适合“old&

回答 1 投票 0

Spring boot应用中如何在应用启动时缓存数据

我有一个连接到 SQL Server 数据库的 Spring boot 应用程序。我需要一些帮助来在我的应用程序中使用缓存。我有一个 CodeCategory 表,其中包含许多代码的代码列表。这个

回答 5 投票 0

EhCache - 过期元素不会被驱逐

我在 Spring Boot 应用程序中使用 EHCache 2.9,并将缓存配置为在 300 秒(5 分钟)后过期。 当我运行应用程序并第一次请求元素时......

回答 1 投票 0

Spring 4 Ehcache 3 Hibernate 5 缓存默认模板设置

我的项目是Spring 4.3.4,Hibernate 5.2.4和Ehcache 3.3 我正在寻找一个具有单一 JSR-107 (JCache) CacheManager 的解决方案,为整个应用程序提供: Spring 缓存 - ...

回答 2 投票 0

Intellij 错误:内部缓存已损坏或格式过时

当我在 Intellij 中构建 Java 项目时,我间歇性地收到错误。 内部缓存已损坏或格式过时,迫使项目重建:java.io.FileNotFoundException:C:ar...

回答 0 投票 0

ehcache 二级缓存因不可写而失败

我正在尝试使用 Hibernate 将 ehcache 配置为二级缓存,但似乎它没有按预期工作。当我清除一级缓存(PersistenceContext)并尝试找到...

回答 1 投票 0

Kryo 序列化器比 EHCache 中的默认序列化器消耗更多空间

我尝试使用 Kryo 序列化程序将 Employee 对象保存到 EHCache 的堆外层,但它比我尝试不使用 Kryo 序列化程序保存它时占用的空间更多...

回答 1 投票 0

在java中使用cacheManager清除缓存

我想使用JAVA代码清除缓存。 为了这个目标我写了这段代码: 公共无效清除缓存(){ CacheManager.getInstance().clearAll(); } 这段代码正确吗? 在那里...

回答 4 投票 0

Hibernate 5.3 Spring 5,Ehcache 3.5.2,jdk 10,Hibernate 说“缓存提供程序未启动”

...我的问题是,使用这个配置,我需要“启动”ehcache吗?如果是,怎么办?这是一个通过库依赖项工作的迷宫,例如需要 hibernate-ehcache 和 ehcache 吗?需要休眠-...

回答 3 投票 0

在 kubernetes 上使用 jgroups 复制设置 ehcache

我正在尝试在 kubernetes 中使用 ehcache 和 jgroups 复制来设置一个小型 springboot 应用程序,但不知何故无法发现其他成员来形成集群。引导程序请求

回答 2 投票 0

Spring boot 3 ehcache 配置设置问题

我正在尝试在 Spring boot 应用程序中设置 ehcache。我有以下 ehcache.xml 和配置文件: 我正在尝试在我的 Spring boot 应用程序中设置 ehcache。我有以下 ehcache.xml 和配置文件: <?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.ehcache.org/v3'> <cache alias="myCache"> <expiry> <ttl unit="minutes">60</ttl> </expiry> <heap>20</heap> </cache> </config> import org.ehcache.jsr107.EhcacheCachingProvider; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.jcache.JCacheCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.cache.CacheManager; import javax.cache.Caching; import java.net.URI; import java.net.URISyntaxException; @Configuration @EnableCaching public class CacheConfig { @Bean public JCacheCacheManager cacheManager() throws URISyntaxException { ClassLoader classLoader = getClass().getClassLoader(); URI ehcacheURI = classLoader.getResource("ehcache.xml").toURI(); CacheManager cacheManager = Caching.getCachingProvider(EhcacheCachingProvider.class.getName()).getCacheManager(ehcacheURI, classLoader); return new JCacheCacheManager(cacheManager); } } 但是,当我启动应用程序时,我收到以下错误消息: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [com/webservice/config/CacheConfig.class]: Failed to instantiate [org.springframework.cache.jcache.JCacheCacheManager]: Factory method 'cacheManager' threw exception with message: javax/xml/bind/ValidationEventHandler at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:652) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:488) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1324) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1161) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:561) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:961) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:915) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) at com.webservice.Application.main(Application.java:15) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.jcache.JCacheCacheManager]: Factory method 'cacheManager' threw exception with message: javax/xml/bind/ValidationEventHandler at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:648) ... 22 common frames omitted Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/ValidationEventHandler at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:114) at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:90) at org.ehcache.jsr107.EhcacheCachingProvider$ConfigSupplier.getConfiguration(EhcacheCachingProvider.java:328) at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:134) at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:85) at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:194) at com.webservice.config.CacheConfig.cacheManager(CacheConfig.java:29) at com.webservice.config.CacheConfig$$SpringCGLIB$$0.CGLIB$cacheManager$0(<generated>) at com.webservice.config.CacheConfig$$SpringCGLIB$$2.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:257) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) at com.webservice.config.CacheConfig$$SpringCGLIB$$0.cacheManager(<generated>) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ... 23 common frames omitted Caused by: java.lang.ClassNotFoundException: javax.xml.bind.ValidationEventHandler at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ... 40 common frames omitted 有人知道我做错了什么吗?看起来它与验证有关。所以我假设我的 ehcache.xml 文件可能不正确。我正在使用 org.ehcache.ehcache 版本 3.10.2。任何帮助将不胜感激。 您错过了一些有关验证的依赖项,请在项目中添加以下依赖项 <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.0</version> <scope>compile</scope> </dependency> https://groups.google.com/g/ehcache-users/c/sKfxWuTpY-U implementation('org.ehcache:ehcache:3.10.8') { capabilities { requireCapability('org.ehcache:ehcache-jakarta') } }

回答 2 投票 0

CacheManager 未找到 Bean - 未尝试设置任何缓存

我找不到 CacheManager bean...但我还没有尝试使用 CacheManager 做任何事情! 这是我的错误! org.springframework.beans.factory.BeanCreationException:使用

回答 7 投票 0

hibernate v6,缓存 | XmlConfigurationException:解析 XML 配置时出错

我有一个正在工作的 hibernate v6 项目,想要添加缓存,但我无法让它工作。 我收到此错误(减少) 线程“main”中的异常 java.lang.IllegalStateException:缓存pr...

回答 1 投票 0

如何获取有关 Spring Boot 应用程序中使用哪个缓存管理器的信息?

在 Spring Boot 应用程序中,即使我完全放弃声明特定的缓存管理器,使用 @Cacheable 注释也可以工作并提供缓存条目。所以我假设,有...

回答 2 投票 0

CacheEventListener 不起作用 Ehcache

我正在尝试记录来自 ehcache 的事件,在日志中我看到了缓存、缓存管理器的创建,但没有看到自定义事件侦听器: 2022-09-05 14:49:42.218 INFO 29767 --- [ restartedMain] org.ehca...

回答 2 投票 0

Cacheable Annotation 有什么用

有人可以解释一下两者之间有什么区别吗 com.googlecode.ehcache.annotations.Cacheable 和 org.springframework.cache.annotation.Cacheable 如果我用第二个替换第一个,那......

回答 1 投票 0

'ehcahce' 包(org.springframework.cache.ehcache)已从 spring-context-support 6.0.x 版本中删除。套餐的替代品是什么?

我在代码中使用 spring-context-support JAR 中的 ehcache 包。在6.0.x版本中,整个软件包已被删除(请参阅下面的SS)。 spring-context-support 6.0.x git 存储库 我...

回答 1 投票 0

无法通过 kubernetes 使用 rmi 复制 ehcache

我正在尝试在 kubernetes 中使用 ehcache 运行我的 grails 应用程序。 当我在本地系统中使用 2 个容器运行 docker 映像时,它能够在 2 个容器之间复制缓存。 是...

回答 1 投票 0

ehcache 3 无法与 Sprint Boot 1.5.22 配合使用

我是 Java 和 SpringBoot 新手。 资源中的ehcache.xml文件: 我是 Java 和 SpringBoot 新手。 resources中的ehcache.xml文件: <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xmlns:jsr107="http://www.ehcache.org/v3/jsr107" xsi:schemaLocation=" http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd"> <cache alias="configCache"> <key-type>java.lang.Integer</key-type> <value-type>java.lang.Integer</value-type> <expiry> <ttl unit="minutes">60</ttl> </expiry> <listeners> <listener> <class>com.demo.example.ehcache.CacheEventLogger</class> <event-firing-mode>ASYNCHRONOUS</event-firing-mode> <event-ordering-mode>UNORDERED</event-ordering-mode> <events-to-fire-on>CREATED</events-to-fire-on> <events-to-fire-on>EXPIRED</events-to-fire-on> </listener> </listeners> <resources> <heap unit="entries">2</heap> <offheap unit="MB">10</offheap> </resources> </cache> </config> application.properties中的以下行: spring.cache.jcache.config=classpath:ehcache.xml 缓存事件记录器: package com.demo.example.ehcache; import com.github.structlog4j.ILogger; import com.github.structlog4j.SLoggerFactory; import org.ehcache.event.CacheEvent; import org.ehcache.event.CacheEventListener; public class CacheEventLogger implements CacheEventListener<Object, Object> { private final ILogger log = SLoggerFactory.getLogger(CacheEventLogger.class); @Override public void onEvent(CacheEvent<? extends Object, ? extends Object> cacheEvent) { log.info("Cache event {} for item with key {}. Old value = {}, New value = {}", cacheEvent.getType(), cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue()); } } ConfigManager.Java: package com.demo.example.ehcache; import org.springframework.cache.annotation.Cacheable; public class ConfigManager { public ConfigManager() { } @Cacheable(value = "configCache", key="#number") public int getNumber(int number) { return number; } } CachingConfig.java: package com.demo.example.configuration; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CachingConfig { } pom.xml 中的以下依赖项: <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.8.1</version> </dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> <version>1.1.1</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-cache</artifactId> </dependency> ServiceFacadeImpl.java: @Component public class ServiceFacadeImpl { private final ConfigManager ConfigManager; ServiceFacadeImpl() { this.ConfigManager = new ConfigManager(); } @Override public Integer getNumber() throws UpfrontException { configManager.getNumber(1); //hard coded 1 here } } 我总是在 getNumber 中传递固定的整数值 1,但它总是执行该函数。为什么缓存不起作用? 您似乎遇到了 Ehcache 3 无法与 Spring Boot 1.5.22 配合使用的问题。以下是 Stack Overflow1 上发布的类似问题的一些详细信息: 用户在资源中有一个 ehcache.xml 文件,其缓存别名为“configCache”,监听器类为 com.demo.example.ehcache.CacheEventLogger。 application.properties 文件具有 spring.cache.jcache.config=classpath:ehcache.xml 行。 CacheEventLogger 类实现了 CacheEventListener。 ConfigManager 类有一个方法 getNumber(int number),并用 @Cacheable(value = "configCache", key="#number") 注释。 CachingConfig 类使用@Configuration 和@EnableCaching 进行注释。 pom.xml 文件具有 ehcache 版本 3.8.1、cache-api 版本 1.1.1、spring-boot-starter-web 和 spring-boot-starter-cache 的依赖项。 如果您的设置类似并且仍然遇到问题,检查以下内容可能会有所帮助: 确保您的 ehcache.xml 已正确配置并位于正确的位置。 确保您的 CacheEventLogger 和 ConfigManager 类已正确实现。 检查您的 pom.xml 是否具有正确的依赖项和版本。 验证您的 application.properties 文件是否具有正确的配置。

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.