|
20 | 20 | import org.apache.flink.cdc.common.event.TableId;
|
21 | 21 | import org.apache.flink.cdc.common.utils.Predicates;
|
22 | 22 |
|
| 23 | +import org.apache.flink.shaded.guava31.com.google.common.cache.Cache; |
| 24 | +import org.apache.flink.shaded.guava31.com.google.common.cache.CacheBuilder; |
| 25 | + |
| 26 | +import java.time.Duration; |
23 | 27 | import java.util.ArrayList;
|
24 | 28 | import java.util.Iterator;
|
25 | 29 | import java.util.List;
|
|
29 | 33 | /** Selectors for filtering tables. */
|
30 | 34 | public class Selectors {
|
31 | 35 |
|
| 36 | + private static final Duration CACHE_EXPIRE_DURATION = Duration.ofHours(1); |
| 37 | + |
32 | 38 | private List<Selector> selectors;
|
33 | 39 |
|
| 40 | + private final Cache<TableId, Boolean> cache = |
| 41 | + CacheBuilder.newBuilder() |
| 42 | + .expireAfterAccess(CACHE_EXPIRE_DURATION) |
| 43 | + .maximumSize(1024) |
| 44 | + .build(); |
| 45 | + |
34 | 46 | private Selectors() {}
|
35 | 47 |
|
36 | 48 | /**
|
@@ -73,6 +85,18 @@ public boolean isMatch(TableId tableId) {
|
73 | 85 |
|
74 | 86 | /** Match the {@link TableId} against the {@link Selector}s. * */
|
75 | 87 | public boolean isMatch(TableId tableId) {
|
| 88 | + Boolean cachedResult = cache.getIfPresent(tableId); |
| 89 | + if (cachedResult != null) { |
| 90 | + return cachedResult; |
| 91 | + } |
| 92 | + |
| 93 | + boolean match = computeIsMatch(tableId); |
| 94 | + cache.put(tableId, match); |
| 95 | + return match; |
| 96 | + } |
| 97 | + |
| 98 | + /** Computes the match result if not present in the cache */ |
| 99 | + private boolean computeIsMatch(TableId tableId) { |
76 | 100 | for (Selector selector : selectors) {
|
77 | 101 | if (selector.isMatch(tableId)) {
|
78 | 102 | return true;
|
|
0 commit comments