org.ehcache.expiry.Expirations Java Examples

The following examples show how to use org.ehcache.expiry.Expirations. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EhCacheProvider3.java    From J2Cache with Apache License 2.0 6 votes vote down vote up
@Override
public EhCache3 buildCache(String region, long timeToLiveInSeconds, CacheExpiredListener listener) {
    EhCache3 ehcache = caches.computeIfAbsent(region, v -> {
        CacheConfiguration<String, Object> conf = CacheConfigurationBuilder.newCacheConfigurationBuilder(
                String.class, Object.class, ResourcePoolsBuilder.heap(defaultHeapSize))
                .withExpiry(Expirations.timeToLiveExpiration(Duration.of(timeToLiveInSeconds, TimeUnit.SECONDS)))
                .build();
        org.ehcache.Cache cache = manager.createCache(region, conf);
        log.info("Started Ehcache region [{}] with TTL: {}", region, timeToLiveInSeconds);
        return new EhCache3(region, cache, listener);
    });

    if (ehcache.ttl() != timeToLiveInSeconds)
        throw new IllegalArgumentException(String.format("Region [%s] TTL %d not match with %d", region, ehcache.ttl(), timeToLiveInSeconds));

    return ehcache;
}
 
Example #2
Source File: TestEhcache.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Before
public void init() {
    cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .withCache("preConfigured",
                    CacheConfigurationBuilder
                            .newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10))
                            .withExpiry(Expirations.timeToLiveExpiration(Duration.of(20, TimeUnit.SECONDS))))
            .build();
    cacheManager.init();

    // PersistentCacheManager persistentCacheManager =
    // CacheManagerBuilder.newCacheManagerBuilder()
    // .with(CacheManagerBuilder.persistence(getStoragePath() +
    // File.separator + "myData"))
    // .withCache("threeTieredCache",
    // CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class,
    // String.class,
    // ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10,
    // EntryUnit.ENTRIES)
    // .offheap(1, MemoryUnit.MB).disk(20, MemoryUnit.MB)))
    // .build(true);
    // persistentCacheManager.close();
}
 
Example #3
Source File: Application.java    From conciliator with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public CacheManager cacheManager(@Autowired Config config) {
    long ttl = Long.valueOf(config.getProperties().getProperty(Config.PROP_CACHE_TTL));

    config.getProperties().getProperty(Config.PROP_CACHE_SIZE);

    MemSize memSize = MemSize.valueOf(config.getProperties().getProperty(Config.PROP_CACHE_SIZE));

    LogFactory.getLog(getClass()).info(
            String.format("Initializing cache TTL=%d secs, size=%d %s",
                    ttl, memSize.getSize(), memSize.getUnit().toString()));

    org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder
            .newCacheConfigurationBuilder(Object.class, Object.class,
                    ResourcePoolsBuilder.newResourcePoolsBuilder()
                            .heap(memSize.getSize(), memSize.getUnit()))
            .withExpiry(Expirations.timeToLiveExpiration(new org.ehcache.expiry.Duration(ttl, TimeUnit.SECONDS)))
            .build();

    Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
    caches.put(CACHE_DEFAULT, cacheConfiguration);

    EhcacheCachingProvider provider = (EhcacheCachingProvider) javax.cache.Caching.getCachingProvider();

    // when our cacheManager bean is re-created several times for
    // diff test configurations, this provider seems to hang on to state
    // causing cache settings to not be right. so we always close().
    provider.close();

    DefaultConfiguration configuration = new DefaultConfiguration(
            caches, provider.getDefaultClassLoader());

    return new JCacheCacheManager(
            provider.getCacheManager(provider.getDefaultURI(), configuration));
}
 
Example #4
Source File: PolicyCacheConfiguration.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Configuration getCacheConfiguration() {
    CacheEventListenerConfiguration eventConfig = CacheEventListenerConfigurationBuilder
            .newEventListenerConfiguration(cacheEvent ->
                    LOG.warn("Policy " + ((Policy) cacheEvent.getOldValue()).getId()
                            + " has been evicted. Check your max heap size settings"), EventType.EVICTED)
            .build();
    return Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
            .newCacheConfigurationBuilder(String.class, Policy.class,
                    ResourcePoolsBuilder.heap(numEntries))
            .withSizeOfMaxObjectGraph(2000)
            .withExpiry(Expirations.noExpiration())
            .add(eventConfig)
            .build());
}
 
Example #5
Source File: EhCache.java    From weed3 with Apache License 2.0 5 votes vote down vote up
public EhCache(String keyHeader, int defSeconds) {
    _cacheKeyHead = keyHeader;
    _defaultSeconds = defSeconds;

    // 配置默认缓存属性
    CacheConfiguration<String, Object> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(
            // 缓存数据K和V的数值类型
            // 在ehcache3.3中必须指定缓存键值类型,如果使用中类型与配置的不同,会报类转换异常
            String.class, Object.class,
            ResourcePoolsBuilder
                    .newResourcePoolsBuilder()
                    //设置缓存堆容纳元素个数(JVM内存空间)超出个数后会存到offheap中
                    .heap(1000L, EntryUnit.ENTRIES)
                    //设置堆外储存大小(内存存储) 超出offheap的大小会淘汰规则被淘汰
                    .offheap(100L, MemoryUnit.MB)
                    // 配置磁盘持久化储存(硬盘存储)用来持久化到磁盘,这里设置为false不启用
                    .disk(500L, MemoryUnit.MB, false)
    ).withExpiry(Expirations.timeToLiveExpiration(
            //设置缓存过期时间
            Duration.of(defSeconds, TimeUnit.SECONDS))
    ).withExpiry(Expirations.timeToIdleExpiration(
            //设置被访问后过期时间(同时设置和TTL和TTI之后会被覆盖,这里TTI生效,之前版本xml配置后是两个配置了都会生效)
            Duration.of(60L, TimeUnit.SECONDS))
    ).build();

    // CacheManager管理缓存
    CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder()
            // 设置一个默认缓存配置
            .withCache("defaultCache", cacheConfiguration)
            //创建之后立即初始化
            .build(true);

    _cache = manager.getCache("defaultCache", String.class, Object.class);//获得缓存
}
 
Example #6
Source File: CacheConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(Expirations.timeToLiveExpiration(Duration.of(ehcache.getTimeToLiveSeconds(), TimeUnit.SECONDS)))
            .build());
}