Avoid complications with merges in the session cache, evict and then update.

This commit is contained in:
Greg Burd 2017-11-01 11:09:14 -04:00
parent e1884cf52d
commit 857eadff45

View file

@ -234,7 +234,7 @@ public class HelenusSession extends AbstractSessionOperations implements Closeab
} }
String tableName = CacheUtil.schemaName(facets); String tableName = CacheUtil.schemaName(facets);
List<String[]> facetCombinations = CacheUtil.flattenFacets(boundFacets); List<String[]> facetCombinations = CacheUtil.flattenFacets(boundFacets);
mergeAndUpdateCacheValues(pojo, tableName, facetCombinations); replaceCachedFacetValues(pojo, tableName, facetCombinations);
} }
@Override @Override
@ -265,10 +265,9 @@ public class HelenusSession extends AbstractSessionOperations implements Closeab
boundFacets.add(facet); boundFacets.add(facet);
} }
} }
// NOTE: should equal `String tableName = CacheUtil.schemaName(facets);`
List<String[]> facetCombinations = CacheUtil.flattenFacets(boundFacets); List<String[]> facetCombinations = CacheUtil.flattenFacets(boundFacets);
String tableName = CacheUtil.schemaName(boundFacets); String tableName = CacheUtil.schemaName(boundFacets);
mergeAndUpdateCacheValues(pojo, tableName, facetCombinations); replaceCachedFacetValues(pojo, tableName, facetCombinations);
} }
} }
@ -284,21 +283,11 @@ public class HelenusSession extends AbstractSessionOperations implements Closeab
} }
} }
private void mergeAndUpdateCacheValues(Object pojo, String tableName, List<String[]> facetCombinations) { private void replaceCachedFacetValues(Object pojo, String tableName, List<String[]> facetCombinations) {
Object merged = null;
for (String[] combination : facetCombinations) { for (String[] combination : facetCombinations) {
String cacheKey = tableName + "." + Arrays.toString(combination); String cacheKey = tableName + "." + Arrays.toString(combination);
Object value = sessionCache.get(cacheKey); sessionCache.invalidate(cacheKey);
if (value == null) {
sessionCache.put(cacheKey, pojo); sessionCache.put(cacheKey, pojo);
} else {
if (merged == null) {
merged = pojo;
} else {
merged = CacheUtil.merge(value, pojo);
}
sessionCache.put(cacheKey, merged);
}
} }
} }