protected boolean isCacheable(HttpRequest request, HttpResponse response) { if (!isCacheable(request)) { return false; } if (request.getCacheTtl() != -1) { // Caching was forced. Ignore what the response wants. return true; } if (response.getHttpStatusCode() == HttpResponse.SC_NOT_MODIFIED) { // Shindig server will serve 304s. Do not cache 304s from the origin server. return false; } // If the HTTP response allows for it, we can cache. return !response.isStrictNoCache(); }
protected boolean isCacheable(HttpRequest request, HttpResponse response) { if (!isCacheable(request)) { return false; } if (request.getCacheTtl() != -1) { // Caching was forced. Ignore what the response wants. return true; } if (response.getHttpStatusCode() == HttpResponse.SC_NOT_MODIFIED) { // Shindig server will serve 304s. Do not cache 304s from the origin server. return false; } // If the HTTP response allows for it, we can cache. return !response.isStrictNoCache(); }
protected boolean isCacheable(HttpRequest request, HttpResponse response, boolean allowStrictNoCacheResponses) { if (!isCacheable(request)) { return false; } if (request.getCacheTtl() != -1) { // Caching was forced. Ignore what the response wants. return true; } if (response.getHttpStatusCode() == HttpResponse.SC_NOT_MODIFIED) { // Shindig server will serve 304s. Do not cache 304s from the origin server. return false; } // If we allow strict no-cache responses or the HTTP response allows for it, we can cache. return allowStrictNoCacheResponses || !response.isStrictNoCache(); }
public boolean shouldRefetch() { // Time after which resource should be refetched. long refetchExpiration = isStrictNoCache() ? date + getRefetchStrictNoCacheAfterMs() : getCacheExpiration(); return refetchExpiration <= getTimeSource().currentTimeMillis(); }
public HttpResponse getResponse(HttpRequest request) { if (isCacheable(request)) { String keyString = createKey(request); HttpResponse cached = getResponseImpl(keyString); if (responseStillUsable(cached) && (!cached.isStrictNoCache() || refetchStrictNoCacheAfterMs >= 0)) { return cached; } } return null; }
@Test public void testCachingHeadersIgnoredOnError() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .setHttpStatusCode(404) .create(); assertFalse(response.isStrictNoCache()); response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .setHttpStatusCode(403) .create(); assertTrue(response.isStrictNoCache()); response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .setHttpStatusCode(401) .create(); assertTrue(response.isStrictNoCache()); }
@Test public void testCachingHeadersIgnoredOnError() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .setHttpStatusCode(404) .create(); assertFalse(response.isStrictNoCache()); response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .setHttpStatusCode(403) .create(); assertTrue(response.isStrictNoCache()); response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .setHttpStatusCode(401) .create(); assertTrue(response.isStrictNoCache()); }
@Test public void buildStrictNoCacheHttpResponseWithPragmaHeader() { HttpResponse response = new HttpResponseBuilder() .setResponseString("result") .addHeader("Pragma", "no-cache") .create(); assertTrue(response.isStrictNoCache()); HttpResponse builtResponse = cache .buildStrictNoCacheHttpResponse(response).create(); assertTrue(builtResponse.isStrictNoCache()); assertEquals("", builtResponse.getResponseAsString()); assertNull(builtResponse.getHeader("Cache-Control")); assertEquals("no-cache", builtResponse.getHeader("Pragma")); }
@Test public void buildStrictNoCacheHttpResponseWithPragmaHeader() { HttpResponse response = new HttpResponseBuilder() .setResponseString("result") .addHeader("Pragma", "no-cache") .create(); assertTrue(response.isStrictNoCache()); HttpResponse builtResponse = cache .buildStrictNoCacheHttpResponse(response).create(); assertTrue(builtResponse.isStrictNoCache()); assertEquals("", builtResponse.getResponseAsString()); assertNull(builtResponse.getHeader("Cache-Control")); assertEquals("no-cache", builtResponse.getHeader("Pragma")); }
@Test public void testStrictPragmaNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testStrictCacheControlNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testStrictCacheControlNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testStrictCacheControlNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testStrictPragmaNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testStrictPragmaNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void buildStrictNoCacheHttpResponse() { HttpResponse response = new HttpResponseBuilder() .setResponseString("result") .addHeader("Cache-Control", "private, max-age=1000") .addHeader("X-Method-Override", "GET") .create(); assertTrue(response.isStrictNoCache()); HttpResponse builtResponse = extendedStrictNoCacheTtlCache .buildStrictNoCacheHttpResponse(response).create(); assertTrue(builtResponse.isStrictNoCache()); assertEquals("", builtResponse.getResponseAsString()); assertEquals("private, max-age=1000", builtResponse.getHeader("Cache-Control")); assertEquals(86400, builtResponse.getRefetchStrictNoCacheAfterMs()); assertFalse(builtResponse.getHeaders().containsKey("Pragma")); assertNull(builtResponse.getHeader("X-Method-Override")); }
@Test public void buildStrictNoCacheHttpResponse() { HttpResponse response = new HttpResponseBuilder() .setResponseString("result") .addHeader("Cache-Control", "private, max-age=1000") .addHeader("X-Method-Override", "GET") .create(); assertTrue(response.isStrictNoCache()); HttpResponse builtResponse = extendedStrictNoCacheTtlCache .buildStrictNoCacheHttpResponse(response).create(); assertTrue(builtResponse.isStrictNoCache()); assertEquals("", builtResponse.getResponseAsString()); assertEquals("private, max-age=1000", builtResponse.getHeader("Cache-Control")); assertEquals(86400, builtResponse.getRefetchStrictNoCacheAfterMs()); assertFalse(builtResponse.getHeaders().containsKey("Pragma")); assertNull(builtResponse.getHeader("X-Method-Override")); }
@Test public void testStrictPragmaJunk() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "junk") .create(); assertFalse(response.isStrictNoCache()); int expected = roundToSeconds(timeSource.currentTimeMillis() + response.getDefaultTtl()); int expires = roundToSeconds(response.getCacheExpiration()); assertEquals(expected, expires); assertTrue(response.getCacheTtl() <= response.getDefaultTtl() && response.getCacheTtl() > 0); }
@Test public void testStrictPragmaJunk() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "junk") .create(); assertFalse(response.isStrictNoCache()); int expected = roundToSeconds(timeSource.currentTimeMillis() + response.getDefaultTtl()); int expires = roundToSeconds(response.getCacheExpiration()); assertEquals(expected, expires); assertTrue(response.getCacheTtl() <= response.getDefaultTtl() && response.getCacheTtl() > 0); }
@Test public void testStrictPragmaJunk() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "junk") .create(); assertFalse(response.isStrictNoCache()); int expected = roundToSeconds(timeSource.currentTimeMillis() + response.getDefaultTtl()); int expires = roundToSeconds(response.getCacheExpiration()); assertEquals(expected, expires); assertTrue(response.getCacheTtl() <= response.getDefaultTtl() && response.getCacheTtl() > 0); }