@Test public void testCaching() throws ExecutionException, InterruptedException { final int res1 = cachingFilter.handleAsync(context).get(); final int res2 = cachingFilter.handleAsync(context).get(); Assert.assertEquals(res1, res2); Assert.assertEquals(VALUE1.get(), res1); Thread.sleep(300); final int res3 = cachingFilter.handleAsync(context).get(); Assert.assertNotEquals(res2, res3); Assert.assertEquals(VALUE2.get(), res3); }
@Override public ComposableFuture<V> handleAsync(final AsyncRequestContext ctx) { final K key = generator.createKey(ctx.getParams()); return cache.getAsync(key).recover(error -> { return null; }).flatMap(result -> { if (result != null) { return ComposableFutures.fromValue(result); } else { return ctx.<V>invokeAsync().map(result1 -> { cache.setAsync(key, result1); return result1; }); } }); }
CachingFilter implements AsyncFilter<V, AsyncRequestContext> { @Override public ComposableFuture<V> handleAsync(final AsyncRequestContext ctx) { final K key = generator.createKey(ctx.getParams()); return cache.getAsync(key).recover(error -> { return null; }).flatMap(result -> { if (result != null) { return ComposableFutures.fromValue(result); } else { return ctx.<V>invokeAsync().map(result1 -> { cache.setAsync(key, result1); return result1; }); } }); } }
CachingFilter implements AsyncFilter<V, AsyncRequestContext> { @Override public ComposableFuture<V> handleAsync(final AsyncRequestContext ctx) { final K key = generator.createKey(ctx.getParams()); return cache.getAsync(key).recover(error -> { return null; }).flatMap(result -> { if (result != null) { return ComposableFutures.fromValue(result); } else { return ctx.<V>invokeAsync().map(result1 -> { cache.setAsync(key, result1); return result1; }); } }); } CachingFilter(final CacheKeyGenerator<K> generator, int cacheSize, int ttl, TimeUnit timeUnit); }
CachingFilter implements AsyncFilter<V, AsyncRequestContext> { @Override public ComposableFuture<V> handleAsync(final AsyncRequestContext ctx) { final K key = generator.createKey(ctx.getParams()); return cache.getAsync(key).recover(error -> { return null; }).flatMap(result -> { if (result != null) { return ComposableFutures.fromValue(result); } else { return ctx.<V>invokeAsync().map(result1 -> { cache.setAsync(key, result1); return result1; }); } }); } CachingFilter(final CacheKeyGenerator<K> generator, int cacheSize, int ttl, TimeUnit timeUnit); @Override ComposableFuture<V> handleAsync(final AsyncRequestContext ctx); }
CachingFilter implements AsyncFilter<V, AsyncRequestContext> { @Override public ComposableFuture<V> handleAsync(final AsyncRequestContext ctx) { final K key = generator.createKey(ctx.getParams()); return cache.getAsync(key).recover(error -> { return null; }).flatMap(result -> { if (result != null) { return ComposableFutures.fromValue(result); } else { return ctx.<V>invokeAsync().map(result1 -> { cache.setAsync(key, result1); return result1; }); } }); } CachingFilter(final CacheKeyGenerator<K> generator, int cacheSize, int ttl, TimeUnit timeUnit); @Override ComposableFuture<V> handleAsync(final AsyncRequestContext ctx);  }