@Test public void testCachingNoCache() throws Exception { CachedActionInterceptor interceptor = new CachedActionInterceptor(); interceptor.cache = new DummyCache(); Cached cached = mock(Cached.class); when(cached.duration()).thenReturn(10); when(cached.key()).thenReturn("key"); RequestContext context = mock(RequestContext.class); when(context.request()).thenReturn(mock(Request.class)); Context ctx = mock(Context.class); when(context.context()).thenReturn(ctx); when(context.context().header(anyString())).thenReturn(null); final Result r = Results.ok("Result"); when(context.proceed()).thenReturn(r); Result result = interceptor.call(cached, context); assertThat(result.getRenderable().<String>content()).isEqualTo("Result"); assertThat(result).isEqualTo(r); final Result r2 = Results.ok("Result2"); when(context.proceed()).thenReturn(r2); result = interceptor.call(cached, context); assertThat(result).isEqualTo(r); when(context.context().header(HeaderNames.CACHE_CONTROL)).thenReturn(HeaderNames.NOCACHE_VALUE); result = interceptor.call(cached, context); assertThat(result).isNotEqualTo(r).isEqualTo(r2); final Result r3 = Results.ok("Result3"); when(context.proceed()).thenReturn(r3); when(context.context().header(HeaderNames.CACHE_CONTROL)).thenReturn(null); result = interceptor.call(cached, context); assertThat(result).isEqualTo(r2).isNotEqualTo(r3); }
@Override public Result call(Cached configuration, RequestContext context) throws Exception { boolean nocache = HeaderNames.NOCACHE_VALUE.equalsIgnoreCase(context.context().header(HeaderNames.CACHE_CONTROL)); String key; if (Strings.isNullOrEmpty(configuration.key())) { key = context.request().uri(); } else { key = configuration.key(); } Result result = null; if (!nocache) { result = (Result) cache.get(key); } if (result == null) { result = context.proceed(); } else { LOGGER.info("Returning cached result for {} (key:{})", context.request().uri(), key); return result; } Duration duration; if (configuration.duration() == 0) { duration = Duration.standardDays(365); } else { duration = Duration.standardSeconds(configuration.duration()); } cache.set(key, result, duration); LoggerFactory.getLogger(this.getClass()).info("Caching result of {} for {} seconds (key:{})", context.request().uri(), configuration.duration(), key); return result; }
CachedActionInterceptor extends Interceptor<Cached> { @Override public Result call(Cached configuration, RequestContext context) throws Exception { boolean nocache = HeaderNames.NOCACHE_VALUE.equalsIgnoreCase(context.context().header(HeaderNames.CACHE_CONTROL)); String key; if (Strings.isNullOrEmpty(configuration.key())) { key = context.request().uri(); } else { key = configuration.key(); } Result result = null; if (!nocache) { result = (Result) cache.get(key); } if (result == null) { result = context.proceed(); } else { LOGGER.info("Returning cached result for {} (key:{})", context.request().uri(), key); return result; } Duration duration; if (configuration.duration() == 0) { duration = Duration.standardDays(365); } else { duration = Duration.standardSeconds(configuration.duration()); } cache.set(key, result, duration); LoggerFactory.getLogger(this.getClass()).info("Caching result of {} for {} seconds (key:{})", context.request().uri(), configuration.duration(), key); return result; } }
CachedActionInterceptor extends Interceptor<Cached> { @Override public Result call(Cached configuration, RequestContext context) throws Exception { boolean nocache = HeaderNames.NOCACHE_VALUE.equalsIgnoreCase(context.context().header(HeaderNames.CACHE_CONTROL)); String key; if (Strings.isNullOrEmpty(configuration.key())) { key = context.request().uri(); } else { key = configuration.key(); } Result result = null; if (!nocache) { result = (Result) cache.get(key); } if (result == null) { result = context.proceed(); } else { LOGGER.info("Returning cached result for {} (key:{})", context.request().uri(), key); return result; } Duration duration; if (configuration.duration() == 0) { duration = Duration.standardDays(365); } else { duration = Duration.standardSeconds(configuration.duration()); } cache.set(key, result, duration); LoggerFactory.getLogger(this.getClass()).info("Caching result of {} for {} seconds (key:{})", context.request().uri(), configuration.duration(), key); return result; }  }
CachedActionInterceptor extends Interceptor<Cached> { @Override public Result call(Cached configuration, RequestContext context) throws Exception { boolean nocache = HeaderNames.NOCACHE_VALUE.equalsIgnoreCase(context.context().header(HeaderNames.CACHE_CONTROL)); String key; if (Strings.isNullOrEmpty(configuration.key())) { key = context.request().uri(); } else { key = configuration.key(); } Result result = null; if (!nocache) { result = (Result) cache.get(key); } if (result == null) { result = context.proceed(); } else { LOGGER.info("Returning cached result for {} (key:{})", context.request().uri(), key); return result; } Duration duration; if (configuration.duration() == 0) { duration = Duration.standardDays(365); } else { duration = Duration.standardSeconds(configuration.duration()); } cache.set(key, result, duration); LoggerFactory.getLogger(this.getClass()).info("Caching result of {} for {} seconds (key:{})", context.request().uri(), configuration.duration(), key); return result; }  @Override Result call(Cached configuration, RequestContext context); @Override Class<Cached> annotation(); }
CachedActionInterceptor extends Interceptor<Cached> { @Override public Result call(Cached configuration, RequestContext context) throws Exception { boolean nocache = HeaderNames.NOCACHE_VALUE.equalsIgnoreCase(context.context().header(HeaderNames.CACHE_CONTROL)); String key; if (Strings.isNullOrEmpty(configuration.key())) { key = context.request().uri(); } else { key = configuration.key(); } Result result = null; if (!nocache) { result = (Result) cache.get(key); } if (result == null) { result = context.proceed(); } else { LOGGER.info("Returning cached result for {} (key:{})", context.request().uri(), key); return result; } Duration duration; if (configuration.duration() == 0) { duration = Duration.standardDays(365); } else { duration = Duration.standardSeconds(configuration.duration()); } cache.set(key, result, duration); LoggerFactory.getLogger(this.getClass()).info("Caching result of {} for {} seconds (key:{})", context.request().uri(), configuration.duration(), key); return result; }  @Override Result call(Cached configuration, RequestContext context); @Override Class<Cached> annotation();  }