Java Code Examples for org.cache2k.Cache2kBuilder#eternal()

The following examples show how to use org.cache2k.Cache2kBuilder#eternal() . 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: Cache2kCacheFactory.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public <K, V> Cache<K, V> build(final String name, final CacheConfig<K, V> config) {
    Cache2kBuilder<K, CacheObject<V>> builder = Cache2kBuilder.forUnknownTypes();
    if (config.getKeyClass() != null) {
        builder.keyType(config.getKeyClass());
    }
    builder.valueType(CacheObject.class);
    builder.permitNullValues(config.isNullable());
    builder.entryCapacity(config.getCapacity() > 0 ? config.getCapacity() : Long.MAX_VALUE);

    if (config.getExpireAfterWrite() > 0) {
        builder.expireAfterWrite(config.getExpireAfterWrite(), TimeUnit.MILLISECONDS);
    } else {
        builder.eternal(true);
    }

    return new Cache2kCache<>(builder.build(), config);
}
 
Example 2
Source File: LocalCache.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructor to instantiate LocalCache object.
 *
 * @param cacheBuilder CacheBuilder instance
 */
@SuppressWarnings("unchecked")
public LocalCache( final CacheBuilder<V> cacheBuilder )
{
    Cache2kBuilder<?, ?> builder = Cache2kBuilder.forUnknownTypes();

    if ( cacheBuilder.isExpiryEnabled() )
    {
        builder.eternal( false );
        if ( cacheBuilder.isRefreshExpiryOnAccess() )
        {
            // TODO https://github.com/cache2k/cache2k/issues/39 is still
            // Open. Once the issue is resolved it can be updated here
            builder.expireAfterWrite( cacheBuilder.getExpiryInSeconds(), SECONDS );
        }
        else
        {
            builder.expireAfterWrite( cacheBuilder.getExpiryInSeconds(), SECONDS );
        }
    }
    else
    {
        builder.eternal( true );
    }
    if ( cacheBuilder.getMaximumSize() > 0 )
    {
        builder.entryCapacity( cacheBuilder.getMaximumSize() );
    }

    // Using unknown typed key for builder and casting it
    this.cache2kInstance = (org.cache2k.Cache<String, V>) builder.build();
    this.defaultValue = cacheBuilder.getDefaultValue();
}
 
Example 3
Source File: Cache2kFactory.java    From cache2k-benchmark with Apache License 2.0 5 votes vote down vote up
private <K,V> Cache<K, V> createInternal(final Class<K> _keyType, final Class<V> _valueType, final int _maxElements, final BenchmarkCacheLoader<K, V> _source) {
  Cache2kBuilder<K, V> b =
    Cache2kBuilder.of(_keyType, _valueType)
      .name("testCache-" + counter.incrementAndGet())
      .entryCapacity(_maxElements)
      .refreshAhead(false)
      .strictEviction(strictEviction);
  if (withExpiry) {
    b.expireAfterWrite(2 * 60, TimeUnit.SECONDS);
  } else {
    b.eternal(true);
  }
  if (disableStatistics) {
    b.disableStatistics(true).strictEviction(false).boostConcurrency(true);
  } else {
    b.strictEviction(true);
  }
  final AtomicInteger _evictCount = new AtomicInteger();
  if (_source != null) {
    b.loader(new CacheLoader<K, V>() {
      @Override
      public V load(final K key) throws Exception {
        return _source.load(key);
      }
    });
  }
  return b.build();
}
 
Example 4
Source File: TestingBase.java    From cache2k with Apache License 2.0 5 votes vote down vote up
protected <K, T> Cache<K, T> freshCache(
    Class<K> _keyClass, Class<T> _dataClass, CacheLoader g, long _maxElements, int _expiry) {
  Cache2kBuilder<K, T> b =
    builder(_keyClass, _dataClass).loader(g).refreshAhead(_expiry >= 0 && g != null);
  if (_expiry < 0) {
    b.eternal(true);
  } else {
    b.expireAfterWrite(_expiry, TimeUnit.SECONDS);
  }
  applyMaxElements(b, _maxElements);
  return cache = b.build();
}
 
Example 5
Source File: BasicCacheOperationsWithoutCustomizationsTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
protected Cache<Integer,Integer> createCache() {
  Cache2kBuilder b;
  if (pars.useObjectKey) {
    b = Cache2kBuilder.forUnknownTypes();
  } else {
    b = Cache2kBuilder.of(Integer.class, Integer.class);
  }
  b.name(this.getClass().getSimpleName() + "-" + pars.toString().replace('=', '~'))
    .retryInterval(Long.MAX_VALUE, TimeUnit.MILLISECONDS)
    .entryCapacity(1000)
    .permitNullValues(true)
    .keepDataAfterExpired(pars.keepDataAfterExpired)
    .recordRefreshedTime(pars.recordRefreshTime)
    .disableStatistics(pars.disableStatistics);
  if (pars.withExpiryAfterWrite) {
    b.expireAfterWrite(TestingParameters.MAX_FINISH_WAIT_MILLIS, TimeUnit.MILLISECONDS);
  } else {
    b.eternal(true);
  }
  if (pars.withWiredCache) {
    StaticUtil.enforceWiredCache(b);
  }
  if (pars.withExpiryListener) {
    b.addListener(new CacheEntryExpiredListener() {
      @Override
      public void onEntryExpired(final Cache cache, final CacheEntry entry) {

      }
    });
  }
  Cache<Integer,Integer> c = b.build();
  if (pars.withEntryProcessor) {
    c = new EntryProcessorCacheWrapper<Integer, Integer>(c);
  }
  if (pars.withForwardingAndAbstract) {
    c = wrapAbstractAndForwarding(c);
  }
  return c;
}