Skip to content

Commit 021f9fe

Browse files
committed
Fix cache key
1 parent 4861eaf commit 021f9fe

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/main/java/com/imsweb/staging/StagingDataProvider.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,25 @@ public abstract class StagingDataProvider implements DataProvider {
6262

6363
protected Trie _trie;
6464

65+
/**
66+
* SchemaLookup is mutable which is not good when using it as a key in the lookup cache; switch
67+
* to a lookup key made up of the inputs as the cache key
68+
*/
69+
record SchemaLookupKey(Map<String, String> inputs) {
70+
71+
public SchemaLookupKey(Map<String, String> inputs) {
72+
this.inputs = inputs.entrySet().stream()
73+
.filter(e -> e.getKey() != null) // keep keys non-null
74+
.collect(Collectors.toUnmodifiableMap(
75+
Map.Entry::getKey,
76+
e -> e.getValue() == null ? "" : e.getValue() // normalize null values
77+
));
78+
}
79+
80+
}
81+
6582
// lookup cache
66-
private final Cache<SchemaLookup, List<Schema>> _lookupCache;
83+
private final Cache<SchemaLookupKey, List<Schema>> _lookupCache;
6784

6885
// site/hist cache
6986
private final Cache<String, Set<String>> _validValuesCache;
@@ -88,10 +105,13 @@ protected StagingDataProvider() {
88105
_mapper.setVisibility(PropertyAccessor.GETTER, Visibility.ANY);
89106

90107
// cache schema lookups
91-
_lookupCache = new Cache2kBuilder<SchemaLookup, List<Schema>>() {}
108+
_lookupCache = new Cache2kBuilder<SchemaLookupKey, List<Schema>>() {}
92109
.entryCapacity(500)
93110
.eternal(true)
94-
.loader(this::getSchemas)
111+
.loader(key -> {
112+
SchemaLookup lookup = new SchemaLookup(key.inputs());
113+
return getSchemas(lookup);
114+
})
95115
.build();
96116

97117
// cache the valid values for certain tables including site and histology
@@ -469,7 +489,7 @@ public List<Schema> lookupSchema(SchemaLookup lookup) {
469489
if (lookup.getSite() == null || lookup.getHistology() == null)
470490
return getSchemas(lookup);
471491

472-
return _lookupCache.get(lookup);
492+
return _lookupCache.get(new SchemaLookupKey(lookup.getInputs()));
473493
}
474494

475495
/**

0 commit comments

Comments
 (0)