Skip to content

Commit

Permalink
use LRU cache in DID Resolve Registry
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Nov 23, 2023
1 parent bac5c8a commit e7440c1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@

import static org.assertj.core.api.Assertions.assertThat;

class LruCacheTest {
class ConcurrentLruCacheTest {
private ConcurrentLruCache<String, String> cache;

@Test
void verifyEviction() {
cache.put("foo", "foo");
cache.put("bar", "bar");
assertThat(cache.containsKey("foo")).isTrue();
assertThat(cache.containsKey("bar")).isTrue();
assertThat(cache)
.containsKey("foo")
.containsKey("bar");

cache.put("baz", "baz");
assertThat(cache.containsKey("baz")).isTrue();
assertThat(cache.containsKey("bar")).isTrue();
assertThat(cache.containsKey("foo")).isFalse();
assertThat(cache)
.containsKey("baz")
.containsKey("bar")
.doesNotContainKey("foo");
}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {

dependencies {
api(project(":spi:common:identity-did-spi"))
implementation(project(":core:common:util"))
implementation(project(":extensions:common:iam:decentralized-identity:identity-did-crypto"))

implementation(libs.jakarta.rsApi)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,36 @@
import org.eclipse.edc.iam.did.spi.resolution.DidResolver;
import org.eclipse.edc.iam.did.spi.resolution.DidResolverRegistry;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.util.collection.ConcurrentLruCache;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Default implementation.
* Default implementation, that delegates to several {@link DidResolver} objects, caching the results in a {@link ConcurrentLruCache}
*/
public class DidResolverRegistryImpl implements DidResolverRegistry {
private static final String DID = "did";
private static final int DID_PREFIX = 0;
private static final int DID_METHOD_NAME = 1;

private final ConcurrentLruCache<String, DidDocument> didCache;
private final Map<String, DidResolver> resolvers = new HashMap<>();

public DidResolverRegistryImpl() {
didCache = new ConcurrentLruCache<>(50);
}

/**
* Constructs a DidResolverRegistryImpl object with the specified cache size.
*
* @param cacheSize the maximum number of entries that the cache can hold. Pass 0 to effectively deactivate the cache.
*/
public DidResolverRegistryImpl(int cacheSize) {
didCache = new ConcurrentLruCache<>(cacheSize);
}

@Override
public void register(DidResolver resolver) {
resolvers.put(resolver.getMethod(), resolver);
Expand All @@ -41,6 +56,7 @@ public void register(DidResolver resolver) {
@Override
public Result<DidDocument> resolve(String didKey) {
Objects.requireNonNull(didKey);

// for the definition of DID syntax, .cf https://www.w3.org/TR/did-core/#did-syntax
var tokens = didKey.split(":");
if (tokens.length < 3) {
Expand All @@ -50,10 +66,26 @@ public Result<DidDocument> resolve(String didKey) {
return Result.failure("Invalid DID prefix");
}
var methodName = tokens[DID_METHOD_NAME];
var resolver = resolvers.get(methodName);
if (resolver == null) {
return Result.failure("No resolver registered for DID Method: " + methodName);

return resolveCachedDocument(didKey, methodName);
}

@NotNull
private Result<DidDocument> resolveCachedDocument(String didKey, String methodName) {
var didDocument = didCache.get(didKey);
if (didDocument == null) {
var resolver = resolvers.get(methodName);
if (resolver == null) {
return Result.failure("No resolver registered for DID Method: " + methodName);
}
var resolveResult = resolver.resolve(didKey);
if (resolveResult.failed()) {
return resolveResult;
}
didDocument = resolveResult.getContent();
didCache.put(didKey, didDocument);
}
return resolver.resolve(didKey);

return Result.success(didDocument);
}
}

0 comments on commit e7440c1

Please sign in to comment.