@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)); }
/** * Associates the {@code value} with the {@code key} in this cache. If the cache previously * contained a value associated with the {@code key}, the old value is replaced by the new * {@code value}. This method differs from {@link Cache#put} by substituting the * configured {@link Expiry} with the specified write duration. * * @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 * @throws IllegalArgumentException if {@code duration} is negative */ default void put(@NonNull K key, @NonNull V value, @NonNull Duration duration) { // This method will be abstract in version 3.0.0 put(key, value, duration.toNanos(), TimeUnit.NANOSECONDS); }
@Override public <T> void put(String appid, String id, T object, Long ttlSeconds) { if (ttlSeconds == null || ttlSeconds <= 0L) { put(appid, id, object); return; } if (!StringUtils.isBlank(id) && object != null && !StringUtils.isBlank(appid)) { String key = key(appid, id); cache.policy().expireVariably().ifPresent((t) -> { t.put(key, object, ttlSeconds, TimeUnit.SECONDS); }); logger.debug("Cache.put() {} {} ttl {}", appid, id, ttlSeconds); } }
@Override public <T> void put(String appid, String id, T object, Long ttlSeconds) { if (ttlSeconds == null || ttlSeconds <= 0L) { put(appid, id, object); return; } if (!StringUtils.isBlank(id) && object != null && !StringUtils.isBlank(appid)) { String key = key(appid, id); cache.policy().expireVariably().ifPresent((t) -> { t.put(key, object, ttlSeconds, TimeUnit.SECONDS); }); logger.debug("Cache.put() {} {} ttl {}", appid, id, ttlSeconds); } }
/** * Associates the {@code value} with the {@code key} in this cache. If the cache previously * contained a value associated with the {@code key}, the old value is replaced by the new * {@code value}. This method differs from {@link Cache#put} by substituting the * configured {@link Expiry} with the specified write duration. * * @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 * @throws IllegalArgumentException if {@code duration} is negative */ default void put(@Nonnull K key, @Nonnull V value, @Nonnull Duration duration) { // This method will be abstract in version 3.0.0 put(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 put_nullDuration(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.put(1, 2, 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 put_nullTimeUnit(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.put(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 put_nullValue(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.put(1, null, 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 put_nullKey(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.put(null, 2, 3, TimeUnit.SECONDS); }
@CheckNoWriter @CheckNoStats @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE) public void put_negativeDuration(Cache<Integer, Integer> cache, CacheContext context, VarExpiration<Integer, Integer> expireAfterVar) { expireAfterVar.put(1, 2, -10, TimeUnit.SECONDS); }