Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.prismic</groupId>
<artifactId>java-kit</artifactId>
<packaging>jar</packaging>
<version>2.0.0</version>
<version>2.1.0-SNAPSHOT</version>
<name>java-kit</name>
<description>The developer kit to access Prismic.io repositories using the Java language.</description>
<url>http://maven.apache.org</url>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/prismic/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.Proxy;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;

/**
* Embodies an API endpoint, from which it is possible to make queries.
Expand Down Expand Up @@ -92,7 +93,7 @@ public static Api get(String endpoint, String accessToken, String defaultReferen
JsonNode json = cache.getOrSet(
url,
5000L,
() -> HttpClient.fetch(url, logger, null, proxy)
CompletableFuture.supplyAsync(() -> HttpClient.fetch(url, logger, null, proxy))
);

ApiData apiData = ApiData.parse(json);
Expand Down
58 changes: 30 additions & 28 deletions src/main/java/io/prismic/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,34 @@
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.collections4.map.LRUMap;

import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static java.util.Collections.synchronizedMap;

public interface Cache {

void set(String key, Long ttl, JsonNode response);

JsonNode get(String key);
JsonNode getOrSet(String key, Long ttl, Callback f);

default JsonNode getOrSet(String key, Long ttl, Future<JsonNode> callback) {
JsonNode found = this.get(key);
if (found != null) {
return found;
}
try {
JsonNode json = callback.get();
this.set(key, ttl, json);
return json;
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}

// --

class NoCache implements Cache {

@Override
Expand All @@ -21,47 +42,40 @@ public JsonNode get(String key) {
return null;
}

@Override
public JsonNode getOrSet(String key, Long ttl, Callback f) {
return f.execute();
}

}

// --

class DefaultCache {

private static final Cache defaultCache = new BuiltInCache(999);
private static final Cache DEFAULT_CACHE = new BuiltInCache(999);

private DefaultCache() {}
private DefaultCache() {
}

public static Cache getInstance() {
return defaultCache;
return DEFAULT_CACHE;
}
}

// --

interface Callback {
JsonNode execute();
}

class BuiltInCache implements Cache {

private final java.util.Map<String, Entry> cache;
private final Map<String, Entry> cache;

static class Entry {
public final Long expiration;
public final JsonNode value;

public Entry(Long expiration, JsonNode value) {
this.expiration = expiration;
this.value = value;
}
}

public BuiltInCache(int maxDocuments) {
this.cache = java.util.Collections.synchronizedMap(new LRUMap<String, Entry>(maxDocuments));
this.cache = synchronizedMap(new LRUMap<String, Entry>(maxDocuments));
}

@Override
Expand All @@ -80,21 +94,9 @@ public void set(String key, Long ttl, JsonNode response) {
this.cache.put(key, new Entry(expiration, response));
}

@Override
public JsonNode getOrSet(String key, Long ttl, Callback f) {
JsonNode found = this.get(key);
if(found == null) {
JsonNode json = f.execute();
this.set(key, ttl, json);
return json;
} else {
return found;
}
}

private Boolean isExpired(String key) {
Entry entry = this.cache.get(key);
return entry != null && entry.expiration !=0 && entry.expiration < System.currentTimeMillis();
return entry != null && entry.expiration != 0 && entry.expiration < System.currentTimeMillis();
}

}
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/prismic/Callback.java

This file was deleted.

13 changes: 11 additions & 2 deletions src/test/java/io/prismic/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -51,9 +52,17 @@ public void apiIsInitialized() {
/**
* Make sure a call to a private repository without a token returns the expected error
*/
@Test(expected = Api.Error.class)
@Test
public void invalidToken() {
Api api = Api.get("https://private-test.prismic.io/api");
Throwable thrown = null;
try {
Api.get("https://private-test.prismic.io/api");
} catch (Throwable t) {
thrown = t;
}
Throwable rootCause = ExceptionUtils.getRootCause(thrown);
Assert.assertEquals(Api.Error.class, rootCause.getClass());
Assert.assertEquals("Invalid access token", rootCause.getMessage());
}

/**
Expand Down