CachingLookup implements Lookup<T> { @Override public T lookup(String key) { return cache.getUnchecked(key); } CachingLookup(final Lookup<T> delegate, CacheConfig cacheConfig); @Override T lookup(String key); @Override Map<String, T> lookup(String... keys); @Override Map<String, T> lookup(Set<String> keys);  }
@Test public void testExpiryByTime() throws InterruptedException { Map<String, String> backing = new HashMap<>(); backing.put("foo", "1"); Lookup<String> delegate = new MapLookup<>(backing); CacheConfig config = new CacheConfig(1, 10); CachingLookup<String> lookup = new CachingLookup<>(delegate, config); Assert.assertEquals("1", lookup.lookup("foo")); backing.put("foo", "2"); Assert.assertEquals("1", lookup.lookup("foo")); Thread.sleep(1100); Assert.assertEquals("2", lookup.lookup("foo")); }
@Test public void testExpiryByFull() { Map<String, String> backing = new HashMap<>(); for (int i = 1; i <= 100; i++) { backing.put("foo" + i, Integer.toString(i)); } Lookup<String> delegate = new MapLookup<>(backing); CacheConfig config = new CacheConfig(0, 10); CachingLookup<String> lookup = new CachingLookup<>(delegate, config); Assert.assertEquals("1", lookup.lookup("foo1")); for (int i = 1; i <= 100; i++) { Assert.assertEquals(Integer.toString(i), lookup.lookup("foo" + i)); } backing.put("foo1", "sdf"); Assert.assertEquals("sdf", lookup.lookup("foo1")); }
@Test public void testBatch() throws InterruptedException { Map<String, String> backing = new HashMap<>(); for (int i = 1; i <= 100; i++) { backing.put("foo" + i, Integer.toString(i)); } Lookup<String> delegate = new MapLookup<>(backing); CacheConfig config = new CacheConfig(1, 10); CachingLookup<String> lookup = new CachingLookup<>(delegate, config); Assert.assertEquals( ImmutableMap.of("foo1", "1", "foo2", "2", "foo4", "4"), lookup.lookup("foo1", "foo4", "foo2")); backing.put("foo2", "sdf"); Assert.assertEquals( ImmutableMap.of("foo1", "1", "foo2", "2", "foo4", "4"), lookup.lookup("foo1", "foo4", "foo2")); Thread.sleep(1100); Assert.assertEquals( ImmutableMap.of("foo1", "1", "foo2", "sdf", "foo4", "4"), lookup.lookup("foo1", "foo4", "foo2")); }