ehcache 相关问题

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

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

Spring 6 中 org.springframework.cache.ehcache.EhCacheManagerFactoryBean 的等价物是什么?

我正在尝试将我的项目(jdk 17,tomcat 10.1.12,使用xml文件进行配置)从spring 5迁移到spring 6。但是我收到此错误: 引起原因:java.lang.ClassNotFoundException:org.

回答 1 投票 0

更改 EHCache 3 中各个条目的 TTL

在 EHCache 2.x 中,可以设置缓存中单个条目的生存时间,例如: 元素 dependentElement = cache.get(key); 长lastAccessTime = dependentElement.getLastAcces...

回答 2 投票 0

我可以为每个条目设置 @Cacheable EhCache 的 TTL 吗?

我需要为每个缓存条目设置 TTL。我使用EhCache、Spring Cache。 EhCache 不提供 API 来为 put 方法提供 TTL。 另一种方法是创建多个具有不同的缓存...

回答 1 投票 0

移植到 Springboot 3 / Hibernate 6 / EHCache 3

几天以来我一直在尝试将基于 Springboot 2 / Hibernate 5 / EHCache 2 的旧项目移动到较新的 Java 17 / Springboot 3 / Hibernate 6 / EHCache 3。 当然我读了很多文档...

回答 1 投票 0

如何使用 ECache 以编程方式配置 L2 Hibernate 缓存?

我正在尝试在运行 Ehcache 3.10.x 的 Spring 6 应用程序中为 Hibernate 6.1.7 配置 L2+QueryCache 在我的应用程序中,具有方法级@Cacheable、@CachePut、@CacheEvict 等的 Spring 级缓存可以正常工作...

回答 1 投票 0

Spring Boot EhCache 返回陈旧数据

