org.ehcache.expiry.Duration Java Examples

The following examples show how to use org.ehcache.expiry.Duration. 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: EhCacheProvider3.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public EhCache3 buildCache(String region, CacheExpiredListener listener) {
    return caches.computeIfAbsent(region, v -> {
        org.ehcache.Cache cache = manager.getCache(region, String.class, Serializable.class);
        if (cache == null) {
            CacheConfiguration defaultCacheConfig = manager.getRuntimeConfiguration().getCacheConfigurations().get(DEFAULT_TPL);
            CacheConfiguration<String, Serializable> cacheCfg = CacheConfigurationBuilder.newCacheConfigurationBuilder(defaultCacheConfig).build();
            cache = manager.createCache(region, cacheCfg);
            Duration dura = cache.getRuntimeConfiguration().getExpiry().getExpiryForCreation(null, null);
            long ttl = dura.isInfinite()?-1:dura.getTimeUnit().toSeconds(dura.getLength());
            log.warn("Could not find configuration [{}]; using defaults (TTL:{} seconds).", region, ttl);
        }
        return new EhCache3(region, cache, listener);
    });
}
 
Example #4
Source File: EhCache3.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public long ttl() {
    Duration dur = this.cache.getRuntimeConfiguration().getExpiry().getExpiryForCreation(null,null);
    if (dur.isInfinite())
        return 0L;
    return dur.getTimeUnit().toSeconds(dur.getLength());
}
 
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());
}
 
Example #7
Source File: EhCachePoolMixin.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private <T> EhCacheImpl<T> createNewCache( String cacheId, Class<T> valueType )
{
    configuration.refresh();
    EhCacheConfiguration config = configuration.get();

    ResourcePoolsBuilder poolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder();

    poolsBuilder = poolsBuilder.heap( config.heapSize().get(), MemoryUnit.valueOf( config.heapUnit().get() ) );

    if( config.offHeapSize().get() != null )
    {
        poolsBuilder = poolsBuilder.offheap( config.offHeapSize().get(),
                                             MemoryUnit.valueOf( config.offHeapUnit().get() ) );
    }
    if( config.diskSize().get() != null )
    {
        poolsBuilder = poolsBuilder.disk( config.diskSize().get(),
                                          MemoryUnit.valueOf( config.diskUnit().get() ),
                                          config.diskPersistent().get() );
    }

    CacheConfigurationBuilder<String, T> configBuilder = CacheConfigurationBuilder
        .newCacheConfigurationBuilder( String.class, valueType, poolsBuilder );
    if( config.maxObjectSize().get() != null )
    {
        configBuilder = configBuilder.withSizeOfMaxObjectSize( config.maxObjectSize().get(),
                                                               MemoryUnit.valueOf( config.maxObjectSizeUnit().get() ) );
    }
    if( config.maxObjectGraphDepth().get() != null )
    {
        configBuilder = configBuilder.withSizeOfMaxObjectGraph( config.maxObjectGraphDepth().get() );
    }
    switch( config.expiry().get() )
    {
        case "TIME_TO_IDLE":
            configBuilder = configBuilder.withExpiry( timeToIdleExpiration( Duration.of(
                config.expiryLength().get() == null ? - 1L : config.expiryLength().get(),
                TimeUnit.valueOf( config.expiryTimeUnit().get() ) ) ) );
            break;
        case "TIME_TO_LIVE":
            configBuilder = configBuilder.withExpiry( timeToLiveExpiration( Duration.of(
                config.expiryLength().get() == null ? - 1L : config.expiryLength().get(),
                TimeUnit.valueOf( config.expiryTimeUnit().get() ) ) ) );
            break;
        case "NONE":
        default:
            configBuilder = configBuilder.withExpiry( noExpiration() );
            break;
    }
    CacheConfiguration<String, T> cacheConfig = configBuilder.build();
    org.ehcache.Cache<String, T> cache = cacheManager.createCache( cacheId, cacheConfig );
    return new EhCacheImpl<>( cacheId, cache, valueType );
}