/** * Specifies that the entry should be automatically removed from the cache once the duration has * elapsed. The expiration policy determines when the entry's age is reset. * * @param key the key for the entry being set * @param duration the length of time from now when the entry should be automatically removed * @throws IllegalArgumentException if {@code duration} is negative */ default void setExpiresAfter(@NonNull K key, @NonNull Duration duration) { // This method will be abstract in version 3.0.0 setExpiresAfter(key, duration.toNanos(), TimeUnit.NANOSECONDS); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.MOCKITO, expiryTime = Expire.ONE_MINUTE) public void setExpiresAfter_duration(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.setExpiresAfter(context.firstKey(), Duration.ofMinutes(2L)); assertThat(expireAfterVar.getExpiresAfter(context.firstKey()), is(Optional.of(Duration.ofMinutes(2L)))); expireAfterVar.setExpiresAfter(context.absentKey(), Duration.ofMinutes(4L)); assertThat(expireAfterVar.getExpiresAfter(context.absentKey()), is(Optional.empty())); context.ticker().advance(90, TimeUnit.SECONDS); cache.cleanUp(); assertThat(cache.estimatedSize(), is(1L)); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.MOCKITO, expiryTime = Expire.ONE_MINUTE) public void setExpiresAfter(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.setExpiresAfter(context.firstKey(), 2, TimeUnit.MINUTES); assertThat(expireAfterVar.getExpiresAfter(context.firstKey(), TimeUnit.MINUTES), is(OptionalLong.of(2))); expireAfterVar.setExpiresAfter(context.absentKey(), 4, TimeUnit.MINUTES); assertThat(expireAfterVar.getExpiresAfter(context.absentKey(), TimeUnit.MINUTES), is(OptionalLong.empty())); context.ticker().advance(90, TimeUnit.SECONDS); cache.cleanUp(); assertThat(cache.estimatedSize(), is(1L)); }
/** * Specifies that the entry should be automatically removed from the cache once the duration has * elapsed. The expiration policy determines when the entry's age is reset. * * @param key the key for the entry being set * @param duration the length of time from now when the entry should be automatically removed * @throws IllegalArgumentException if {@code duration} is negative */ default void setExpiresAfter(@Nonnull K key, @Nonnull Duration duration) { // This method will be abstract in version 3.0.0 setExpiresAfter(key, duration.toNanos(), TimeUnit.NANOSECONDS); }