@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)); }
/** * Associates the {@code value} with the {@code key} in this cache if the specified key is not * already associated with a value. This method differs from {@link Map#putIfAbsent} by * substituting the configured {@link Expiry} with the specified write duration, has no effect * on the duration if the entry was present, and returns the success rather than a value. * * @param key the key with which the specified value is to be associated * @param value value to be associated with the specified key * @param duration the length of time from now when the entry should be automatically removed * @return <tt>true</tt> if this cache did not already contain the specified entry * @throws IllegalArgumentException if {@code duration} is negative */ default boolean putIfAbsent(@NonNull K key, @NonNull V value, @NonNull Duration duration) { // This method will be abstract in version 3.0.0 return putIfAbsent(key, value, duration.toNanos(), TimeUnit.NANOSECONDS); }
/** * Associates the {@code value} with the {@code key} in this cache if the specified key is not * already associated with a value. This method differs from {@link Map#putIfAbsent} by * substituting the configured {@link Expiry} with the specified write duration, has no effect * on the duration if the entry was present, and returns the success rather than a value. * * @param key the key with which the specified value is to be associated * @param value value to be associated with the specified key * @param duration the length of time from now when the entry should be automatically removed * @return <tt>true</tt> if this cache did not already contain the specified entry * @throws IllegalArgumentException if {@code duration} is negative */ default boolean putIfAbsent(@Nonnull K key, @Nonnull V value, @Nonnull Duration duration) { // This method will be abstract in version 3.0.0 return putIfAbsent(key, value, duration.toNanos(), TimeUnit.NANOSECONDS); }
@CheckNoWriter @CheckNoStats @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE) public void putIfAbsent_nullDuration(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.putIfAbsent(1, 2, null); }
@CheckNoWriter @CheckNoStats @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE) public void putIfAbsent_negativeDuration(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.putIfAbsent(1, 2, -10, TimeUnit.SECONDS); }
@CheckNoWriter @CheckNoStats @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE) public void putIfAbsent_nullKey(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.putIfAbsent(null, 2, 3, TimeUnit.SECONDS); }
@CheckNoWriter @CheckNoStats @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE) public void putIfAbsent_nullTimeUnit(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.putIfAbsent(1, 2, 3, null); }
@CheckNoWriter @CheckNoStats @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE) public void putIfAbsent_nullValue(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.putIfAbsent(1, null, 3, TimeUnit.SECONDS); }