public boolean getTransient(TileObject tile) { String key = TransientCache.computeTransientKey(tile); Resource resource; synchronized (transientCache) { resource = transientCache.get(key); } tile.setBlob(resource); return resource != null; }
@Test public void testRemoveOnHit() throws Exception { Resource r = new ByteArrayResource(new byte[] {1, 2, 3}); transCache.put("foo", r); ticker.advanceMilli(EXPIRE_TIME - 1); transCache.get("foo"); // Hit Resource result = transCache.get("foo"); assertThat(result, nullValue()); // Should have been cleared }
public Boolean answer() throws Throwable { TileObject tile = (TileObject) EasyMock.getCurrentArguments()[0]; String key = TransientCache.computeTransientKey(tile); Resource resource; synchronized (transientCache) { resource = transientCache.get(key); } if (resource != null) { cacheHits.incrementAndGet(); } else { cacheMisses.incrementAndGet(); } tile.setBlob(resource); return resource != null; } })
@Test public void testHit() throws Exception { Resource r = new ByteArrayResource(new byte[] {1, 2, 3}); transCache.put("foo", r); ticker.advanceMilli(EXPIRE_TIME - 1); Resource result = transCache.get("foo"); assertThat(result, notNullValue()); assertThat(r.getLastModified(), equalTo(r.getLastModified())); try (InputStream is = result.getInputStream(); ) { assertThat(is.read(), equalTo(1)); assertThat(is.read(), equalTo(2)); assertThat(is.read(), equalTo(3)); assertThat(is.read(), equalTo(-1)); } }
@Test public void testRemoveOnExpire() throws Exception { Resource r = new ByteArrayResource(new byte[] {1, 2, 3}); transCache.put("foo", r); ticker.advanceMilli(EXPIRE_TIME + 1); Resource result = transCache.get("foo"); assertThat(result, nullValue()); // Should have expired }
@Test public void testRemoveWhenMaxTiles() throws Exception { for (byte i = 0; i < MAX_TILES; i++) { Resource r = new ByteArrayResource( new byte[] {(byte) (i + 1), (byte) (i + 2), (byte) (i + 3)}); transCache.put("foo" + i, r); assertThat(transCache.size(), is(i + 1)); } assertThat(transCache.storageSize(), is((long) MAX_TILES * 3)); Resource r = new ByteArrayResource(new byte[] {(byte) (MAX_TILES + 1), (byte) (MAX_TILES + 2)}); transCache.put("foo" + MAX_TILES, r); assertThat(transCache.size(), is(MAX_TILES)); assertThat( transCache.storageSize(), is((long) MAX_TILES * 3 - 1)); // remove a 3 byte and add a 2 byte ticker.advanceMilli(1); Resource result1 = transCache.get("foo0"); assertThat(result1, nullValue()); // Should have expired Resource result2 = transCache.get("foo1"); assertThat(result2, notNullValue()); // Should still be cached }