OkHttpEngine: improve cache API, add example #138
This commit is contained in:
parent
903dbad9c6
commit
3f959a3b46
@ -55,11 +55,11 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OkHttp cache implemented through {@link OkHttpClient.Builder#cache(Cache)}.
|
* Sets the response cache to be used to read and write cached responses.
|
||||||
*/
|
*/
|
||||||
public OkHttpFactory(Cache cache) {
|
public OkHttpFactory cache(Cache cache) {
|
||||||
mClientBuilder = new OkHttpClient.Builder()
|
mClientBuilder.cache(cache);
|
||||||
.cache(cache);
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,13 +23,29 @@ import org.oscim.tiling.source.OkHttpEngine;
|
|||||||
import org.oscim.tiling.source.UrlTileSource;
|
import org.oscim.tiling.source.UrlTileSource;
|
||||||
import org.oscim.tiling.source.mvt.MapboxTileSource;
|
import org.oscim.tiling.source.mvt.MapboxTileSource;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import okhttp3.Cache;
|
||||||
|
|
||||||
public class MapboxTest extends GdxMapApp {
|
public class MapboxTest extends GdxMapApp {
|
||||||
|
|
||||||
|
private static final boolean USE_CACHE = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createLayers() {
|
public void createLayers() {
|
||||||
|
// Cache the tiles into file system
|
||||||
|
File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
|
||||||
|
int cacheSize = 10 * 1024 * 1024; // 10 MB
|
||||||
|
Cache cache = new Cache(cacheDirectory, cacheSize);
|
||||||
|
|
||||||
|
OkHttpEngine.OkHttpFactory factory = new OkHttpEngine.OkHttpFactory();
|
||||||
|
if (USE_CACHE)
|
||||||
|
factory.cache(cache);
|
||||||
|
|
||||||
UrlTileSource tileSource = MapboxTileSource.builder()
|
UrlTileSource tileSource = MapboxTileSource.builder()
|
||||||
.apiKey("mapzen-xxxxxxx") // Put a proper API key
|
.apiKey("mapzen-xxxxxxx") // Put a proper API key
|
||||||
.httpFactory(new OkHttpEngine.OkHttpFactory())
|
.httpFactory(factory)
|
||||||
//.locale("en")
|
//.locale("en")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ public class OkHttpEngineTest {
|
|||||||
Cache cache = new Cache(new File("tmp"), 1024);
|
Cache cache = new Cache(new File("tmp"), 1024);
|
||||||
OSciMap4TileSource tileSource =
|
OSciMap4TileSource tileSource =
|
||||||
new OSciMap4TileSource(server.url("/tiles/vtm").toString());
|
new OSciMap4TileSource(server.url("/tiles/vtm").toString());
|
||||||
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(cache).create(tileSource);
|
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory().cache(cache).create(tileSource);
|
||||||
engine.sendRequest(new Tile(1, 2, (byte) 3));
|
engine.sendRequest(new Tile(1, 2, (byte) 3));
|
||||||
engine.requestCompleted(true);
|
engine.requestCompleted(true);
|
||||||
assertThat(cache.requestCount()).isEqualTo(1);
|
assertThat(cache.requestCount()).isEqualTo(1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user