net.sf.ehcache和org.ehcache有什么区别?
net.sf.ehcache的当前版本为2.10.5,而org.ehcache的当前版本为3.5.2。
Spring使用net.sf.ehcache的CacheManager,而org.ehcache的CacheManager不兼容。
有什么具体原因吗?请解释。
我想要完成的是为缓存设置 TTL(生存时间)和 TTI(空闲时间),以便密钥要么在 TTL 时间之后过期,要么可以提前过期以防万一TTI 期间访问。
在 Ehcache 2 中,可以使用以下配置:
<cache name="my.custom.Cache"
timeToIdleSeconds="10"
timeToLiveSeconds="120">
</cache>
Run Code Online (Sandbox Code Playgroud)
在 Ehcache 3 中,类似的配置块如下所示:
<cache alias="my.custom.Cache">
<expiry>
<tti unit="seconds">10</tti>
<ttl unit="minutes">2</ttl>
</expiry>
</cache>
Run Code Online (Sandbox Code Playgroud)
问题是这样的配置被认为是无效的,因为ehcache.xsd声明标签下应该只有一个选项expiry(tti或ttl,但不能同时存在)。
现在我有以下配置:
@Configuration
@EnableCaching
public class EhcacheConfig {
@Bean
public CacheManager cacheManager() throws URISyntaxException {
return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager(
getClass().getResource("/ehcache.xml").toURI(),
getClass().getClassLoader()
));
}
}
Run Code Online (Sandbox Code Playgroud)
它指的是以下 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="pow_cache">
<key-type>org.springframework.cache.interceptor.SimpleKey</key-type>
<value-type>java.lang.Double</value-type>
<expiry>
<ttl unit="seconds">15</ttl>
</expiry>
<listeners>
<listener>
<class>my.pack.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>
Run Code Online (Sandbox Code Playgroud)
服务看起来像这样:
@Cacheable(value = "pow_cache", unless = "#pow==3||#result>100", condition = "#val<5")
public Double pow(int val, int pow) throws InterruptedException {
System.out.println(String.format("REAL invocation myService.pow(%s, …Run Code Online (Sandbox Code Playgroud) 我希望 EhCache 使用默认模板创建缓存。这是我的ehcache.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<config
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
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">
<service>
<jsr107:defaults default-template="default" jsr-107-compliant-atomics="true">
</jsr107:defaults>
</service>
<cache-template name="default">
<expiry>
<ttl unit="seconds">5</ttl>
</expiry>
<heap>100</heap>
</cache-template>
<!-- <cache alias="test" uses-template="default" />--> <!-- I want to omit this. -->
</config>
Run Code Online (Sandbox Code Playgroud)
这是我的application.yml文件:
spring:
cache:
jcache:
config: classpath:ehcache.xml
Run Code Online (Sandbox Code Playgroud)
我不想ehcache.xml每次添加需要新的缓存名称时都进行更新。如何使用 Spring Boot 配置 EhCache 以使用我的默认模板?
如果我不指定缓存名称配置,则会收到此错误:
java.lang.IllegalArgumentException: Cannot find cache named 'test' for CacheResultOperation
Run Code Online (Sandbox Code Playgroud) 有没有替代net.sf.ehcache.CacheManager.ALL_CACHE_MANAGERS品
<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
?
这是我需要修改才能使用版本 3.8.1 的代码:
import org.ehcache.CacheManager;
.
.
.
List list = CacheManager.ALL_CACHE_MANAGERS;
for (int i = 0, n = list.size(); i < n; i++) {
CacheManager cm = (CacheManager) list.get(i);
log.debug("CacheManager: " + cm.getName());
if (cm.getName().equals(CACHE_MANAGER_NAME)) {
log.debug("CM " + CACHE_MANAGER_NAME + " existed. Destroying it.");
cm.shutdown();
}
}
Run Code Online (Sandbox Code Playgroud)
ehcache 3.8.1 有办法做到这一点吗?
有人用Spring 4.2实现了EhCache 3(不使用Spring启动).如果是这样,实施该步骤的步骤是什么?
问题是spring-context-support(添加Spring的缓存注释)要求Ehcache的CacheManager在这个类路径上:net.sf.ehcache.CacheManager
但是,在Ehcache 3中,CacheManager类驻留在另一个类路径上:org.ehcache.CacheManager.
所以,基本上spring-context-support不支持Ehcache 3.你必须直接使用JSR-107注释,而不是Spring提供的注释.
如果有人实现了这个组合,请给你ehcache.xml和spring配置以供参考.
我试图在基于Spring Boot 2 / Spring Framework 5的Web应用程序中使用EhCache 3.5缓存功能。
我添加了EHCache依赖项:
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后在src / main / resources文件夹中创建ehcache.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<cache name="orders" maxElementsInMemory="100"
eternal="false" overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" copyOnRead="true"
copyOnWrite="true" />
</ehcache>
Run Code Online (Sandbox Code Playgroud)
Spring 5参考指南没有提到EHCache的使用,Spring 4参考指南指出:“ Ehcache 3.x完全符合JSR-107,并且不需要专用支持。”
所以我创建了控制器OrderController和REST端点:
@Cacheable("orders")
@GetMapping(path = "/{id}")
public Order findById(@PathVariable int id) {
return orderRepository.findById(id);
}
Run Code Online (Sandbox Code Playgroud)
Spring Boot配置:
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
} …Run Code Online (Sandbox Code Playgroud) 在 ehcache 2.x 版本中我有以下配置。
<cache name="basicCache"
maxEntriesLocalHeap="400"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false">
</cache>
Run Code Online (Sandbox Code Playgroud)
以下是对应的ehcache 3.x版本。
<ehcache:cache alias="basicCache">
<ehcache:key-type>java.lang.Long</ehcache:key-type>
<ehcache:value-type>java.lang.String</ehcache:value-type>
<ehcache:resources>
<ehcache:heap unit=entries">400</ehcache:heap>
</ehcache:resources>
</ehcache:cache>
Run Code Online (Sandbox Code Playgroud)
有人可以帮我在 ehcache 3.5.2 版本中配置以下属性吗?
永恒=“真”和 overflowToDisk=“假”
出于某种原因,我无法将expiry元素添加到ehcache3配置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'>
<service>
<jsr107:defaults>
<jsr107:cache name="people" template="heap-cache"/>
</jsr107:defaults>
</service>
<cache-template name="heap-cache">
<listeners>
<listener>
<class>org.terracotta.ehcache.EventLogger</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>UPDATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>REMOVED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
<expiry>
<ttl unit="seconds">10</ttl>
</expiry>
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap> <!-- unit of measure is case sensitive! -->
</resources>
</cache-template>
</config>
Run Code Online (Sandbox Code Playgroud)
错误消息是:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'expiry'. One of '{"http://www.ehcache.org/v3":heap, "http://www.ehcache.org/v3":resources, "http://www.ehcache.org/v3":heap-store-settings, "http://www.ehcache.org/v3":disk-store-settings, "http://www.ehcache.org/v3":service-configuration}' is expected.
Run Code Online (Sandbox Code Playgroud)
但是看看这个xsd:http: //www.ehcache.org/schema/ehcache-core.xsd 我没看到标签有什么问题,任何人都有任何想法?
我正在尝试在我的应用程序中使用 Ehcache 管理器。我想在没有 xml 配置的情况下设置它。我有下一个依赖项:
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我有这样的 CacheManager bean:
@Bean
public org.springframework.cache.CacheManager cacheManager() {
org.ehcache.CacheManager mainPageCache = CacheManagerBuilder
.newCacheManagerBuilder()
.withCache("mainPageCache", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
Pageable.class,
Collection.class,
ResourcePoolsBuilder.heap(10))
.withExpiry(ExpiryPolicyBuilder
.timeToLiveExpiration(Duration
.of(10, ChronoUnit.SECONDS))))
.build(true);
// ...
}
Run Code Online (Sandbox Code Playgroud)
是否可以将 Ehcache CacheManager 转换为 Spring CacheManager?我认为应该是这样的:return new JCacheCacheManager(/*some code*/);
ehcache-3 ×10
ehcache ×6
java ×4
spring ×4
spring-boot ×2
spring-cache ×2
caching ×1
spring-mvc ×1
xml ×1
xsd ×1