Skip to content

Commit

Permalink
feat: cache registered LD documents in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Nov 16, 2023
1 parent dd99058 commit 3a77545
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import jakarta.json.JsonValue;
import org.eclipse.edc.jsonld.document.JarLoader;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.result.Result;

Expand Down Expand Up @@ -177,7 +178,8 @@ private Stream<String> contextsForScope(String scope) {

private static class CachedDocumentLoader implements DocumentLoader {

private final Map<String, URI> cache = new HashMap<>();
private final Map<String, URI> uriCache = new HashMap<>();
private final Map<URI, Document> documentCache = new HashMap<>();
private final DocumentLoader loader;

CachedDocumentLoader(JsonLdConfiguration configuration) {
Expand All @@ -191,14 +193,20 @@ private static class CachedDocumentLoader implements DocumentLoader {
@Override
public Document loadDocument(URI url, DocumentLoaderOptions options) throws JsonLdError {
var uri = Optional.of(url.toString())
.map(cache::get)
.map(uriCache::get)
.orElse(url);

return loader.loadDocument(uri, options);
return Optional.of(documentCache.get(uri))
.orElse(loader.loadDocument(uri, options));
}

public void register(String contextUrl, URI uri) {
cache.put(contextUrl, uri);
uriCache.put(contextUrl, uri);
try {
documentCache.put(uri, loader.loadDocument(uri, new DocumentLoaderOptions()));
} catch (JsonLdError e) {
throw new EdcException(e);
}
}

}
Expand Down

0 comments on commit 3a77545

Please sign in to comment.