18
18
*/
19
19
package org .apache .iceberg .flink .sink .dynamic ;
20
20
21
- import com .github .benmanes .caffeine .cache .Cache ;
22
- import com .github .benmanes .caffeine .cache .Caffeine ;
23
21
import java .util .Map ;
24
22
import java .util .Set ;
25
23
import org .apache .flink .annotation .Internal ;
@@ -53,18 +51,18 @@ class TableMetadataCache {
53
51
private final Catalog catalog ;
54
52
private final long refreshMs ;
55
53
private final int inputSchemasPerTableCacheMaximumSize ;
56
- private final Cache <TableIdentifier , CacheItem > cache ;
54
+ private final LRUCache <TableIdentifier , CacheItem > tableCache ;
57
55
58
56
TableMetadataCache (
59
57
Catalog catalog , int maximumSize , long refreshMs , int inputSchemasPerTableCacheMaximumSize ) {
60
58
this .catalog = catalog ;
61
59
this .refreshMs = refreshMs ;
62
60
this .inputSchemasPerTableCacheMaximumSize = inputSchemasPerTableCacheMaximumSize ;
63
- this .cache = Caffeine . newBuilder (). maximumSize ( maximumSize ). build ( );
61
+ this .tableCache = new LRUCache <>( maximumSize , ignored -> {} );
64
62
}
65
63
66
64
Tuple2 <Boolean , Exception > exists (TableIdentifier identifier ) {
67
- CacheItem cached = cache . getIfPresent (identifier );
65
+ CacheItem cached = tableCache . get (identifier );
68
66
if (cached != null && Boolean .TRUE .equals (cached .tableExists )) {
69
67
return EXISTS ;
70
68
} else if (needsRefresh (cached , true )) {
@@ -87,7 +85,7 @@ PartitionSpec spec(TableIdentifier identifier, PartitionSpec spec) {
87
85
}
88
86
89
87
void update (TableIdentifier identifier , Table table ) {
90
- cache .put (
88
+ tableCache .put (
91
89
identifier ,
92
90
new CacheItem (
93
91
true ,
@@ -98,7 +96,7 @@ void update(TableIdentifier identifier, Table table) {
98
96
}
99
97
100
98
private String branch (TableIdentifier identifier , String branch , boolean allowRefresh ) {
101
- CacheItem cached = cache . getIfPresent (identifier );
99
+ CacheItem cached = tableCache . get (identifier );
102
100
if (cached != null && cached .tableExists && cached .branches .contains (branch )) {
103
101
return branch ;
104
102
}
@@ -113,7 +111,7 @@ private String branch(TableIdentifier identifier, String branch, boolean allowRe
113
111
114
112
private ResolvedSchemaInfo schema (
115
113
TableIdentifier identifier , Schema input , boolean allowRefresh ) {
116
- CacheItem cached = cache . getIfPresent (identifier );
114
+ CacheItem cached = tableCache . get (identifier );
117
115
Schema compatible = null ;
118
116
if (cached != null && cached .tableExists ) {
119
117
// This only works if the {@link Schema#equals(Object)} returns true for the old schema
@@ -164,7 +162,7 @@ private ResolvedSchemaInfo schema(
164
162
}
165
163
166
164
private PartitionSpec spec (TableIdentifier identifier , PartitionSpec spec , boolean allowRefresh ) {
167
- CacheItem cached = cache . getIfPresent (identifier );
165
+ CacheItem cached = tableCache . get (identifier );
168
166
if (cached != null && cached .tableExists ) {
169
167
for (PartitionSpec tableSpec : cached .specs .values ()) {
170
168
if (PartitionSpecEvolution .checkCompatibility (tableSpec , spec )) {
@@ -188,7 +186,7 @@ private Tuple2<Boolean, Exception> refreshTable(TableIdentifier identifier) {
188
186
return EXISTS ;
189
187
} catch (NoSuchTableException e ) {
190
188
LOG .debug ("Table doesn't exist {}" , identifier , e );
191
- cache .put (identifier , new CacheItem (false , null , null , null , 1 ));
189
+ tableCache .put (identifier , new CacheItem (false , null , null , null , 1 ));
192
190
return Tuple2 .of (false , e );
193
191
}
194
192
}
@@ -199,7 +197,7 @@ private boolean needsRefresh(CacheItem cacheItem, boolean allowRefresh) {
199
197
}
200
198
201
199
public void invalidate (TableIdentifier identifier ) {
202
- cache . invalidate (identifier );
200
+ tableCache . remove (identifier );
203
201
}
204
202
205
203
/** Handles timeout for missing items only. Caffeine performance causes noticeable delays. */
@@ -268,7 +266,7 @@ DataConverter recordConverter() {
268
266
}
269
267
270
268
@ VisibleForTesting
271
- Cache <TableIdentifier , CacheItem > getInternalCache () {
272
- return cache ;
269
+ LRUCache <TableIdentifier , CacheItem > getInternalCache () {
270
+ return tableCache ;
273
271
}
274
272
}
0 commit comments