23
23
24
24
import org .checkerframework .checker .nullness .qual .Nullable ;
25
25
26
+ import java .time .Duration ;
26
27
import java .util .Set ;
27
28
import java .util .concurrent .ExecutionException ;
28
29
import java .util .concurrent .TimeUnit ;
29
30
30
31
import static java .util .Objects .requireNonNull ;
31
32
32
33
/**
33
- * This class can be used to cache lookups.
34
+ * This class is using a {@code LoadingCache} to speed up lookups,
35
+ * delegated to another {@link Lookup} instance.
36
+ *
37
+ * <p>This class is thread safe. All entries are evicted after one minute.
38
+ * Negative matches are never cached. If a new entry
39
+ * becomes available in the associated {@code Lookup}, it's immediately
40
+ * visible outside. If an entry is deleted in the associated {@code Lookup},
41
+ * it takes one minute until it disappears if it was cached in the last minute.
42
+ * Otherwise, it disappears immediately.
34
43
*
35
44
* @param <T> Element Type
36
45
*/
@@ -40,16 +49,20 @@ public class LoadingCacheLookup<T> implements Lookup<T> {
40
49
private final LoadingCache <String , T > cache ;
41
50
private final LoadingCache <String , Named <T >> cacheIgnoreCase ;
42
51
43
- public LoadingCacheLookup (Lookup <T > delegate ) {
52
+ public LoadingCacheLookup (Lookup <T > delegate , Duration expiration ) {
44
53
this .delegate = delegate ;
45
54
this .cache = CacheBuilder .newBuilder ()
46
- .expireAfterWrite (1 , TimeUnit .MINUTES )
55
+ .expireAfterWrite (expiration . toMillis () , TimeUnit .MILLISECONDS )
47
56
.build (CacheLoader .from (name -> requireNonNull (delegate .get (name ))));
48
57
this .cacheIgnoreCase = CacheBuilder .newBuilder ()
49
- .expireAfterWrite (1 , TimeUnit .MINUTES )
58
+ .expireAfterWrite (expiration . toMillis () , TimeUnit .MILLISECONDS )
50
59
.build (CacheLoader .from (name -> requireNonNull (delegate .getIgnoreCase (name ))));
51
60
}
52
61
62
+ public LoadingCacheLookup (Lookup <T > delegate ) {
63
+ this (delegate , Duration .ofMinutes (1 ));
64
+ }
65
+
53
66
@ Override public @ Nullable T get (String name ) {
54
67
try {
55
68
return cache .get (name );
0 commit comments