如果数据过期后服务被命中。它返回旧数据,然后在第二次命中时刷新数据。这导致服务失败。 @Cacheable(value = "tokenCache&...

回答 0 投票 0

如何在 Kubernetes 上配置分布式 Hibernate 缓存 Ehcache

我将 ehcache 用于 Hibernate 缓存,以便我在多个 pod 上运行的服务。我该如何配置? org.hibernate 冬眠-

回答 0 投票 0

Spring Cache @Cacheable - 从同一 bean 的另一个方法调用时不工作

从同一 bean 的另一个方法调用缓存的方法时,Spring 缓存不起作用。 这是一个示例,可以清楚地解释我的问题。 配置: 从同一 bean 的另一个方法调用缓存方法时,Spring 缓存不起作用。 这里有一个例子可以清楚地解释我的问题。 配置: <cache:annotation-driven cache-manager="myCacheManager" /> <bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="myCache" /> </bean> <!-- Ehcache library setup --> <bean id="myCache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true"> <property name="configLocation" value="classpath:ehcache.xml"></property> </bean> <cache name="employeeData" maxElementsInMemory="100"/> 缓存服务: @Named("aService") public class AService { @Cacheable("employeeData") public List<EmployeeData> getEmployeeData(Date date){ ..println("Cache is not being used"); ... } public List<EmployeeEnrichedData> getEmployeeEnrichedData(Date date){ List<EmployeeData> employeeData = getEmployeeData(date); ... } } 结果: aService.getEmployeeData(someDate); output: Cache is not being used aService.getEmployeeData(someDate); output: aService.getEmployeeEnrichedData(someDate); output: Cache is not being used getEmployeeData 方法调用在第二次调用中按预期使用缓存 employeeData。但是当在getEmployeeData类中调用AService方法时(在getEmployeeEnrichedData中),Cache没有被使用。 这是 spring 缓存的工作方式还是我遗漏了什么? 我相信这就是它的工作原理。根据我的记忆,生成了一个代理类,它拦截所有请求并使用缓存值进行响应,但同一类中的“内部”调用将不会获取缓存值。 来自https://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable 只有通过代理传入的外部方法调用是 拦截。这意味着自调用实际上是一种方法 在目标对象中调用目标对象的另一个方法, 不会在运行时导致实际的缓存拦截,即使 被调用的方法标有@Cacheable。 自 Spring 4.3 以来,问题可以通过 @Resource 注释使用 self-autowiring 来解决: @Component @CacheConfig(cacheNames = "SphereClientFactoryCache") public class CacheableSphereClientFactoryImpl implements SphereClientFactory { /** * 1. Self-autowired reference to proxified bean of this class. */ @Resource private SphereClientFactory self; @Override @Cacheable(sync = true) public SphereClient createSphereClient(@Nonnull TenantConfig tenantConfig) { // 2. call cached method using self-bean return self.createSphereClient(tenantConfig.getSphereClientConfig()); } @Override @Cacheable(sync = true) public SphereClient createSphereClient(@Nonnull SphereClientConfig clientConfig) { return CtpClientConfigurationUtils.createSphereClient(clientConfig); } } 下面的示例是我用来从同一个 bean 中访问代理的示例,它类似于@mario-eis 的解决方案,但我发现它更具可读性(也许不是:-)。无论如何,我喜欢在服务级别保留@Cacheable 注释: @Service @Transactional(readOnly=true) public class SettingServiceImpl implements SettingService { @Inject private SettingRepository settingRepository; @Inject private ApplicationContext applicationContext; @Override @Cacheable("settingsCache") public String findValue(String name) { Setting setting = settingRepository.findOne(name); if(setting == null){ return null; } return setting.getValue(); } @Override public Boolean findBoolean(String name) { String value = getSpringProxy().findValue(name); if (value == null) { return null; } return Boolean.valueOf(value); } /** * Use proxy to hit cache */ private SettingService getSpringProxy() { return applicationContext.getBean(SettingService.class); } ... 另见在 Spring bean 中开始新事务 以下是我为小型项目所做的,在同一类中只使用少量的方法调用。强烈建议使用代码内文档,因为它对同事来说可能看起来很奇怪。但它易于测试、简单、实现速度快,并且让我省去了成熟的 AspectJ 工具。但是,对于更频繁的使用,我建议使用 AspectJ 解决方案。 @Service @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) class AService { private final AService _aService; @Autowired public AService(AService aService) { _aService = aService; } @Cacheable("employeeData") public List<EmployeeData> getEmployeeData(Date date){ ..println("Cache is not being used"); ... } public List<EmployeeEnrichedData> getEmployeeEnrichedData(Date date){ List<EmployeeData> employeeData = _aService.getEmployeeData(date); ... } } 如果您从同一个 bean 调用缓存方法,它将被视为私有方法并且注释将被忽略 是的,由于其他帖子中已经提到的原因,缓存不会发生。但是,我会通过将该方法放入其自己的类(在本例中为服务)来解决问题。这样你的代码将更容易维护/测试和理解。 @Service // or @Named("aService") public class AService { @Autowired //or how you inject your dependencies private EmployeeService employeeService; public List<EmployeeData> getEmployeeData(Date date){ employeeService.getEmployeeData(date); } public List<EmployeeEnrichedData> getEmployeeEnrichedData(Date date){ List<EmployeeData> employeeData = getEmployeeData(date); ... } } @Service // or @Named("employeeService") public class EmployeeService { @Cacheable("employeeData") public List<EmployeeData> getEmployeeData(Date date){ println("This will be called only once for same date"); ... } } 在我的案例中,我添加了变量: @Autowired private AService aService; 所以我使用getEmployeeData调用aService方法 @Named("aService") public class AService { @Cacheable("employeeData") public List<EmployeeData> getEmployeeData(Date date){ ..println("Cache is not being used"); ... } public List<EmployeeEnrichedData> getEmployeeEnrichedData(Date date){ List<EmployeeData> employeeData = aService.getEmployeeData(date); ... } } 在这种情况下它将使用缓存。 更好的方法应该是创建另一个服务,如 ACachingService 并调用 ACachingService.cachingMethod() 而不是自我自动装配(或任何其他尝试自我注入的方法)。这样你就不会陷入循环依赖,升级到更新的 Spring 时可能会导致警告/错误(在我的例子中是 Spring 2.6.6 ): ERROR o.s.boot.SpringApplication - Application run failed org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'webSecurityConfig': Requested bean is currently in creation: Is there an unresolvable circular reference? 我们查看了这里的所有解决方案,并决定为缓存方法使用一个单独的类,因为 Spring 5 不喜欢循环依赖。 使用静态编织在你的 bean 周围创建代理。在这种情况下,即使是“内部”方法也能正常工作 为此目的,我使用带有真实缓存的内部 bean (FactoryInternalCache): @Component public class CacheableClientFactoryImpl implements ClientFactory { private final FactoryInternalCache factoryInternalCache; @Autowired public CacheableClientFactoryImpl(@Nonnull FactoryInternalCache factoryInternalCache) { this.factoryInternalCache = factoryInternalCache; } /** * Returns cached client instance from cache. */ @Override public Client createClient(@Nonnull AggregatedConfig aggregateConfig) { return factoryInternalCache.createClient(aggregateConfig.getClientConfig()); } /** * Returns cached client instance from cache. */ @Override public Client createClient(@Nonnull ClientConfig clientConfig) { return factoryInternalCache.createClient(clientConfig); } /** * Spring caching feature works over AOP proxies, thus internal calls to cached methods don't work. That's why * this internal bean is created: it "proxifies" overloaded {@code #createClient(...)} methods * to real AOP proxified cacheable bean method {@link #createClient}. * * @see <a href="https://stackoverflow.com/questions/16899604/spring-cache-cacheable-not-working-while-calling-from-another-method-of-the-s">Spring Cache @Cacheable - not working while calling from another method of the same bean</a> * @see <a href="https://stackoverflow.com/questions/12115996/spring-cache-cacheable-method-ignored-when-called-from-within-the-same-class">Spring cache @Cacheable method ignored when called from within the same class</a> */ @EnableCaching @CacheConfig(cacheNames = "ClientFactoryCache") static class FactoryInternalCache { @Cacheable(sync = true) public Client createClient(@Nonnull ClientConfig clientConfig) { return ClientCreationUtils.createClient(clientConfig); } } } 我想分享我认为最简单的方法: 自动装配控制器并使用它来调用方法,而不是使用类上下文this. 更新后的代码如下所示: @Controller public class TestController { @Autowired TestController self; @RequestMapping("/test") public String testView(){ self.expensiveMethod(); return "test"; } @Cacheable("ones") public void expensiveMethod(){ System.out.println("Cache is not being used"); } } 处理缓存注解的默认advice方式是“proxy”。在应用程序启动时,将扫描所有缓存注解,如@Caching、@Cacheable、@CacheEvict 等,并为所有这些类生成一个目标代理类。代理允许拦截对这些可缓存方法的调用,从而添加缓存建议/行为。 所以当我们从同一个类调用可缓存的方法时,如下所示,来自客户端的调用不会以允许向它们添加缓存建议的方式被拦截。因此,每次都会出现意外的缓存未命中。 解决方案: 从不同的 bean 调用 Cacheable 方法以使用带有缓存建议的代理类。 基于单一职责原则的实现;可缓存类只支持缓存而不使用缓存 ;-). 这就是它的工作原理。 将缓存的方法移动到帮助器/包装器组件 bean。

回答 15 投票 0

无法在 grails 4.0.10 中创建请求的服务 org.hibernate.cache.spi.RegionFactory 错误?

我在使休眠二级缓存工作时遇到问题。 我正在使用 grails 4.0.10。 我得到的错误是 引起:org.springframework.beans.factory.BeanCreationException:创建错误

回答 1 投票 0

如何知道Spring-cache中缓存的大小?

如何知道当前spring-cache中缓存的大小?我想记录缓存大小达到最大值的时间。我找不到可以这样做的函数。我怎么才能知道呢?

回答 1 投票 0

关闭net.sf.ehcache和org.hibernate DEBUG日志记录

在我的Spring(版本5.2.5.RELEASE)应用程序中,我从net.sf.ehcache和org.hibernate收到了大量的DEBUG消息。我正在使用hibernate-ehcache的5.2.10.Final版本。这些消息看起来...

回答 1 投票 0

并发修改 EHCache 多线程Java

我们有多个线程试图从EHCache获取信息,这有时会导致并发修改异常。我试过Retryable来解决这个问题,但它似乎没有任何作用。任何...

回答 1 投票 0

带ehcache的倒置索引

假设我想在一个有4个独特单词的文档上创建一个倒序索引。它将看起来像word1 -> document, word2 -> document, word3 -> document, word4 -> document。使用一个...

回答 1 投票 1

如何使用terracotta服务器实现集群缓存?

谁能告诉我如何使用terracota服务器实现两个微服务之间的集群缓存共享,任何参考项目都将是伟大的开始。

回答 1 投票 0

Spring Transaction Aware缓存不起作用

我正在使用Spring Ehcache作为缓存提供程序来使用Spring缓存抽象。我正在尝试将缓存操作附加到Spring JPA事务,但无法这样做。即使交易失败/ ...

回答 1 投票 0

具有暂停功能的可缓存功能

我正在尝试编写可缓存的暂挂函数,我认为它无法正常工作。我正在使用@Cacheable(“ product-image”)暂停乐趣getProductImage(productId:String):字符串{return ...

回答 1 投票 0

我正在将某些项目存储在我的缓存中,这些项目对于实时查询而言过于昂贵。如何更新这些缓存元素?

我需要每半小时更新一次缓存元素,以防止其过时。在更新期间,我需要确保get(element)不会返回null,因为我不希望...

回答 1 投票 0

纯Java替代数据库/用于存储记录的缓存

在我的Java应用程序中,我开始使用H2数据库存储所有数据。但是该软件必须在某些较慢的单个cpu服务器上运行,因此我想减少所用的cpu周期,并减少一个区域I ...

回答 2 投票 0

Ehcache 2.10.6命中统计信息未增加

我已经在我的一个项目中建立了一个缓存,以及以下自定义运行状况指示器:@Component公共类CustomHealthIndicator扩展了AbstractHealthIndicator {@Override ...

回答 1 投票 0

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