Skip to content

Commit

Permalink
⚡ Use virtual threads executor in caffeine cache async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Aug 23, 2024
1 parent 03281d0 commit d5a8e8d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import java.util.AbstractMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.restheart.utils.ThreadsUtils;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
Expand All @@ -39,10 +41,11 @@
* @param <V> the class of the values (is Optional-ized).
*/
public class CaffeineCache<K, V> implements org.restheart.cache.Cache<K, V> {
private static final Executor virtualThreadsExecutor = ThreadsUtils.virtualThreadsExecutor();
private final Cache<K, Optional<V>> wrapped;

public CaffeineCache(long size, EXPIRE_POLICY expirePolicy, long ttl) {
var builder = Caffeine.newBuilder();
var builder = Caffeine.newBuilder().executor(virtualThreadsExecutor);

builder.maximumSize(size);

Expand All @@ -56,7 +59,7 @@ public CaffeineCache(long size, EXPIRE_POLICY expirePolicy, long ttl) {
}

public CaffeineCache(long size, EXPIRE_POLICY expirePolicy, long ttl, Consumer<Map.Entry<K, Optional<V>>> remover) {
var builder = Caffeine.newBuilder();
var builder = Caffeine.newBuilder().executor(virtualThreadsExecutor);

builder.maximumSize(size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @param <V> the class of the values (is Optional-ized).
*/
public class CaffeineLoadingCache<K, V> implements org.restheart.cache.LoadingCache<K, V> {
private static final Executor threadPerTaskExecutor = ThreadsUtils.virtualThreadsExecutor();
private static final Executor virtualThreadsExecutor = ThreadsUtils.virtualThreadsExecutor();
private final AsyncLoadingCache<K, Optional<V>> wrapped;

public CaffeineLoadingCache(long size, EXPIRE_POLICY expirePolicy, long ttl, Function<K, V> loader) {
Expand All @@ -57,11 +57,11 @@ public CaffeineLoadingCache(long size, EXPIRE_POLICY expirePolicy, long ttl, Fun
}

wrapped = builder
.executor(threadPerTaskExecutor)
.executor(virtualThreadsExecutor)
.buildAsync(new AsyncCacheLoader<K, Optional<V>>() {
@Override
public CompletableFuture<? extends Optional<V>> asyncLoad(K key, Executor executor) throws Exception {
return CompletableFuture.supplyAsync(() -> Optional.ofNullable(loader.apply(key)));
return CompletableFuture.supplyAsync(() -> Optional.ofNullable(loader.apply(key)), virtualThreadsExecutor);
}

@Override
Expand All @@ -71,7 +71,7 @@ public CompletableFuture<? extends Optional<V>> asyncLoad(K key, Executor execut
ret.put(key, Optional.ofNullable(loader.apply(key)));
});

return CompletableFuture.supplyAsync(() -> ret);
return CompletableFuture.supplyAsync(() -> ret, virtualThreadsExecutor);
}
});
}
Expand Down

0 comments on commit d5a8e8d

Please sign in to comment.