MemcacheStateRepository implements StateRepository { @Override public FeatureState getFeatureState(Feature feature) { CacheEntry entry = (CacheEntry) cache.get(key(feature.name())); if (entry != null) { return entry.getState() != null ? entry.getState() : null; } FeatureState featureState = delegate.getFeatureState(feature); cache.put(key(feature.name()), new CacheEntry(featureState != null ? featureState : null), getExpiration()); return featureState; } MemcacheStateRepository(StateRepository delegate);  MemcacheStateRepository(StateRepository delegate, int ttlInSeconds); @Override FeatureState getFeatureState(Feature feature); @Override void setFeatureState(FeatureState featureState);  }
@Test public void testCacheExpiryBecauseOfTimeToLife() throws InterruptedException { int ttl = 5; MemcacheStateRepository repository = new MemcacheStateRepository(delegate, ttl); for (int i = 0; i < 5; i++) { assertTrue(repository.getFeatureState(TestFeature.F1).isEnabled()); Thread.sleep(ttl + 10); } Mockito.verify(delegate, Mockito.times(5)).getFeatureState(TestFeature.F1); Mockito.verifyNoMoreInteractions(delegate); }