首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用StatisticsService从XMLConfiguration创建CacheManager

使用StatisticsService从XMLConfiguration创建CacheManager
EN

Stack Overflow用户
提问于 2020-02-05 00:54:26
回答 2查看 454关注 0票数 1

我知道如何在org.ehcache:ehcache:3.8.1中使用XMLConfiguration创建CacheManager

代码语言:javascript
复制
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

代码语言:javascript
复制
StatisticsService statisticsService = new DefaultStatisticsService();
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .using(statisticsService)
      .build();
cacheManager.init();

但是,如何使用StatisticsServiceXMLConfiguration创建CacheManager

EN

回答 2

Stack Overflow用户

发布于 2020-03-05 21:00:07

代码语言:javascript
复制
 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); 

  1. 从应用程序的类路径中检索默认的CachingProvider实现。当且仅当类路径中只有一个JCache实现jar时,此方法才有效。如果您的类路径中有多个提供程序,那么使用完全限定名称org.ehcache.jsr107.EhcacheCachingProvider来检索Ehcache缓存提供程序。您可以通过使用provider.
  2. Create a缓存配置、MutableConfiguration
  3. with键类型和值类型分别作为LongString…,对默认的Caching.getCachingProvider(String)实例使用CacheManager静态方法来完成此操作配置为按引用(而不是按值)存储缓存条目的
  4. ​…从那一刻起,使用为条目定义的一分钟的过期时间将一些数据放入缓存管理器,并使用在步骤
  5. 中创建的配置创建名为jCache的缓存<3>
  6. cache.
  7. Retrieve <3>
  8. cache.
  9. Retrieve<3>​data from​cache。
票数 1
EN

Stack Overflow用户

发布于 2020-03-03 11:11:37

在类EhcacheManager中有一个构造函数,它接受两个参数:

代码语言:javascript
复制
public EhcacheManager(Configuration config, Collection<Service> services)

您可以按如下方式使用它:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60062088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档