我通读了ehcache3的文档,它在spring缓存的上下文中有点令人困惑。我的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache:config updateCheck="true"
monitoring="autodetect"
dynamicConfig="true"
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:ehcache='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xsi:schemaLocation="
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd">
<ehcache:annotation-driven />
<ehcache:service>
<service>
<jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>
</ehcache:service>
<ehcache:cache alias="xyz" statistics="true">
<ehcache:key-type>java.lang.Long</ehcache:key-type>
<ehcache:value-type>a.b.c.something</ehcache:value-type>
<ehcache:expiry>
<ehcache:ttl unit="seconds">10</ehcache:ttl>
</ehcache:expiry>
<ehcache:resources>
<ehcache:heap unit="entries">10000</ehcache:heap>
<ehcache:offheap unit="MB">1</ehcache:offheap>
</ehcache:resources>
<jsr107:mbeans enable-statistics="true"/>
</ehcache:cache>
...
</ehcache:config>
cache:
jcache:
config: classpath*:ehcache.xml
我的yaml:
cache:
jcache:
config: classpath*:ehcache.xml
对于我应该寻找的东西,我有点迷失了--我以为org.ehcache可以被剖析出来,并且会出现。我在jvisualvm中看不到有这种模式的任何东西。或者不确定如何读取信息。在ehcache2.x中曾经很简单,任何帮助都将不胜感激。我想知道缓存的大小和数量。缓存中当前元素的数量等。

发布于 2018-01-19 02:26:18
要确定如何回答,需要spring配置和一些Java代码。然而,您当前的ehcache.xml似乎是Ehcache2和Ehcache3的奇怪混合体,而且yaml是错误的。
因此,您需要一个包含JSR-107的pom.xml
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.4.0</version>
</dependency>application.yml应该有一个spring前缀。
spring:
cache:
jcache:
config: classpath:ehcache.xml要启用缓存,您需要@EnableCaching或<cache:annotation-driven/>之类的东西,它们应该在Spring配置中,而不是在ehcache.xml中。
最后,我清理了不应该出现在ehcache.xml中的所有内容。我还使用了一个默认的名称空间,使其更具可读性。此外,由于Spring缓存是无类型的,您需要从缓存配置中删除key-type和value-type。
<?xml version="1.0" encoding="UTF-8"?>
<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/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd">
<service>
<jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>
<cache alias="xyz">
<expiry>
<ttl unit="seconds">10</ttl>
</expiry>
<resources>
<heap unit="entries">10000</heap>
<offheap unit="MB">1</offheap>
</resources>
<jsr107:mbeans enable-statistics="true"/>
</cache>
</config>https://stackoverflow.com/questions/48308705
复制相似问题