@Test(expectedExceptions = IllegalStateException.class) public void refreshAfterWrite_twice() { Caffeine.newBuilder().refreshAfterWrite(1, TimeUnit.MILLISECONDS) .refreshAfterWrite(1, TimeUnit.MILLISECONDS); }
private void configureRefreshAfterWrite() { if (config.getRefreshAfterWrite().isPresent()) { caffeine.refreshAfterWrite(config.getRefreshAfterWrite().getAsLong(), TimeUnit.NANOSECONDS); } } }
@Test(expectedExceptions = IllegalStateException.class) public void refreshAfterWrite_duration_twice() { Caffeine.newBuilder().refreshAfterWrite(Duration.ofMillis(1)) .refreshAfterWrite(Duration.ofMillis(1)); }
/** * Specifies that active entries are eligible for automatic refresh once a fixed duration has * elapsed after the entry's creation, or the most recent replacement of its value. The semantics * of refreshes are specified in {@link LoadingCache#refresh}, and are performed by calling {@link * CacheLoader#reload}. * <p> * Automatic refreshes are performed when the first stale request for an entry occurs. The request * triggering refresh will make an asynchronous call to {@link CacheLoader#reload} and immediately * return the old value. * <p> * <b>Note:</b> <i>all exceptions thrown during refresh will be logged and then swallowed</i>. * * @param duration the length of time after an entry is created that it should be considered * stale, and thus eligible for refresh * @return this {@code Caffeine} instance (for chaining) * @throws IllegalArgumentException if {@code duration} is negative * @throws IllegalStateException if the refresh interval was already set * @throws ArithmeticException for durations greater than +/- approximately 292 years */ @NonNull public Caffeine<K, V> refreshAfterWrite(@NonNull Duration duration) { return refreshAfterWrite(duration.toNanos(), TimeUnit.NANOSECONDS); }
@Test(expectedExceptions = IllegalArgumentException.class) public void refreshAfterWrite_zero() { Caffeine.newBuilder().refreshAfterWrite(0, TimeUnit.MILLISECONDS); }
@Test(expectedExceptions = IllegalArgumentException.class) public void refreshAfterWrite_duration_negative() { Caffeine.newBuilder().refreshAfterWrite(Duration.ofMillis(-1)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void refreshAfterWrite_duration_zero() { Caffeine.newBuilder().refreshAfterWrite(Duration.ofMillis(0)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void refreshAfterWrite_negative() { Caffeine.newBuilder().refreshAfterWrite(-1, TimeUnit.MILLISECONDS); }
@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) { } }
@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) {} }
@Test(expectedExceptions = IllegalStateException.class) public void refreshAfterWrite_duration_noCacheLoader() { Caffeine.newBuilder().refreshAfterWrite(Duration.ofMillis(1)).build(); }
@Test public void refreshAfterWrite_duration() { Caffeine<Object, Object> builder = Caffeine.newBuilder() .refreshAfterWrite(Duration.ofMillis(1)); assertThat(builder.getRefreshAfterWriteNanos(), is(TimeUnit.MILLISECONDS.toNanos(1))); builder.build(k -> k); }
@Test public void refreshAfterWrite() { Caffeine<Object, Object> builder = Caffeine.newBuilder() .refreshAfterWrite(1, TimeUnit.MILLISECONDS); assertThat(builder.getRefreshAfterWriteNanos(), is(TimeUnit.MILLISECONDS.toNanos(1))); builder.build(k -> k); }
@Test(expectedExceptions = IllegalStateException.class) public void refreshAfterWrite_noCacheLoader() { Caffeine.newBuilder().refreshAfterWrite(1, TimeUnit.MILLISECONDS).build(); }
@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) { } }
@Test public void configured() { Caffeine<Object, Object> configured = Caffeine.newBuilder() .initialCapacity(1).weakKeys() .expireAfterAccess(1, TimeUnit.SECONDS).expireAfterWrite(1, TimeUnit.SECONDS) .removalListener((k, v, c) -> {}).recordStats(); assertThat(configured.build(), is(not(nullValue()))); assertThat(configured.buildAsync(), is(not(nullValue()))); assertThat(configured.build(loader), is(not(nullValue()))); assertThat(configured.buildAsync(loader), is(not(nullValue()))); assertThat(configured.refreshAfterWrite(1, TimeUnit.SECONDS).toString(), is(not(Caffeine.newBuilder().toString()))); assertThat(Caffeine.newBuilder().maximumSize(1).toString(), is(not(Caffeine.newBuilder().maximumWeight(1).toString()))); }
@GwtIncompatible("refreshAfterWrite") public void testRefresh_zero() { Caffeine<Object, Object> builder = Caffeine.newBuilder(); try { builder.refreshAfterWrite(0, SECONDS); fail(); } catch (IllegalArgumentException expected) {} }
builder.refreshAfterWrite(refreshAfterWriteDuration, refreshAfterWriteTimeUnit);
builder.refreshAfterWrite(refresh.duration, refresh.unit);
@GwtIncompatible // java.time.Duration public void testLargeDurations() { java.time.Duration threeHundredYears = java.time.Duration.ofDays(365 * 300); Caffeine<Object, Object> builder = Caffeine.newBuilder(); try { builder.expireAfterWrite(threeHundredYears); fail(); } catch (ArithmeticException expected) { } try { builder.expireAfterAccess(threeHundredYears); fail(); } catch (ArithmeticException expected) { } try { builder.refreshAfterWrite(threeHundredYears); fail(); } catch (ArithmeticException expected) { } }