-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
29 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 19 additions & 8 deletions
27
src/main/java/de/jangassen/lambda/loader/MethodInvocationContextCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,32 @@ | ||
package de.jangassen.lambda.loader; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import de.jangassen.lambda.api.ApiResource; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import javax.annotation.Nonnull; | ||
import java.time.Duration; | ||
|
||
public class MethodInvocationContextCache implements MethodInvocationContextProvider { | ||
private final ClassLoaderFactory classLoaderFactory; | ||
private final LoadingCache<ApiResource, MethodInvocationContext> cache; | ||
|
||
private final Map<ApiResource, MethodInvocationContext> cache = new ConcurrentHashMap<>(); | ||
|
||
public MethodInvocationContextCache(ClassLoaderFactory classLoaderFactory) { | ||
this.classLoaderFactory = classLoaderFactory; | ||
public MethodInvocationContextCache(ClassLoaderFactory classLoaderFactory, Duration timeout) { | ||
cache = CacheBuilder.newBuilder() | ||
.expireAfterAccess(timeout) | ||
.build(new CacheLoader<ApiResource, MethodInvocationContext>() { | ||
@Override | ||
public MethodInvocationContext load(@Nonnull ApiResource apiResource) { | ||
return MethodInvocationContextBuilder | ||
.start(classLoaderFactory) | ||
.withApiResource(apiResource) | ||
.build(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public MethodInvocationContext getMethodInvocationContext(ApiResource apiResource) { | ||
return cache.computeIfAbsent(apiResource, key -> MethodInvocationContextBuilder.start(classLoaderFactory).withApiResource(key).build()); | ||
return cache.getUnchecked(apiResource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters