Try removing oldest sessions instead of maintaining max size 2
This commit is contained in:
parent
053d29df46
commit
917ff364a0
|
@ -37,15 +37,6 @@ public class ExplicitExpirySessionCacheDecorator implements SessionCache {
|
||||||
return key;
|
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) {
|
private void maintainSessionIds(String keyToAdd) {
|
||||||
if (keyToAdd != null || sessionCache.sessionExists(keyToAdd)) {
|
if (keyToAdd != null || sessionCache.sessionExists(keyToAdd)) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -45,12 +45,6 @@ public class PassiveExpiringSessionCache implements SessionCache {
|
||||||
return generatedId;
|
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
|
* 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.
|
* passed in, a new key is generated and returned.
|
||||||
|
|
|
@ -15,14 +15,6 @@ public interface SessionCache {
|
||||||
*/
|
*/
|
||||||
String cacheSession(ValidationEngine validationEngine);
|
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
|
* 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.
|
* passed in, a new key is generated and returned.
|
||||||
|
|
|
@ -490,19 +490,10 @@ public class ValidationService {
|
||||||
System.out.println("No such cached session exists for session id " + sessionId + ", re-instantiating validator.");
|
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
|
ValidationEngine validationEngine = getValidationEngineFromCliContext(cliContext, definitions, tt);
|
||||||
// or expire old sessions as needed)
|
sessionId = sessionCache.cacheSession(validationEngine);
|
||||||
sessionId = sessionCache.cacheSession(() -> {
|
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 {
|
} else {
|
||||||
System.out.println("Cached session exists for session id " + sessionId + ", returning stored validator session id. Cache size = " + sessionCache.getSessionIds().size());
|
System.out.println("Cached session exists for session id " + sessionId + ", returning stored validator session id. Cache size = " + sessionCache.getSessionIds().size());
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import java.util.ArrayList;
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
public class MaxSizeSessionCacheDecoratorTest {
|
public class ExplicitExpirySessionCacheDecoratorTest {
|
||||||
|
|
||||||
private List<ValidationEngine> getMockedEngines(int count) {
|
private List<ValidationEngine> getMockedEngines(int count) {
|
||||||
List<ValidationEngine> engines = new ArrayList<>();
|
List<ValidationEngine> engines = new ArrayList<>();
|
||||||
|
@ -71,17 +71,6 @@ public class MaxSizeSessionCacheDecoratorTest {
|
||||||
Assertions.assertFalse(sessionCache.expireOldestSession());
|
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) {
|
private String getKeyByIndex(LinkedHashMap<String, ValidationEngine> engineMap, int index) {
|
||||||
return (String) engineMap.keySet().toArray()[index];
|
return (String) engineMap.keySet().toArray()[index];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue