@Test void timeoutAuto() { this.interval = 200; final CountDownLatch latch = new CountDownLatch(1); final long start = System.currentTimeMillis(); String value = cache.computeIfAbsent(String.class, "foo", (Element e) -> { latch.countDown(); return true; }, () -> "bar"); assertEquals("bar", value); final String v2 = cache.computeIfAbsent(String.class, "foo", () -> "value2"); assertEquals("bar", v2); try { latch.await(); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); } final long end = System.currentTimeMillis(); final long duration = end - start; assertEquals(200, duration, 200); final String v3 = cache.computeIfAbsent(String.class, "foo", () -> "value3"); assertEquals("value3", v3); }
@Override public <T> T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove, final long timeoutMs, final Supplier<T> value) { final Integer maxSize = this.getConfigValue(CacheConfiguration::getDefaultMaxSize, -1); if (maxSize > 0 && this.cache.size() >= maxSize) { this.clean(); if (this.cache.size() >= maxSize) { synchronized (this.cache) { while (this.cache.size() >= maxSize) { final String keyToRemove = this.cache.keySet().iterator().next(); this.cache.remove(keyToRemove); } } } } final ScheduledFuture<?> task = timeoutMs > 0 ? this.evictionTask(key, timeoutMs) : null; final long endOfValidity = this.calcEndOfValidity(timeoutMs); final ElementImpl element = this.addToMap(key, () -> new ElementImpl(value, toRemove, endOfValidity, task, this.timer)); return element.getValue(expectedClass); }
LocalCacheService implements LocalCache, Serializable { @Override public <T> T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove, final long timeoutMs, final Supplier<T> value) { final Integer maxSize = this.getConfigValue(CacheConfiguration::getDefaultMaxSize, -1); if (maxSize > 0 && this.cache.size() >= maxSize) { this.clean(); if (this.cache.size() >= maxSize) { synchronized (this.cache) { while (this.cache.size() >= maxSize) { final String keyToRemove = this.cache.keySet().iterator().next(); this.cache.remove(keyToRemove); } } } } final ScheduledFuture<?> task = timeoutMs > 0 ? this.evictionTask(key, timeoutMs) : null; final long endOfValidity = this.calcEndOfValidity(timeoutMs); final ElementImpl element = this.addToMap(key, () -> new ElementImpl(value, toRemove, endOfValidity, task, this.timer)); return element.getValue(expectedClass); } }
LocalCacheService implements LocalCache, Serializable { @Override public <T> T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove, final long timeoutMs, final Supplier<T> value) { final Integer maxSize = this.getConfigValue(CacheConfiguration::getDefaultMaxSize, -1); if (maxSize > 0 && this.cache.size() >= maxSize) { this.clean(); if (this.cache.size() >= maxSize) { synchronized (this.cache) { while (this.cache.size() >= maxSize) { final String keyToRemove = this.cache.keySet().iterator().next(); this.cache.remove(keyToRemove); } } } } final ScheduledFuture<?> task = timeoutMs > 0 ? this.evictionTask(key, timeoutMs) : null; final long endOfValidity = this.calcEndOfValidity(timeoutMs); final ElementImpl element = this.addToMap(key, () -> new ElementImpl(value, toRemove, endOfValidity, task, this.timer)); return element.getValue(expectedClass); } LocalCacheService(final String plugin, final Supplier<Long> timer,
            final Supplier<ScheduledExecutorService> threadServiceGetter); }
LocalCacheService implements LocalCache, Serializable { @Override public <T> T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove, final long timeoutMs, final Supplier<T> value) { final Integer maxSize = this.getConfigValue(CacheConfiguration::getDefaultMaxSize, -1); if (maxSize > 0 && this.cache.size() >= maxSize) { this.clean(); if (this.cache.size() >= maxSize) { synchronized (this.cache) { while (this.cache.size() >= maxSize) { final String keyToRemove = this.cache.keySet().iterator().next(); this.cache.remove(keyToRemove); } } } } final ScheduledFuture<?> task = timeoutMs > 0 ? this.evictionTask(key, timeoutMs) : null; final long endOfValidity = this.calcEndOfValidity(timeoutMs); final ElementImpl element = this.addToMap(key, () -> new ElementImpl(value, toRemove, endOfValidity, task, this.timer)); return element.getValue(expectedClass); } LocalCacheService(final String plugin, final Supplier<Long> timer,
            final Supplier<ScheduledExecutorService> threadServiceGetter); @Override void evict(final String key); @Override void evictIfValue(final String key, final Object expected); @Override T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove,
            final long timeoutMs, final Supplier<T> value); @Override T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove,
            final Supplier<T> value); @Override T computeIfAbsent(final Class<T> expectedClass, final String key, final long timeoutMs,
            final Supplier<T> value); @Override T computeIfAbsent(final Class<T> expectedClass, final String key, final Supplier<T> value); @PreDestroy void release(); void clean(); }
LocalCacheService implements LocalCache, Serializable { @Override public <T> T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove, final long timeoutMs, final Supplier<T> value) { final Integer maxSize = this.getConfigValue(CacheConfiguration::getDefaultMaxSize, -1); if (maxSize > 0 && this.cache.size() >= maxSize) { this.clean(); if (this.cache.size() >= maxSize) { synchronized (this.cache) { while (this.cache.size() >= maxSize) { final String keyToRemove = this.cache.keySet().iterator().next(); this.cache.remove(keyToRemove); } } } } final ScheduledFuture<?> task = timeoutMs > 0 ? this.evictionTask(key, timeoutMs) : null; final long endOfValidity = this.calcEndOfValidity(timeoutMs); final ElementImpl element = this.addToMap(key, () -> new ElementImpl(value, toRemove, endOfValidity, task, this.timer)); return element.getValue(expectedClass); } LocalCacheService(final String plugin, final Supplier<Long> timer,
            final Supplier<ScheduledExecutorService> threadServiceGetter); @Override void evict(final String key); @Override void evictIfValue(final String key, final Object expected); @Override T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove,
            final long timeoutMs, final Supplier<T> value); @Override T computeIfAbsent(final Class<T> expectedClass, final String key, final Predicate<Element> toRemove,
            final Supplier<T> value); @Override T computeIfAbsent(final Class<T> expectedClass, final String key, final long timeoutMs,
            final Supplier<T> value); @Override T computeIfAbsent(final Class<T> expectedClass, final String key, final Supplier<T> value); @PreDestroy void release(); void clean();  }