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