Java Code Examples for com.github.benmanes.caffeine.cache.Caffeine#refreshAfterWrite()

The following examples show how to use com.github.benmanes.caffeine.cache.Caffeine#refreshAfterWrite() . 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: AuthCache.java    From timely with Apache License 2.0 6 votes vote down vote up
private static Cache<String, TimelyPrincipal> getCache() {
    if (-1 == cacheExpirationMinutes) {
        throw new IllegalStateException("Cache session max age not configured.");
    }
    if (null == CACHE) {
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder();
        caffeine.expireAfterWrite(cacheExpirationMinutes, TimeUnit.MINUTES);
        if (cacheRefreshMinutes > 0) {
            caffeine.refreshAfterWrite(cacheRefreshMinutes, TimeUnit.MINUTES);
            CACHE = caffeine.build(key -> getTimelyPrincipal(key));
        } else {
            CACHE = caffeine.build();
        }
    }
    return CACHE;
}
 
Example 2
Source File: CacheBuilderFactory.java    From caffeine with Apache License 2.0 5 votes vote down vote up
private Caffeine<Object, Object> createCacheBuilder(
    Integer concurrencyLevel, Integer initialCapacity, Integer maximumSize,
    DurationSpec expireAfterWrite, DurationSpec expireAfterAccess, DurationSpec refresh,
    Strength keyStrength, Strength valueStrength) {

  Caffeine<Object, Object> builder = Caffeine.newBuilder();
  if (concurrencyLevel != null) {
    //builder.concurrencyLevel(concurrencyLevel);
  }
  if (initialCapacity != null) {
    builder.initialCapacity(initialCapacity);
  }
  if (maximumSize != null) {
    builder.maximumSize(maximumSize);
  }
  if (expireAfterWrite != null) {
    builder.expireAfterWrite(expireAfterWrite.duration, expireAfterWrite.unit);
  }
  if (expireAfterAccess != null) {
    builder.expireAfterAccess(expireAfterAccess.duration, expireAfterAccess.unit);
  }
  if (refresh != null) {
    builder.refreshAfterWrite(refresh.duration, refresh.unit);
  }
  if (keyStrength == Strength.WEAK) {
    builder.weakKeys();
  }
  if (valueStrength == Strength.WEAK) {
    builder.weakValues();
  } else if (valueStrength == Strength.SOFT) {
    builder.softValues();
  }
  builder.executor(MoreExecutors.directExecutor());
  return builder;
}
 
Example 3
Source File: CacheBuilderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@GwtIncompatible("refreshAfterWrite")
public void testRefresh_zero() {
  Caffeine<Object, Object> builder = Caffeine.newBuilder();
  try {
    builder.refreshAfterWrite(0, SECONDS);
    fail();
  } catch (IllegalArgumentException expected) {}
}
 
Example 4
Source File: CacheBuilderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@GwtIncompatible // java.time.Duration
public void testRefresh_zero_duration() {
  Caffeine<Object, Object> builder = Caffeine.newBuilder();
  try {
    builder.refreshAfterWrite(java.time.Duration.ZERO);
    fail();
  } catch (IllegalArgumentException expected) {
  }
}
 
Example 5
Source File: CacheBuilderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@GwtIncompatible("refreshAfterWrite")
public void testRefresh_setTwice() {
  Caffeine<Object, Object> builder =
      Caffeine.newBuilder().refreshAfterWrite(3600, SECONDS);
  try {
    // even to the same value is not allowed
    builder.refreshAfterWrite(3600, SECONDS);
    fail();
  } catch (IllegalStateException expected) {}
}
 
Example 6
Source File: CacheBuilderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@GwtIncompatible // java.time.Duration
public void testRefresh_setTwice_duration() {
  Caffeine<Object, Object> builder =
      Caffeine.newBuilder().refreshAfterWrite(java.time.Duration.ofSeconds(3600));
  try {
    // even to the same value is not allowed
    builder.refreshAfterWrite(java.time.Duration.ofSeconds(3600));
    fail();
  } catch (IllegalStateException expected) {
  }
}
 
Example 7
Source File: CaffeineImpl.java    From RxCache with Apache License 2.0 4 votes vote down vote up
public CaffeineImpl(long maxSize, CacheConfig cacheConfig) {

        super(maxSize);
        Caffeine caffeine = Caffeine.newBuilder()
                .recordStats()
                .maximumSize(maxSize);

        if (cacheConfig!=null) {

            if (cacheConfig.expireDuration>0 && cacheConfig.expireTimeUnit!=null) {

                caffeine.expireAfterWrite(cacheConfig.expireDuration,cacheConfig.expireTimeUnit);
            }

            if (cacheConfig.refreshDuration>0 && cacheConfig.refreshTimeUnit!=null) {

                caffeine.refreshAfterWrite(cacheConfig.refreshDuration,cacheConfig.refreshTimeUnit);
            }
        }

        cache = caffeine.build();
    }
 
Example 8
Source File: CaffeineCacheFromContext.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public static <K, V> Cache<K, V> newCaffeineCache(CacheContext context) {
  Caffeine<Object, Object> builder = Caffeine.newBuilder();
  context.caffeine = builder;

  if (context.initialCapacity != InitialCapacity.DEFAULT) {
    builder.initialCapacity(context.initialCapacity.size());
  }
  if (context.isRecordingStats()) {
    builder.recordStats();
  }
  if (context.maximumSize != Maximum.DISABLED) {
    if (context.weigher == CacheWeigher.DEFAULT) {
      builder.maximumSize(context.maximumSize.max());
    } else {
      builder.weigher(context.weigher);
      builder.maximumWeight(context.maximumWeight());
    }
  }
  if (context.expiryType() != CacheExpiry.DISABLED) {
    builder.expireAfter(context.expiry);
  }
  if (context.afterAccess != Expire.DISABLED) {
    builder.expireAfterAccess(context.afterAccess.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.afterWrite != Expire.DISABLED) {
    builder.expireAfterWrite(context.afterWrite.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.refresh != Expire.DISABLED) {
    builder.refreshAfterWrite(context.refresh.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.expires() || context.refreshes()) {
    SerializableTicker ticker = context.ticker()::read;
    builder.ticker(ticker);
  }
  if (context.keyStrength == ReferenceType.WEAK) {
    builder.weakKeys();
  } else if (context.keyStrength == ReferenceType.SOFT) {
    throw new IllegalStateException();
  }
  if (context.isWeakValues()) {
    builder.weakValues();
  } else if (context.isSoftValues()) {
    builder.softValues();
  }
  if (context.cacheExecutor != CacheExecutor.DEFAULT) {
    builder.executor(context.executor);
  }
  if (context.cacheScheduler != CacheScheduler.DEFAULT) {
    builder.scheduler(context.scheduler);
  }
  if (context.removalListenerType != Listener.DEFAULT) {
    builder.removalListener(context.removalListener);
  }
  if (context.isStrongKeys() && !context.isAsync()) {
    builder.writer(context.cacheWriter());
  }
  if (context.isAsync()) {
    if (context.loader == null) {
      context.asyncCache = builder.buildAsync();
    } else {
      context.asyncCache = builder.buildAsync(
          context.isAsyncLoading ? context.loader.async() : context.loader);
    }
    context.cache = context.asyncCache.synchronous();
  } else if (context.loader == null) {
    context.cache = builder.build();
  } else {
    context.cache = builder.build(context.loader);
  }

  @SuppressWarnings("unchecked")
  Cache<K, V> castedCache = (Cache<K, V>) context.cache;
  RandomSeedEnforcer.resetThreadLocalRandom();
  return castedCache;
}