@Deprecated public DefaultStorageBroker(BlobStore blobStore) { this(blobStore, new TransientCache(100, 1024)); }
public boolean getTransient(TileObject tile) { String key = TransientCache.computeTransientKey(tile); Resource resource; synchronized (transientCache) { resource = transientCache.get(key); } tile.setBlob(resource); return resource != null; }
public void putTransient(TileObject tile) { String key = TransientCache.computeTransientKey(tile); synchronized (transientCache) { transientCache.put(key, tile.getBlob()); } }
@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 }
@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 }
@Before public void setUp() throws Exception { transCache = new TransientCache(MAX_TILES, MAX_SPACE_KiB, EXPIRE_TIME); ticker = new TestTicker(System.nanoTime()); transCache.setTicker(ticker); }
@Override public void setValue(TileObject value) { puts.add( TransientCache.computeTransientKey( value)); storagePutCounter.incrementAndGet(); } })))
System.out.println("transientCacheSize " + mock.transientCache.size()); System.out.println("transientCacheStorage " + mock.transientCache.storageSize());
public CachedResource(Resource content) { this(content, currentTime()); } }
? 1023 : 1024]); // make the first one 1 byte less than a KiB transCache.put("foo" + i, r); assertThat( transCache.storageSize(), is((i + 1) * 1024 - 1)); // 1 KiB per resource, less a byte for the first ticker.advanceMilli(1); transCache.storageSize(), is( (long) MAX_SPACE_KiB * 1024 - 1)); // 1 KiB per resource, less a byte for the first assertThat(transCache.size(), is(MAX_SPACE_KiB)); Resource r = new ByteArrayResource(new byte[2]); // 2 bytes will go over the maximum transCache.put("foo" + MAX_SPACE_KiB, r); assertThat( transCache.storageSize(), is( (long) (MAX_SPACE_KiB - 1) * 1024 + 2)); // 1 KiB for each of the resources except the first should be assertThat(transCache.size(), is(MAX_SPACE_KiB)); Resource result1 = transCache.get("foo0"); Resource result2 = transCache.get("foo1");
public Boolean answer() throws Throwable { TileObject tile = (TileObject) EasyMock.getCurrentArguments()[0]; if (puts.contains(TransientCache.computeTransientKey(tile))) { tile.setBlob(new ByteArrayResource(fakeWMSResponse)); storageGetCounter.incrementAndGet(); return true; } else { return false; } } })
/** * Retrieve a resource * * @param key * @return The resource cached under the given key, or null if no resource is cached. */ public Resource get(String key) { CachedResource cached = cache.get(key); if (cached != null) { cache.remove(key); currentStorage -= cached.content.getSize(); if (cached.time + expireDelay < currentTime()) { return null; } else { return cached.content; } } return 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)); } }
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; } })
@Override public void setValue(TileObject tile) { String key = TransientCache.computeTransientKey(tile); synchronized (transientCache) { transientCache.put(key, tile.getBlob()); } } }));
private StorageBroker resetAndPrepStorageBroker() throws Exception { System.out.println("Deleting old test database."); String blobPath = findTempDir() + File.separator + TEST_BLOB_DIR_NAME; System.out.println("Creating new blobstore in " + blobPath); File blobDirs = new File(blobPath); if (!blobDirs.exists() && !blobDirs.mkdirs()) { throw new StorageException("Unable to create " + blobPath); } BlobStore blobStore = new FileBlobStore(blobPath); TransientCache transCache = new TransientCache(100, 1024, 2000); StorageBroker sb = new DefaultStorageBroker(blobStore); // long[] xyz = {1L,2L,3L}; Resource blob = new ByteArrayResource(new byte[20 * 1024]); System.out.println("Inserting into database, " + TILE_PUT_COUNT + " tiles"); long startInsert = System.currentTimeMillis(); for (int i = 1; i < TILE_PUT_COUNT; i++) { long tmp = (long) Math.log(i) + 1; long tmp2 = i % tmp; long[] xyz = {tmp2, tmp2, (long) Math.log10(i)}; TileObject completeObj = TileObject.createCompleteTileObject( "test", xyz, "hefty-gridSet:id1", "image/jpeg", null, blob); sb.put(completeObj); } long stopInsert = System.currentTimeMillis(); System.out.println( TILE_PUT_COUNT + " inserts took " + Long.toString(stopInsert - startInsert) + "ms"); return sb; }