在没有 XML 配置的情况下将 Ehcache CacheManager (v 3.x) 转换为 Spring CacheManager
Converting Ehcache CacheManager (v 3.x) to Spring CacheManager without XML configuration
我正在尝试在我的应用程序中使用 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>
我有这样的 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);
// ...
}
是否可以将 Ehcache CacheManager 转换为 Spring CacheManager?
我认为应该是这样的:return new JCacheCacheManager(/*some code*/);
您不能简单地将 ehcache CacheManager 转换为 spring CacheManager。
您可以使用 org.ehcache.jsr107.EhcacheCachingProvider
获得 javax.cache.CacheManager
并将其提供给 org.springframework.cache.jcache.JCacheCacheManager
这是 org.springframework.cache.CacheManager
用于 jcache(又名 jsr107)的实现。
import java.util.HashMap;
import java.util.Map;
import javax.cache.CacheManager;
import javax.cache.Caching;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.core.config.DefaultConfiguration;
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;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public JCacheCacheManager jCacheCacheManager() {
JCacheCacheManager jCacheManager = new JCacheCacheManager(cacheManager());
return jCacheManager;
}
@Bean(destroyMethod = "close")
public CacheManager cacheManager() {
ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(2000, EntryUnit.ENTRIES)
.offheap(100, MemoryUnit.MB)
.build();
CacheConfiguration<Object,Object> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(
Object.class,
Object.class,
resourcePools).
build();
Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
caches.put("myCache", cacheConfiguration);
EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider");
org.ehcache.config.Configuration configuration = new DefaultConfiguration(caches, provider.getDefaultClassLoader());
return provider.getCacheManager(provider.getDefaultURI(), (org.ehcache.config.Configuration) configuration);
}
}
如果您使用 spring-boot,它应该会自动为您配置 JCacheCacheManager。然后,您可以使用 JCacheManagerCustomizer
配置缓存。
我正在尝试在我的应用程序中使用 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>
我有这样的 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);
// ...
}
是否可以将 Ehcache CacheManager 转换为 Spring CacheManager?
我认为应该是这样的:return new JCacheCacheManager(/*some code*/);
您不能简单地将 ehcache CacheManager 转换为 spring CacheManager。
您可以使用 org.ehcache.jsr107.EhcacheCachingProvider
获得 javax.cache.CacheManager
并将其提供给 org.springframework.cache.jcache.JCacheCacheManager
这是 org.springframework.cache.CacheManager
用于 jcache(又名 jsr107)的实现。
import java.util.HashMap;
import java.util.Map;
import javax.cache.CacheManager;
import javax.cache.Caching;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.core.config.DefaultConfiguration;
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;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public JCacheCacheManager jCacheCacheManager() {
JCacheCacheManager jCacheManager = new JCacheCacheManager(cacheManager());
return jCacheManager;
}
@Bean(destroyMethod = "close")
public CacheManager cacheManager() {
ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(2000, EntryUnit.ENTRIES)
.offheap(100, MemoryUnit.MB)
.build();
CacheConfiguration<Object,Object> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(
Object.class,
Object.class,
resourcePools).
build();
Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
caches.put("myCache", cacheConfiguration);
EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider");
org.ehcache.config.Configuration configuration = new DefaultConfiguration(caches, provider.getDefaultClassLoader());
return provider.getCacheManager(provider.getDefaultURI(), (org.ehcache.config.Configuration) configuration);
}
}
如果您使用 spring-boot,它应该会自动为您配置 JCacheCacheManager。然后,您可以使用 JCacheManagerCustomizer
配置缓存。