Skip to content

Commit

Permalink
Cache should do entry update instead of remove/insert when DB is SQL #5
Browse files Browse the repository at this point in the history
  • Loading branch information
yurem committed Mar 9, 2021
1 parent f301136 commit 99107db
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.jans.orm.PersistenceEntryManager;
import io.jans.orm.exception.EntryPersistenceException;
import io.jans.orm.exception.operation.DuplicateEntryException;
import io.jans.orm.exception.operation.SearchException;
import io.jans.orm.model.base.SimpleBranch;
import io.jans.orm.search.filter.Filter;
import org.apache.commons.codec.binary.Base64;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class NativePersistenceCacheProvider extends AbstractCacheProvider<Persis

private boolean skipRemoveBeforePut;

private boolean attemptUpdateBeforeInsert;

@PostConstruct
public void init() {
}
Expand Down Expand Up @@ -78,6 +81,7 @@ public void create() {
String persistenceType = entryManager.getPersistenceType(baseDn);
// CouchbaseEntryManagerFactory.PERSISTENCE_TYPE
skipRemoveBeforePut = "couchbase".equals(persistenceType);
attemptUpdateBeforeInsert = "sql".equals(persistenceType);

log.info("Created NATIVE_PERSISTENCE cache provider. `baseDn`: " + baseDn);
} catch (Exception e) {
Expand Down Expand Up @@ -178,10 +182,14 @@ private void putImpl(String key, Object object, Date creationDate, int expiratio
entity.setDeletable(true);

try {
if (!skipRemoveBeforePut) {
silentlyRemoveEntityIfExists(entity.getDn());
}
entryManager.persist(entity);
if (attemptUpdateBeforeInsert) {
entryManager.merge(entity);
} else {
if (!skipRemoveBeforePut) {
silentlyRemoveEntityIfExists(entity.getDn());
}
entryManager.persist(entity);
}
} catch (EntryPersistenceException e) {
if (e.getCause() instanceof DuplicateEntryException) { // on duplicate, remove entry and try to persist again
try {
Expand All @@ -192,6 +200,18 @@ private void putImpl(String key, Object object, Date creationDate, int expiratio
log.error("Failed to retry put entry, key: " + originalKey + ", hashedKey: " + key + ", message: " + ex.getMessage(), ex);
}
}

if (e.getCause() instanceof SearchException) { // on lookup error, try to persist new entry
if (attemptUpdateBeforeInsert) {
try {
entryManager.persist(entity);
return;
} catch (Exception ex) {
log.error("Failed to retry put entry, key: " + originalKey + ", hashedKey: " + key + ", message: " + ex.getMessage(), ex);
}
}
}

log.error("Failed to put entry, key: " + originalKey + ", hashedKey: " + key + ", message: " + e.getMessage(), e);
} catch (Exception e) {
log.error("Failed to put entry, key: " + originalKey + ", hashedKey: " + key + ", message: " + e.getMessage(), e); // log as trace since it is perfectly valid that entry is removed by timer for example
Expand Down

0 comments on commit 99107db

Please sign in to comment.