com.android.volley.Cache Java Examples
The following examples show how to use
com.android.volley.Cache.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OkNetwork.java From OkVolley with Apache License 2.0 | 6 votes |
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) { // If there's no cache entry, we're done. if (entry == null) { return; } if (entry.etag != null) { headers.put("If-None-Match", entry.etag); } if (entry.serverDate > 0) { Date refTime = new Date(entry.serverDate); final SimpleDateFormat formatter = DateFormatHolder.formatFor(PATTERN_RFC1036); headers.put("If-Modified-Since", formatter.format(refTime)); } }
Example #2
Source File: HttpHeaderParserTest.java From product-emm with Apache License 2.0 | 6 votes |
@Test public void parseCacheHeaders_cacheControlMustRevalidateWithMaxAgeAndStale() { long now = System.currentTimeMillis(); headers.put("Date", rfc1123Date(now)); headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS)); // - max-age (entry.softTtl) indicates that the asset is fresh for 1 day // - stale-while-revalidate (entry.ttl) indicates that the asset may // continue to be served stale for up to additional 7 days, but this is // ignored in this case because of the must-revalidate header. headers.put("Cache-Control", "must-revalidate, max-age=86400, stale-while-revalidate=604800"); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNull(entry.etag); assertEqualsWithin(now + ONE_DAY_MILLIS, entry.softTtl, ONE_MINUTE_MILLIS); assertEquals(entry.softTtl, entry.ttl); }
Example #3
Source File: DiskBasedCacheTest.java From android-project-wo2b with Apache License 2.0 | 6 votes |
public void testCacheHeaderSerialization() throws Exception { Cache.Entry e = new Cache.Entry(); e.data = new byte[8]; e.serverDate = 1234567L; e.ttl = 9876543L; e.softTtl = 8765432L; e.etag = "etag"; e.responseHeaders = new HashMap<String, String>(); e.responseHeaders.put("fruit", "banana"); CacheHeader first = new CacheHeader("my-magical-key", e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); first.writeHeader(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); CacheHeader second = CacheHeader.readHeader(bais); assertEquals(first.key, second.key); assertEquals(first.serverDate, second.serverDate); assertEquals(first.ttl, second.ttl); assertEquals(first.softTtl, second.softTtl); assertEquals(first.etag, second.etag); assertEquals(first.responseHeaders, second.responseHeaders); }
Example #4
Source File: DiskBasedCacheTest.java From CrossBow with Apache License 2.0 | 6 votes |
@Test public void cacheHeaderSerialization() throws Exception { Cache.Entry e = new Cache.Entry(); e.data = new byte[8]; e.serverDate = 1234567L; e.lastModified = 13572468L; e.ttl = 9876543L; e.softTtl = 8765432L; e.etag = "etag"; e.responseHeaders = new HashMap<String, String>(); e.responseHeaders.put("fruit", "banana"); CacheHeader first = new CacheHeader("my-magical-key", e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); first.writeHeader(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); CacheHeader second = CacheHeader.readHeader(bais); assertEquals(first.key, second.key); assertEquals(first.serverDate, second.serverDate); assertEquals(first.lastModified, second.lastModified); assertEquals(first.ttl, second.ttl); assertEquals(first.softTtl, second.softTtl); assertEquals(first.etag, second.etag); assertEquals(first.responseHeaders, second.responseHeaders); }
Example #5
Source File: DiskBasedCacheTest.java From volley with Apache License 2.0 | 6 votes |
@Test public void testTrimWithPartialEvictions() { Cache.Entry entry1 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry1") - 1); cache.put("entry1", entry1); Cache.Entry entry2 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry2") - 1); cache.put("entry2", entry2); Cache.Entry entry3 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry3") - 1); cache.put("entry3", entry3); assertThatEntriesAreEqual(cache.get("entry1"), entry1); assertThatEntriesAreEqual(cache.get("entry2"), entry2); assertThatEntriesAreEqual(cache.get("entry3"), entry3); Cache.Entry entry4 = randomData((MAX_SIZE - getEntrySizeOnDisk("entry4") - 1) / 2); cache.put("entry4", entry4); assertThat(cache.get("entry1"), is(nullValue())); assertThat(cache.get("entry2"), is(nullValue())); assertThatEntriesAreEqual(cache.get("entry3"), entry3); assertThatEntriesAreEqual(cache.get("entry4"), entry4); }
Example #6
Source File: ResponseTest.java From product-emm with Apache License 2.0 | 6 votes |
@Test public void publicMethods() throws Exception { // Catch-all test to find API-breaking changes. assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class)); assertNotNull(Response.class.getMethod("error", VolleyError.class)); assertNotNull(Response.class.getMethod("isSuccess")); assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class)); assertNotNull(Response.ErrorListener.class.getDeclaredMethod("onErrorResponse", VolleyError.class)); assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class, boolean.class, long.class)); assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class, boolean.class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class)); }
Example #7
Source File: ResponseTest.java From volley with Apache License 2.0 | 6 votes |
@Test public void publicMethods() throws Exception { // Catch-all test to find API-breaking changes. assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class)); assertNotNull(Response.class.getMethod("error", VolleyError.class)); assertNotNull(Response.class.getMethod("isSuccess")); assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class)); assertNotNull( Response.ErrorListener.class.getDeclaredMethod( "onErrorResponse", VolleyError.class)); assertNotNull( NetworkResponse.class.getConstructor( int.class, byte[].class, Map.class, boolean.class, long.class)); assertNotNull( NetworkResponse.class.getConstructor( int.class, byte[].class, Map.class, boolean.class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class)); }
Example #8
Source File: CacheTestUtils.java From volley with Apache License 2.0 | 6 votes |
/** * Makes a random cache entry. * * @param data Data to use, or null to use random data * @param isExpired Whether the TTLs should be set such that this entry is expired * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh */ public static Cache.Entry makeRandomCacheEntry( byte[] data, boolean isExpired, boolean needsRefresh) { Random random = new Random(); Cache.Entry entry = new Cache.Entry(); if (data != null) { entry.data = data; } else { entry.data = new byte[random.nextInt(1024)]; } entry.etag = String.valueOf(random.nextLong()); entry.lastModified = random.nextLong(); entry.ttl = isExpired ? 0 : Long.MAX_VALUE; entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE; return entry; }
Example #9
Source File: ResponseTest.java From SaveVolley with Apache License 2.0 | 6 votes |
@Test public void publicMethods() throws Exception { // Catch-all test to find API-breaking changes. assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class)); assertNotNull(Response.class.getMethod("error", VolleyError.class)); assertNotNull(Response.class.getMethod("isSuccess")); assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class)); assertNotNull(Response.ErrorListener.class.getDeclaredMethod("onErrorResponse", VolleyError.class)); assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class, boolean.class, long.class)); assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class, boolean.class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class)); }
Example #10
Source File: ResponseTest.java From CrossBow with Apache License 2.0 | 6 votes |
@Test public void publicMethods() throws Exception { // Catch-all test to find API-breaking changes. assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class)); assertNotNull(Response.class.getMethod("error", VolleyError.class)); assertNotNull(Response.class.getMethod("isSuccess")); assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class)); assertNotNull(Response.ErrorListener.class.getDeclaredMethod("onErrorResponse", VolleyError.class)); assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class, boolean.class, long.class)); assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class, boolean.class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class)); }
Example #11
Source File: DiskBasedCacheTest.java From android-discourse with Apache License 2.0 | 6 votes |
public void testCacheHeaderSerialization() throws Exception { Cache.Entry e = new Cache.Entry(); e.data = new byte[8]; e.serverDate = 1234567L; e.ttl = 9876543L; e.softTtl = 8765432L; e.etag = "etag"; e.responseHeaders = new HashMap<String, String>(); e.responseHeaders.put("fruit", "banana"); CacheHeader first = new CacheHeader("my-magical-key", e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); first.writeHeader(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); CacheHeader second = CacheHeader.readHeader(bais); assertEquals(first.key, second.key); assertEquals(first.serverDate, second.serverDate); assertEquals(first.ttl, second.ttl); assertEquals(first.softTtl, second.softTtl); assertEquals(first.etag, second.etag); assertEquals(first.responseHeaders, second.responseHeaders); }
Example #12
Source File: HttpHeaderParserTest.java From device-database with Apache License 2.0 | 6 votes |
@Test public void parseCacheHeaders_cacheControlMustRevalidateWithMaxAgeAndStale() { long now = System.currentTimeMillis(); headers.put("Date", rfc1123Date(now)); headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS)); // - max-age (entry.softTtl) indicates that the asset is fresh for 1 day // - stale-while-revalidate (entry.ttl) indicates that the asset may // continue to be served stale for up to additional 7 days, but this is // ignored in this case because of the must-revalidate header. headers.put("Cache-Control", "must-revalidate, max-age=86400, stale-while-revalidate=604800"); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNull(entry.etag); assertEqualsWithin(now + ONE_DAY_MILLIS, entry.softTtl, ONE_MINUTE_MILLIS); assertEquals(entry.softTtl, entry.ttl); }
Example #13
Source File: HttpHeaderParserTest.java From device-database with Apache License 2.0 | 6 votes |
@Test public void parseCaseInsensitive() { long now = System.currentTimeMillis(); Header[] headersArray = new Header[5]; headersArray[0] = new BasicHeader("eTAG", "Yow!"); headersArray[1] = new BasicHeader("DATE", rfc1123Date(now)); headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS)); headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400"); headersArray[4] = new BasicHeader("content-type", "text/plain"); Map<String, String> headers = BasicNetwork.convertHeaders(headersArray); NetworkResponse response = new NetworkResponse(0, null, headers, false); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertEquals("Yow!", entry.etag); assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS); assertEquals(entry.softTtl, entry.ttl); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); }
Example #14
Source File: DiskBasedCacheTest.java From device-database with Apache License 2.0 | 6 votes |
@Test public void cacheHeaderSerialization() throws Exception { Cache.Entry e = new Cache.Entry(); e.data = new byte[8]; e.serverDate = 1234567L; e.lastModified = 13572468L; e.ttl = 9876543L; e.softTtl = 8765432L; e.etag = "etag"; e.responseHeaders = new HashMap<String, String>(); e.responseHeaders.put("fruit", "banana"); CacheHeader first = new CacheHeader("my-magical-key", e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); first.writeHeader(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); CacheHeader second = CacheHeader.readHeader(bais); assertEquals(first.key, second.key); assertEquals(first.serverDate, second.serverDate); assertEquals(first.lastModified, second.lastModified); assertEquals(first.ttl, second.ttl); assertEquals(first.softTtl, second.softTtl); assertEquals(first.etag, second.etag); assertEquals(first.responseHeaders, second.responseHeaders); }
Example #15
Source File: ResponseTest.java From device-database with Apache License 2.0 | 6 votes |
@Test public void publicMethods() throws Exception { // Catch-all test to find API-breaking changes. assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class)); assertNotNull(Response.class.getMethod("error", VolleyError.class)); assertNotNull(Response.class.getMethod("isSuccess")); assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class)); assertNotNull(Response.ErrorListener.class.getDeclaredMethod("onErrorResponse", VolleyError.class)); assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class, boolean.class, long.class)); assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class, boolean.class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class)); assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class)); }
Example #16
Source File: HttpHeaderParserTest.java From CrossBow with Apache License 2.0 | 5 votes |
@Test public void parseCacheHeaders_cacheControlMustRevalidateWithMaxAge() { long now = System.currentTimeMillis(); headers.put("Date", rfc1123Date(now)); headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS)); headers.put("Cache-Control", "must-revalidate, max-age=3600"); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNull(entry.etag); assertEqualsWithin(now + ONE_HOUR_MILLIS, entry.ttl, ONE_MINUTE_MILLIS); assertEquals(entry.softTtl, entry.ttl); }
Example #17
Source File: BasicNetwork.java From device-database with Apache License 2.0 | 5 votes |
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) { // If there's no cache entry, we're done. if (entry == null) { return; } if (entry.etag != null) { headers.put("If-None-Match", entry.etag); } if (entry.lastModified > 0) { Date refTime = new Date(entry.lastModified); headers.put("If-Modified-Since", DateUtils.formatDate(refTime)); } }
Example #18
Source File: HttpHeaderParserTest.java From product-emm with Apache License 2.0 | 5 votes |
@Test public void parseCacheHeaders_cacheControlNoCache() { long now = System.currentTimeMillis(); headers.put("Date", rfc1123Date(now)); headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS)); headers.put("Cache-Control", "no-cache"); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNull(entry); }
Example #19
Source File: HttpHeaderParserTest.java From android-project-wo2b with Apache License 2.0 | 5 votes |
public void testParseCacheHeaders_headersSet() { headers.put("MyCustomHeader", "42"); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNotNull(entry.responseHeaders); assertEquals(1, entry.responseHeaders.size()); assertEquals("42", entry.responseHeaders.get("MyCustomHeader")); }
Example #20
Source File: BasicNetwork.java From android_tv_metro with Apache License 2.0 | 5 votes |
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) { // If there's no cache entry, we're done. if (entry == null) { return; } if (entry.etag != null) { headers.put("If-None-Match", entry.etag); } if (entry.serverDate > 0) { Date refTime = new Date(entry.serverDate); headers.put("If-Modified-Since", DateUtils.formatDate(refTime)); } }
Example #21
Source File: HttpHeaderParserTest.java From android-discourse with Apache License 2.0 | 5 votes |
public void testParseCacheHeaders_headersSet() { headers.put("MyCustomHeader", "42"); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNotNull(entry.responseHeaders); assertEquals(1, entry.responseHeaders.size()); assertEquals("42", entry.responseHeaders.get("MyCustomHeader")); }
Example #22
Source File: HttpHeaderParserTest.java From SaveVolley with Apache License 2.0 | 5 votes |
@Test public void parseCacheHeaders_noHeaders() { Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNull(entry.etag); assertEquals(0, entry.serverDate); assertEquals(0, entry.lastModified); assertEquals(0, entry.ttl); assertEquals(0, entry.softTtl); }
Example #23
Source File: CacheHeader.java From volley with Apache License 2.0 | 5 votes |
private static List<Header> getAllResponseHeaders(Cache.Entry entry) { // If the entry contains all the response headers, use that field directly. if (entry.allResponseHeaders != null) { return entry.allResponseHeaders; } // Legacy fallback - copy headers from the map. return HttpHeaderParser.toAllHeaderList(entry.responseHeaders); }
Example #24
Source File: HttpHeaderParserTest.java From volley with Apache License 2.0 | 5 votes |
@Test public void parseCacheHeaders_noHeaders() { Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNull(entry.etag); assertEquals(0, entry.serverDate); assertEquals(0, entry.lastModified); assertEquals(0, entry.ttl); assertEquals(0, entry.softTtl); }
Example #25
Source File: HttpHeaderParserTest.java From android-project-wo2b with Apache License 2.0 | 5 votes |
public void testParseCacheHeaders_expiresInPast() { long now = System.currentTimeMillis(); headers.put("Date", rfc1123Date(now)); headers.put("Expires", rfc1123Date(now - ONE_HOUR_MILLIS)); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNull(entry.etag); assertEqualsWithin(entry.serverDate, now, ONE_MINUTE_MILLIS); assertEquals(0, entry.ttl); assertEquals(0, entry.softTtl); }
Example #26
Source File: HttpHeaderParserTest.java From product-emm with Apache License 2.0 | 5 votes |
@Test public void parseCacheHeaders_cacheControlMustRevalidateNoMaxAge() { long now = System.currentTimeMillis(); headers.put("Date", rfc1123Date(now)); headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS)); headers.put("Cache-Control", "must-revalidate"); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNull(entry.etag); assertEqualsWithin(now, entry.ttl, ONE_MINUTE_MILLIS); assertEquals(entry.softTtl, entry.ttl); }
Example #27
Source File: HttpHeaderParserTest.java From CrossBow with Apache License 2.0 | 5 votes |
@Test public void parseCacheHeaders_headersSet() { headers.put("MyCustomHeader", "42"); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertNotNull(entry.responseHeaders); assertEquals(1, entry.responseHeaders.size()); assertEquals("42", entry.responseHeaders.get("MyCustomHeader")); }
Example #28
Source File: DiskBasedCacheTest.java From volley with Apache License 2.0 | 5 votes |
@Test public void testManyResponseHeaders() { Cache.Entry entry = new Cache.Entry(); entry.data = new byte[0]; entry.responseHeaders = new HashMap<>(); for (int i = 0; i < 0xFFFF; i++) { entry.responseHeaders.put(Integer.toString(i), ""); } cache.put("key", entry); }
Example #29
Source File: DiskBasedCacheTest.java From volley with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("TryFinallyCanBeTryWithResources") public void testGetWrongKey() throws IOException { // Cache something Cache.Entry entry = randomData(1023); cache.put("key", entry); assertThatEntriesAreEqual(cache.get("key"), entry); // Access the cached file File cacheFolder = temporaryFolder.getRoot(); File file = cacheFolder.listFiles()[0]; FileOutputStream fos = new FileOutputStream(file); try { // Overwrite with a different key CacheHeader wrongHeader = new CacheHeader("bad", entry); wrongHeader.writeHeader(fos); } finally { //noinspection ThrowFromFinallyBlock fos.close(); } // key is gone, but file is still there assertThat(cache.get("key"), is(nullValue())); assertThat(listCachedFiles(), is(arrayWithSize(1))); // Note: file is now a zombie because its key does not map to its name }
Example #30
Source File: ApiInvoker.java From swaggy-jenkins with MIT License | 5 votes |
public static void initializeInstance(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery, int connectionTimeout) { INSTANCE = new ApiInvoker(cache, network, threadPoolSize, delivery, connectionTimeout); setUserAgent("OpenAPI-Generator/1.0.0/android"); // Setup authentications (key: authentication name, value: authentication). INSTANCE.authentications = new HashMap<String, Authentication>(); INSTANCE.authentications.put("jenkins_auth", new HttpBasicAuth()); INSTANCE.authentications.put("jwt_auth", new ApiKeyAuth("header", "Authorization")); // Prevent the authentications from being modified. INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications); }