我知道如何在org.ehcache:ehcache:3.8.1中使用XMLConfiguration创建CacheManager:
import org.ehcache.config.Configuration;
import org.ehcache.xml.XmlConfiguration;
import org.ehcache.config.builders.CacheManagerBuilder;
.
.
.
URL myUrl = CacheUtil.class.getResource("/my-config.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
cacheManager.init();我还知道如何使用StatisticsService创建CacheManager
StatisticsService statisticsService = new DefaultStatisticsService();
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statisticsService)
.build();
cacheManager.init();但是,如何使用StatisticsService从XMLConfiguration创建CacheManager呢
发布于 2020-03-05 21:00:07
CachingProvider provider = Caching.getCachingProvider();
CacheManager cacheManager = provider.getCacheManager();
MutableConfiguration<Long, String> configuration =
new MutableConfiguration<Long, String>()
.setTypes(Long.class, String.class)
.setStoreByValue(false)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
Cache<Long, String> cache = cacheManager.createCache("jCache", configuration);
cache.put(1L, "one");
String value = cache.get(1L); CachingProvider实现。当且仅当类路径中只有一个JCache实现jar时,此方法才有效。如果您的类路径中有多个提供程序,那么使用完全限定名称org.ehcache.jsr107.EhcacheCachingProvider来检索Ehcache缓存提供程序。您可以通过使用provider.MutableConfigurationLong和String…,对默认的Caching.getCachingProvider(String)实例使用CacheManager静态方法来完成此操作配置为按引用(而不是按值)存储缓存条目的jCache的缓存<3>发布于 2020-03-03 11:11:37
在类EhcacheManager中有一个构造函数,它接受两个参数:
public EhcacheManager(Configuration config, Collection<Service> services)您可以按如下方式使用它:
URL myUrl = CacheUtil.class.getResource("/my-config.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
StatisticsService statisticsService = new DefaultStatisticsService();
Set<Service> services = new HashSet<>();
services.add(statisticsService);
cacheManager = new EhcacheManager(xmlConfig, services);https://stackoverflow.com/questions/60062088
复制相似问题