use of io.nuls.cache.utils.EhcacheListener 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);
}
Aggregations