TTL Object retrieving fix

This commit is contained in:
Fabio Sinibaldi 2021-09-07 10:30:27 +02:00
parent ad56a91310
commit 3c129484d4
2 changed files with 10 additions and 4 deletions

View File

@ -28,16 +28,21 @@ public abstract class AbstractScopedMap<T> implements Engine<T>{
public T getObject() throws ConfigurationException { public T getObject() throws ConfigurationException {
String currentScope=ContextUtils.getCurrentScope(); String currentScope=ContextUtils.getCurrentScope();
log.debug(name+" : obtaining object for context "+currentScope); log.debug(name+" : obtaining object for context "+currentScope);
scopeMap.putIfAbsent(currentScope, new TTLObject<T>(LocalDateTime.now(),retrieveObject()));
TTLObject<T> found=scopeMap.get(currentScope); TTLObject<T> found=scopeMap.get(currentScope);
if(found== null){
log.debug(name+" : init object for context "+currentScope);
return scopeMap.put(currentScope, new TTLObject<T>(LocalDateTime.now(),retrieveObject())).getTheObject();
}
if(TTL!=null) { if(TTL!=null) {
if(!found.getCreationTime().plus(TTL).isBefore(LocalDateTime.now())) { if(!found.getCreationTime().plus(TTL).isBefore(LocalDateTime.now())) {
log.debug(name+" : elapsed TTL, disposing.."); log.debug(name+" : elapsed TTL, disposing..");
dispose(found.getTheObject()); dispose(found.getTheObject());
found=scopeMap.put(currentScope, new TTLObject<T>(LocalDateTime.now(),retrieveObject())); found=scopeMap.put(currentScope, new TTLObject<T>(LocalDateTime.now(),retrieveObject()));
} }
} }else {log.debug(name+" : TTL is null, never disposing..");}
return found.getTheObject(); return found.getTheObject();
} }

View File

@ -8,13 +8,14 @@ import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.service.engine.ImplementationProvider; import org.gcube.application.geoportal.service.engine.ImplementationProvider;
import org.gcube.application.geoportal.service.model.internal.db.MongoConnection; import org.gcube.application.geoportal.service.model.internal.db.MongoConnection;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException; import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
@Slf4j @Slf4j
public class MongoClientProvider extends AbstractScopedMap<MongoClient>{ public class MongoClientProvider extends AbstractScopedMap<MongoClient>{
public MongoClientProvider() { public MongoClientProvider() {
super("MongoClient cache"); super("MongoClient cache");
// setTTL(Duration.of(10,ChronoUnit.MINUTES)); // setTTL(Duration.of(10, ChronoUnit.MINUTES));
} }
@Override @Override