/** * Returns the duration until the entry should be automatically removed. The expiration policy * determines when the entry's duration is reset. * * @param key the key for the entry being queried * @return the duration if the entry is present in the cache */ @NonNull default Optional<Duration> getExpiresAfter(@NonNull K key) { // This method will be abstract in version 3.0.0 OptionalLong duration = getExpiresAfter(key, TimeUnit.NANOSECONDS); return duration.isPresent() ? Optional.of(Duration.ofNanos(duration.getAsLong())) : Optional.empty(); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE) public void putIfAbsent_insert(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { Integer key = context.absentKey(); Integer value = context.absentValue(); assertThat(expireAfterVar.putIfAbsent(key, value, Duration.ofMinutes(2L)), is(true)); assertThat(cache.getIfPresent(key), is(value)); assertThat(expireAfterVar.getExpiresAfter(key), is(Optional.of(Duration.ofMinutes(2L)))); 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.WRITE, expiryTime = Expire.ONE_MINUTE) public void putIfAbsent_present(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { Integer key = context.firstKey(); Integer value = context.absentValue(); assertThat(expireAfterVar.putIfAbsent(key, value, Duration.ofMinutes(2L)), is(false)); assertThat(cache.getIfPresent(key), is(context.original().get(key))); assertThat(expireAfterVar.getExpiresAfter(key), is(Optional.of(Duration.ofMinutes(1L)))); context.ticker().advance(90, TimeUnit.SECONDS); cache.cleanUp(); assertThat(cache.estimatedSize(), is(0L)); }
/** * Returns the duration until the entry should be automatically removed. The expiration policy * determines when the entry's duration is reset. * * @param key the key for the entry being queried * @return the duration if the entry is present in the cache */ @Nonnull default Optional<Duration> getExpiresAfter(@Nonnull K key) { // This method will be abstract in version 3.0.0 OptionalLong duration = getExpiresAfter(key, TimeUnit.NANOSECONDS); return duration.isPresent() ? Optional.of(Duration.ofNanos(duration.getAsLong())) : Optional.empty(); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.MOCKITO, expiryTime = Expire.ONE_MINUTE) public void getExpiresAfter_duration(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { assertThat(expireAfterVar.getExpiresAfter(context.absentKey()), is(Optional.empty())); assertThat(expireAfterVar.getExpiresAfter(context.firstKey()), is(Optional.of(Duration.ofMinutes(1L)))); when(context.expiry().expireAfterUpdate(any(), any(), anyLong(), anyLong())) .thenReturn(TimeUnit.HOURS.toNanos(1)); cache.put(context.firstKey(), context.absentValue()); assertThat(expireAfterVar.getExpiresAfter(context.firstKey()), is(Optional.of(Duration.ofHours(1)))); assertThat(expireAfterVar.getExpiresAfter(context.lastKey()), is(Optional.of(Duration.ofMinutes(1)))); }
@CacheSpec(implementation = Implementation.Caffeine, expiryTime = Expire.ONE_MINUTE, population = Population.FULL, expiry = CacheExpiry.MOCKITO) @Test(dataProvider = "caches", expectedExceptions = ExpirationException.class) public void put_insert_replaceExpired_expiryFails(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireVariably) { OptionalLong duration = expireVariably.getExpiresAfter(context.firstKey(), NANOSECONDS); try { context.ticker().advance(1, TimeUnit.HOURS); when(context.expiry().expireAfterCreate(any(), any(), anyLong())) .thenThrow(ExpirationException.class); cache.put(context.firstKey(), context.absentValue()); } finally { context.ticker().advance(-1, TimeUnit.HOURS); assertThat(cache.asMap(), equalTo(context.original())); assertThat(expireVariably.getExpiresAfter(context.firstKey(), NANOSECONDS), is(duration)); } }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.MOCKITO, expiryTime = Expire.ONE_MINUTE) public void getExpiresAfter(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { assertThat(expireAfterVar.getExpiresAfter(context.absentKey(), TimeUnit.MINUTES), is(OptionalLong.empty())); assertThat(expireAfterVar.getExpiresAfter(context.firstKey(), TimeUnit.MINUTES), is(OptionalLong.of(1))); when(context.expiry().expireAfterUpdate(any(), any(), anyLong(), anyLong())) .thenReturn(TimeUnit.HOURS.toNanos(1)); cache.put(context.firstKey(), context.absentValue()); assertThat(expireAfterVar.getExpiresAfter(context.firstKey(), TimeUnit.MINUTES), is(OptionalLong.of(60))); assertThat(expireAfterVar.getExpiresAfter(context.lastKey(), TimeUnit.MINUTES), is(OptionalLong.of(1))); }
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.MOCKITO) @Test(dataProvider = "caches", expectedExceptions = ExpirationException.class) public void put_update_expiryFails(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireVariably) { OptionalLong duration = expireVariably.getExpiresAfter(context.firstKey(), NANOSECONDS); try { context.ticker().advance(1, TimeUnit.HOURS); when(context.expiry().expireAfterUpdate(any(), any(), anyLong(), anyLong())) .thenThrow(ExpirationException.class); cache.put(context.firstKey(), context.absentValue()); } finally { context.ticker().advance(-1, TimeUnit.HOURS); assertThat(cache.asMap(), equalTo(context.original())); assertThat(expireVariably.getExpiresAfter(context.firstKey(), NANOSECONDS), is(duration)); } }
@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)); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE) public void put_replace(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { Integer key = context.firstKey(); Integer value = context.absentValue(); expireAfterVar.put(key, value, Duration.ofMinutes(2L)); assertThat(cache.getIfPresent(key), is(value)); assertThat(expireAfterVar.getExpiresAfter(key), is(Optional.of(Duration.ofMinutes(2L)))); 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.WRITE, expiryTime = Expire.ONE_MINUTE) public void put_insert(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { Integer key = context.absentKey(); Integer value = context.absentValue(); expireAfterVar.put(key, value, Duration.ofMinutes(2L)); assertThat(cache.getIfPresent(key), is(value)); assertThat(expireAfterVar.getExpiresAfter(key), is(Optional.of(Duration.ofMinutes(2L)))); 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_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)); }