Skip to content

Commit

Permalink
[Fix] Fix stale cache entry for deep term hierarchies (depth > 2).
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Dec 10, 2024
1 parent c4bfa3f commit 5bc2cc8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,30 @@ private void evictPossiblyCachedReferences(Term term) {
em.getEntityManagerFactory().getCache().evict(Term.class, term.getUri(), null);
em.getEntityManagerFactory().getCache().evict(TermDto.class, term.getUri(), null);
em.getEntityManagerFactory().getCache().evict(TermInfo.class, term.getUri(), null);
// Should be replaced by implementation of https://github.com/kbss-cvut/jopa/issues/92
Utils.emptyIfNull(term.getParentTerms()).forEach(pt -> {
em.getEntityManagerFactory().getCache().evict(Term.class, pt.getUri(), null);
em.getEntityManagerFactory().getCache().evict(TermDto.class, pt.getUri(), null);
subTermsCache.evict(pt.getUri());
});
term.setSubTerms(getSubTerms(term));
// Should be replaced by implementation of https://github.com/kbss-cvut/jopa/issues/92
Utils.emptyIfNull(term.getSubTerms())
.forEach(st -> {
em.getEntityManagerFactory().getCache().evict(Term.class, st.getUri(), null);
em.getEntityManagerFactory().getCache().evict(TermDto.class, st.getUri(), null);
});
evictAllCachedDescendants(term);
}

/**
* Evicts all descendants of the specified term from the cache - default context.
* <p>
* This is done to prevent stale references through the parentTerms chain.
*
* @param term Term whose descendants to evict
*/
private void evictAllCachedDescendants(Term term) {
em.createNativeQuery("SELECT ?child WHERE { ?t ?hasChild* ?child . }", URI.class)
.setParameter("hasChild", URI.create(SKOS.NARROWER))
.setParameter("t", term).getResultStream().forEach(st -> {
em.getEntityManagerFactory().getCache().evict(Term.class, st, null);
em.getEntityManagerFactory().getCache().evict(TermDto.class, st, null);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ public void remove(@Nonnull Term term) {
@Transactional(propagation = Propagation.REQUIRES_NEW)
@PreAuthorize("@termAuthorizationService.canModify(#term)")
public void analyzeTermDefinition(AbstractTerm term, URI vocabularyIri) {
term = findRequired(term.getUri()); // required when throttling for persistent context
Objects.requireNonNull(term);
term = repositoryService.findRequired(term.getUri()); // required when throttling for persistent context
if (term.getDefinition() == null || term.getDefinition().isEmpty()) {
return;
}
Expand Down

0 comments on commit 5bc2cc8

Please sign in to comment.