@Test void localTimeout() throws InterruptedException { final String v1 = cache.computeIfAbsent(String.class, "key1", 200L, () -> "value1"); final String v2 = cache.computeIfAbsent(String.class, "key1", 200L, () -> "value2"); final String valueKey2 = cache.computeIfAbsent(String.class, "key2", -1L, () -> "valueKey2"); Thread.sleep(400L); final String v3 = cache.computeIfAbsent(String.class, "key1", 200L, () -> "value3"); final String value2Key2 = cache.computeIfAbsent(String.class, "key2", () -> "value2Key2"); assertEquals("value1", v1); assertEquals("value1", v2); assertEquals("value3", v3); assertEquals("valueKey2", valueKey2); assertEquals("valueKey2", value2Key2); }
@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();  }