Spring boot 无法使用ehcache版本3.4.0和spring boot 2.0.2.0版本进行缓存

Spring boot 无法使用ehcache版本3.4.0和spring boot 2.0.2.0版本进行缓存,spring-boot,spring-cache,ehcache-3,Spring Boot,Spring Cache,Ehcache 3,我尝试了几种方法在SpringBoot应用程序上实现缓存,这似乎是正确的方法,但它只是记录了这一点 CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=studentCache Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=st

我尝试了几种方法在SpringBoot应用程序上实现缓存,这似乎是正确的方法,但它只是记录了这一点

CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=studentCache
Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=urn.X-ehcache.jsr107-default-config,Cache=studentCache
我有一个事件记录器,但没有看到任何输出:

@Component
public class EventLogger implements CacheEventListener<Object, Object> {

    private static final Logger LOGGER = LoggerFactory.getLogger(EventLogger.class);

    @Override
    public void onEvent(CacheEvent<?, ?> event) {
        LOGGER.info("Event: " + event.getType() + " Key: " + event.getKey() + " old value: " + event.getOldValue() + " new value: " + event.getNewValue());
    }
}
我看了很多帖子和博客,看起来我做的很正确,但我肯定是错了

建议

---------------更新1-----------------

我得到一个错误,说:

    Error creating bean with name 'cacheManager' defined in class path 
    resource
 [org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class]: Unsatisfied dependency expressed through method 'cacheManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jCacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'jCacheCacheManager' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] org.terracotta.ehcache.EventLogger
当我加上:

# caching
spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider
spring.cache.jcache.config=classpath:ehcache.xml
痕迹

restartedMain] heConfiguration$JCacheAvailableCondition : Condition JCacheCacheConfiguration.JCacheAvailableCondition on org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration matched due to AnyNestedCondition 1 matched 1 did not; NestedCondition on JCacheCacheConfiguration.JCacheAvailableCondition.CustomJCacheCacheManager @ConditionalOnSingleCandidate (types: javax.cache.CacheManager; SearchStrategy: all) did not find any beans; NestedCondition on JCacheCacheConfiguration.JCacheAvailableCondition.JCacheProvider JCache JCache provider specified
--------------更新2------------------

我在gradle文件中添加了以下内容

task showJarLocations {
    doLast {
        configurations.compile.resolve().each { file ->
            println file.canonicalPath
        }
    }
}
使用ehcache显示的唯一jar是:

/org.ehcache/ehcache/3.4.0/cac1f0840af0040a81401dfa55fa31a4ccc17932/ehcache-3.4.0.jar
and

javax.cache/cache-api/1.1.0/77bdcff7814076dfa61611b0db88487c515150b6/cache-api-1.1.0.jar
我有

spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider
spring.cache.jcache.config=classpath:ehcache.xml
application.properties中也可以使用。这应该可以解释我添加
spring.cache.jcache.config=classpath:ehcache.xml时失败的原因。

但在intellij中,它在我的项目结构中:


我发现使用SpringBootStarter缓存会产生一些微妙的问题。如果找不到您的ehcache.xml或您错误地命名了缓存名称,Spring似乎会退回到通用缓存实现,从而隐藏问题。尝试将spring启动程序缓存作为依赖项删除,并添加:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>

org.springframework
spring上下文支持
还将ehcache添加为显式依赖项,然后查看这是否有帮助。您甚至不需要访问CacheManager,因为您使用的是ehcache.xml;xml的全部要点是使您的配置具有声明性,并且在代码之外。

  • 确保springboot应用程序配置了
    @EnableCaching
  • 确保
    @Cacheable
    中的名称与您的缓存名称匹配(您发布的内容不匹配)

  • 删除缓存配置

  • @Configuration
    public class CacheConfig {
    
        @Bean
        public JCacheManagerCustomizer cacheManagerCustomizer() {
            return new JCacheManagerCustomizer() {
    
                @Override
                public void customize(CacheManager cacheManager) {
                    cacheManager.createCache("studentCache", new MutableConfiguration<>()
                            .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 5)))
                            .setStoreByValue(false)
                            .setStatisticsEnabled(true));
    
                }
            };
        }
    
    } 
    
  • 将您的ehcache.xml文件放在资源的子文件夹中,以确保您没有从其他jar中获取一个

    e.g. `resources/myconfig/ehcache.xml`
    
  • 在application.properties中设置属性,告诉spring在哪里可以找到配置文件

    spring.cache.jcache.config=classpath:myconfig/ehcache.xml
    
  • 使用简化的ehcache.xml。e、 g

    <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="studentCache" uses-template="heap-cache" />
    
        <cache-template name="heap-cache">
            <resources>
                <heap unit="entries">2000</heap>
                <offheap unit="MB">100</offheap>
            </resources>
        </cache-template>
    
    </config>
    
    
    2000
    100
    
如果仍然失败,发布结果,显示您如何知道它失败


使用
时需要CacheConfig的原因是使实体或数据实现可序列化。

在我的情况下工作。

您的配置工作正常如果您添加属性spring.cache.jcache.provider=org.ehcache.jsr107.ehcachechingprovider spring.cache.jcache.config=classpath:ehcache.xml,您在日志上看到了什么区别
logging.level.org.springframework.boot.autoconfigure.cache=trace
logging.level.org.ehcache=trace
@pcoates没有日志输出,如果我添加
=classpath:ehcache.xml
我会在类路径资源中定义名为“cacheManager”的bean时出现
错误[org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class]:
。org.ehcache.jsr107.EhcacheCachingProvider没有变化,这很奇怪,你应该会看到一些东西。我假设你的主应用程序上有
@EnableCaching
。可能值得在主应用程序上发布你的注释,以及你拥有的全套依赖项,以防有什么东西妨碍。我请注意,在您的帖子
Cacheable(value=“studetNode”)中,我假设这是一个输入错误,因为它应该是studentNode,还需要在注释上添加一个“
@”。谢谢。我将试一试!谢谢您的帖子我尝试过了,并在类路径资源中定义了“创建名为ObjectMapPerConfigure”的bean时出错[springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]:`值得一试。感谢您的发布。您是否删除了CacheConfig并尝试精简的ehcache.xml,例如:
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>
@Cacheable(value = "studentCache")
e.g. `resources/myconfig/ehcache.xml`
spring.cache.jcache.config=classpath:myconfig/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="studentCache" uses-template="heap-cache" />

    <cache-template name="heap-cache">
        <resources>
            <heap unit="entries">2000</heap>
            <offheap unit="MB">100</offheap>
        </resources>
    </cache-template>

</config>