Search in sources :

Example 1 with EventType

use of org.ehcache.event.EventType in project nuls by nuls-io.

the class EhCacheManager method createCache.

public void createCache(String title, Class keyType, Class<? extends Serializable> valueType, int heapMb, int timeToLiveSeconds, int timeToIdleSeconds, NulsCacheListener listener) {
    CacheConfigurationBuilder builder = CacheConfigurationBuilder.newCacheConfigurationBuilder(keyType, valueType, ResourcePoolsBuilder.newResourcePoolsBuilder().heap(heapMb, MemoryUnit.MB));
    builder = builder.withSizeOfMaxObjectGraph(EhCacheConstant.MAX_SIZE_OF_CACHE_OBJ_GRAPH);
    if (timeToLiveSeconds > 0) {
        builder = builder.withExpiry(Expirations.timeToLiveExpiration(Duration.of(timeToLiveSeconds, TimeUnit.SECONDS)));
    }
    if (timeToIdleSeconds > 0) {
        builder = builder.withExpiry(Expirations.timeToIdleExpiration(Duration.of(timeToIdleSeconds, TimeUnit.SECONDS)));
    }
    if (listener != null) {
        Set<EventType> types = new HashSet<>();
        types.add(EventType.CREATED);
        types.add(EventType.UPDATED);
        types.add(EventType.EVICTED);
        types.add(EventType.EXPIRED);
        types.add(EventType.REMOVED);
        CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder.newEventListenerConfiguration(new EhcacheListener(listener), types).unordered().asynchronous();
        builder = builder.add(cacheEventListenerConfiguration);
    }
    cacheManager.createCache(title, builder.build());
    KEY_TYPE_MAP.put(title, keyType);
    VALUE_TYPE_MAP.put(title, valueType);
}
Also used : CacheEventListenerConfigurationBuilder(org.ehcache.config.builders.CacheEventListenerConfigurationBuilder) EventType(org.ehcache.event.EventType) EhcacheListener(io.nuls.cache.utils.EhcacheListener) CacheConfigurationBuilder(org.ehcache.config.builders.CacheConfigurationBuilder)

Aggregations

EhcacheListener (io.nuls.cache.utils.EhcacheListener)1 CacheConfigurationBuilder (org.ehcache.config.builders.CacheConfigurationBuilder)1 CacheEventListenerConfigurationBuilder (org.ehcache.config.builders.CacheEventListenerConfigurationBuilder)1 EventType (org.ehcache.event.EventType)1