Try removing oldest sessions instead of maintaining max size 2

This commit is contained in:
dotasek 2024-06-25 13:33:59 -04:00
parent 053d29df46
commit 917ff364a0
5 changed files with 4 additions and 47 deletions

View File

@ -37,15 +37,6 @@ public class ExplicitExpirySessionCacheDecorator implements SessionCache {
return key;
}
@Override
public String cacheSession(Supplier<ValidationEngine> validationEngineSupplier) {
maintainSessionIds(null);
ValidationEngine validationEngine = validationEngineSupplier.get();
String key = sessionCache.cacheSession(validationEngine);
sessionIds.add(key);
return key;
}
private void maintainSessionIds(String keyToAdd) {
if (keyToAdd != null || sessionCache.sessionExists(keyToAdd)) {
return;

View File

@ -45,12 +45,6 @@ public class PassiveExpiringSessionCache implements SessionCache {
return generatedId;
}
@Override
public String cacheSession(Supplier<ValidationEngine> validationEngineSupplier) {
ValidationEngine engine = validationEngineSupplier.get();
return this.cacheSession(engine);
}
/**
* Stores the initialized {@link ValidationEngine} in the cache with the passed in id as the key. If a null key is
* passed in, a new key is generated and returned.

View File

@ -15,14 +15,6 @@ public interface SessionCache {
*/
String cacheSession(ValidationEngine validationEngine);
/**
* Uses the passed {@link Supplier} to generate a {@link ValidationEngine} and add it to the cache. Returns the
* session id that will be associated with the generated instance.
* @param validationEngineSupplier {@link Supplier} of {@link ValidationEngine}
* @return The {@link String} id associated with the stored instance.
*/
String cacheSession(Supplier<ValidationEngine> validationEngineSupplier);
/**
* Stores the initialized {@link ValidationEngine} in the cache with the passed in id as the key. If a null key is
* passed in, a new key is generated and returned.

View File

@ -490,19 +490,10 @@ public class ValidationService {
System.out.println("No such cached session exists for session id " + sessionId + ", re-instantiating validator.");
}
// Send a supplier instead of instantiating. This will permit the sessionCache to manage existing sessions (drop
// or expire old sessions as needed)
sessionId = sessionCache.cacheSession(() -> {
ValidationEngine validationEngine = getValidationEngineFromCliContext(cliContext, definitions, tt);
sessionId = sessionCache.cacheSession(validationEngine);
System.out.println("Cached new session. Cache size = " + sessionCache.getSessionIds().size());
try {
return buildValidationEngine(cliContext, definitions, tt);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
});
} else {
System.out.println("Cached session exists for session id " + sessionId + ", returning stored validator session id. Cache size = " + sessionCache.getSessionIds().size());
}

View File

@ -11,7 +11,7 @@ import java.util.ArrayList;
import static org.mockito.Mockito.mock;
public class MaxSizeSessionCacheDecoratorTest {
public class ExplicitExpirySessionCacheDecoratorTest {
private List<ValidationEngine> getMockedEngines(int count) {
List<ValidationEngine> engines = new ArrayList<>();
@ -71,17 +71,6 @@ public class MaxSizeSessionCacheDecoratorTest {
Assertions.assertFalse(sessionCache.expireOldestSession());
}
@Test
public void producerAddTest() {
ExplicitExpirySessionCacheDecorator maxSizeSessionCacheDecorator = new ExplicitExpirySessionCacheDecorator(new PassiveExpiringSessionCache());
ValidationEngine producedEngine = mock(ValidationEngine.class);
String sessionId = maxSizeSessionCacheDecorator.cacheSession(() -> {
return producedEngine;
});
Assertions.assertEquals(1, maxSizeSessionCacheDecorator.getSessionIds().size());
Assertions.assertSame(producedEngine, maxSizeSessionCacheDecorator.fetchSessionValidatorEngine(sessionId));
}
private String getKeyByIndex(LinkedHashMap<String, ValidationEngine> engineMap, int index) {
return (String) engineMap.keySet().toArray()[index];
}