Skip to content

Commit

Permalink
feat: add LRU cache for DID Documents (#3651)
Browse files Browse the repository at this point in the history
* feat: add concurrent LRU cache for DID documents

* use LRU cache in DID Resolve Registry
  • Loading branch information
paullatzelsperger authored Nov 23, 2023
1 parent 2b5f079 commit 906bc17
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.util.collection;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* A thread-safe LRU cache with a specified capacity.
* <p>
* This class extends {@link LinkedHashMap} and adds concurrency using a ReentrantReadWriteLock.
* The cache uses a LinkedHashMap to store the entries and automatically evicts the least recently used entry
* when the capacity is reached.
*
* @param <K> the type of keys maintained by this cache
* @param <V> the type of mapped values
*/
public class ConcurrentLruCache<K, V> extends LinkedHashMap<K, V> {
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final int capacity;

public ConcurrentLruCache(int capacity) {
super(capacity + 1, 1, true);
this.capacity = capacity;
}

@Override
public V put(K key, V value) {
lock.writeLock().lock();
try {
return super.put(key, value);
} finally {
lock.writeLock().unlock();
}
}

@Override
public V remove(Object key) {
lock.writeLock().lock();
try {
return super.remove(key);
} finally {
lock.writeLock().unlock();
}
}

@Override
public boolean remove(Object key, Object value) {
lock.writeLock().lock();
try {
return super.remove(key, value);
} finally {
lock.writeLock().unlock();
}
}

@Override
public Object clone() {
throw new UnsupportedOperationException();
}

@Override
public V get(Object key) {
lock.readLock().lock();
try {
return super.get(key);
} finally {
lock.readLock().unlock();
}
}

@Override
public void clear() {
lock.writeLock().lock();
try {
super.clear();
} finally {
lock.writeLock().unlock();
}
}

@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > capacity;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,25 @@

package org.eclipse.edc.util.collection;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

class LruCacheTest {
private LruCache<String, String> cache;
class ConcurrentLruCacheTest {
private final ConcurrentLruCache<String, String> cache = new ConcurrentLruCache<>(2);

@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();
}

@BeforeEach
void setUp() {
cache = new LruCache<>(2);
assertThat(cache)
.containsKey("baz")
.containsKey("bar")
.doesNotContainKey("foo");
}
}
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 906bc17

Please sign in to comment.