TimeBasedCache { public V get(K key) { V value = _cache.get(key); if (value == null && mayReload()) { reloadCache(); value = _cache.get(key); } return value; } TimeBasedCache(Duration minTimeBetweenReloads, Supplier<ImmutableMap<K, V>> valuesSupplier);  TimeBasedCache(Duration minTimeBetweenReloads,
                          Supplier<ImmutableMap<K, V>> valuesSupplier,
                          ImmutableMap<K, V> initialCache,
                          Clock clock); V get(K key); void clear(); Optional<Instant> getLastReloadInstant(); Duration getMinTimeBetweenReloads();  }
@Test public void doesNotReloadCacheWithinTimeLimit() throws Exception { Clock fakeClock = mock(Clock.class); Instant now = Instant.now(); when(fakeClock.millis()).thenCallRealMethod(); when(fakeClock.instant()).thenReturn( now, now, now.plus(Duration.ofSeconds(4)), now.plus(Duration.ofSeconds(4)), now.plus(Duration.ofSeconds(8))); @SuppressWarnings("unchecked") Supplier<ImmutableMap> fakeSupplier = mock(Supplier.class); when(fakeSupplier.get()).thenReturn( ImmutableMap.of("0", 0), ImmutableMap.of("1", 1)); AtomicInteger reloads = new AtomicInteger(0); TimeBasedCache<String, Integer> cache = new TimeBasedCache<>(Duration.ofSeconds(5), () -> ImmutableMap.copyOf(ImmutableMap.of(Integer.toString(reloads.get()), reloads.getAndIncrement())), ImmutableMap.of(), fakeClock); assertNotNull(cache.get("0")); assertNull(cache.get("1")); assertNotNull(cache.get("0")); assertNull(cache.get("1")); assertNotNull(cache.get("1")); assertNull(cache.get("0")); }