@Test public void testExpireOnWrite() throws ExecutionException, InterruptedException { final String key = "testExpireOnWrite"; final int firstValue = 20; final int secondValue = 40; final int getFirstValue1 = cache.get(key, new ImmediateInteger(firstValue)); assertEquals(firstValue, getFirstValue1); Thread.sleep(timeoutMillis/2); final int getFirstValue2 = cache.get(key, new ImmediateInteger(firstValue)); assertEquals(firstValue, getFirstValue2); Thread.sleep(timeoutMillis + timeoutMillis/4); final int getSecondValue = cache.get(key, new ImmediateInteger(secondValue)); assertEquals(secondValue, getSecondValue); }
@Override public V get(final K key, final Callable<V> valueFetcher) throws ExecutionException { expireEntries(); final WrappedValue<V> newWrappedValue = new WrappedValue<>(valueFetcher, currentTime); final WrappedValue<V> existingWrappedValue = internalMap.putIfAbsent(key, newWrappedValue); if (existingWrappedValue == null) { return newWrappedValue.loadAndGet(); } else { final Optional<V> existingValue = existingWrappedValue.getValue(); if (existingValue.isPresent()) { return existingValue.get(); } else { return existingWrappedValue.waitAndGet(); } } }
CacheImpl implements Cache<K, V> { @Override public V get(final K key, final Callable<V> valueFetcher) throws ExecutionException { expireEntries(); final WrappedValue<V> newWrappedValue = new WrappedValue<>(valueFetcher, currentTime); final WrappedValue<V> existingWrappedValue = internalMap.putIfAbsent(key, newWrappedValue); if (existingWrappedValue == null) { return newWrappedValue.loadAndGet(); } else { final Optional<V> existingValue = existingWrappedValue.getValue(); if (existingValue.isPresent()) { return existingValue.get(); } else { return existingWrappedValue.waitAndGet(); } } } }
CacheImpl implements Cache<K, V> { @Override public V get(final K key, final Callable<V> valueFetcher) throws ExecutionException { expireEntries(); final WrappedValue<V> newWrappedValue = new WrappedValue<>(valueFetcher, currentTime); final WrappedValue<V> existingWrappedValue = internalMap.putIfAbsent(key, newWrappedValue); if (existingWrappedValue == null) { return newWrappedValue.loadAndGet(); } else { final Optional<V> existingValue = existingWrappedValue.getValue(); if (existingValue.isPresent()) { return existingValue.get(); } else { return existingWrappedValue.waitAndGet(); } } } @Inject  CacheImpl(final CurrentTime currentTime,
                   final long timeoutMillis); }
CacheImpl implements Cache<K, V> { @Override public V get(final K key, final Callable<V> valueFetcher) throws ExecutionException { expireEntries(); final WrappedValue<V> newWrappedValue = new WrappedValue<>(valueFetcher, currentTime); final WrappedValue<V> existingWrappedValue = internalMap.putIfAbsent(key, newWrappedValue); if (existingWrappedValue == null) { return newWrappedValue.loadAndGet(); } else { final Optional<V> existingValue = existingWrappedValue.getValue(); if (existingValue.isPresent()) { return existingValue.get(); } else { return existingWrappedValue.waitAndGet(); } } } @Inject  CacheImpl(final CurrentTime currentTime,
                   final long timeoutMillis); @Override V get(final K key, final Callable<V> valueFetcher); @Override void invalidate(final K key); }
CacheImpl implements Cache<K, V> { @Override public V get(final K key, final Callable<V> valueFetcher) throws ExecutionException { expireEntries(); final WrappedValue<V> newWrappedValue = new WrappedValue<>(valueFetcher, currentTime); final WrappedValue<V> existingWrappedValue = internalMap.putIfAbsent(key, newWrappedValue); if (existingWrappedValue == null) { return newWrappedValue.loadAndGet(); } else { final Optional<V> existingValue = existingWrappedValue.getValue(); if (existingValue.isPresent()) { return existingValue.get(); } else { return existingWrappedValue.waitAndGet(); } } } @Inject  CacheImpl(final CurrentTime currentTime,
                   final long timeoutMillis); @Override V get(final K key, final Callable<V> valueFetcher); @Override void invalidate(final K key);  }