-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP add a decorator to limit cache size
- Loading branch information
Showing
5 changed files
with
179 additions
and
6 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...tion/src/main/java/org/hl7/fhir/validation/cli/services/MaxSizeSessionCacheDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.hl7.fhir.validation.cli.services; | ||
|
||
import org.hl7.fhir.validation.ValidationEngine; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Supplier; | ||
|
||
public class MaxSizeSessionCacheDecorator implements SessionCache { | ||
|
||
public final int maxSize; | ||
public final SessionCache sessionCache; | ||
|
||
private final List<String> sessionIds; | ||
|
||
public MaxSizeSessionCacheDecorator(SessionCache sessionCache, int maxSize) { | ||
this.sessionCache = sessionCache; | ||
this.maxSize = maxSize; | ||
this.sessionIds = new ArrayList<>(sessionCache.getSessionIds()); | ||
if (this.sessionIds.size() > maxSize) { | ||
throw new IllegalArgumentException("Session cache size exceeds the maximum size"); | ||
} | ||
} | ||
|
||
@Override | ||
public String cacheSession(ValidationEngine validationEngine) { | ||
checkSizeAndMaintainMax(null); | ||
String key = sessionCache.cacheSession(validationEngine); | ||
sessionIds.add(key); | ||
return key; | ||
} | ||
|
||
@Override | ||
public String cacheSession(Supplier<ValidationEngine> validationEngineSupplier) { | ||
checkSizeAndMaintainMax(null); | ||
ValidationEngine validationEngine = validationEngineSupplier.get(); | ||
return sessionCache.cacheSession(validationEngine); | ||
} | ||
|
||
private void checkSizeAndMaintainMax(String keyToAdd) { | ||
if (keyToAdd != null || sessionCache.sessionExists(keyToAdd)) { | ||
return; | ||
} | ||
Set<String> sessionIds = sessionCache.getSessionIds(); | ||
//Sync our tracked keys, in case the underlying cache has changed | ||
this.sessionIds.removeIf(key -> !sessionIds.contains(key)); | ||
|
||
if (this.sessionIds.size() >= maxSize) { | ||
final String key = this.sessionIds.remove(0); | ||
sessionCache.removeSession(key); | ||
} | ||
} | ||
|
||
@Override | ||
public String cacheSession(String sessionId, ValidationEngine validationEngine) { | ||
checkSizeAndMaintainMax(sessionId); | ||
cacheSession(sessionId, validationEngine); | ||
return sessionCache.cacheSession( | ||
sessionId, validationEngine); | ||
} | ||
|
||
@Override | ||
public boolean sessionExists(String sessionId) { | ||
return sessionCache.sessionExists(sessionId); | ||
} | ||
|
||
@Override | ||
public ValidationEngine removeSession(String sessionId) { | ||
sessionIds.remove(sessionId); | ||
return sessionCache.removeSession(sessionId); | ||
} | ||
|
||
@Override | ||
public ValidationEngine fetchSessionValidatorEngine(String sessionId) { | ||
return sessionCache.fetchSessionValidatorEngine(sessionId); | ||
} | ||
|
||
@Override | ||
public Set<String> getSessionIds() { | ||
return sessionCache.getSessionIds(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
.../src/test/java/org/hl7/fhir/validation/cli/services/MaxSizeSessionCacheDecoratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.hl7.fhir.validation.cli.services; | ||
|
||
import org.hl7.fhir.validation.ValidationEngine; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
public class MaxSizeSessionCacheDecoratorTest { | ||
|
||
private List<ValidationEngine> getMockedEngines(int count) { | ||
List<ValidationEngine> engines = new ArrayList<>(); | ||
for (int i = 0; i < count; i++) { | ||
engines.add(mock(ValidationEngine.class)); | ||
} | ||
return engines; | ||
} | ||
|
||
private LinkedHashMap<String, ValidationEngine> addMockedEngines(SessionCache cache, int count) { | ||
LinkedHashMap<String, ValidationEngine> engineMap = new LinkedHashMap<>(); | ||
List<ValidationEngine> engines = getMockedEngines(count); | ||
for (ValidationEngine engine : engines) { | ||
String key = cache.cacheSession(engine); | ||
engineMap.put(key, engine); | ||
} | ||
return engineMap; | ||
} | ||
|
||
@Test | ||
public void trivialCase() { | ||
|
||
MaxSizeSessionCacheDecorator maxSizeSessionCacheDecorator = new MaxSizeSessionCacheDecorator(new PassiveExpiringSessionCache(), 4); | ||
|
||
LinkedHashMap<String, ValidationEngine> initialEngines = addMockedEngines(maxSizeSessionCacheDecorator, 3); | ||
|
||
Assertions.assertEquals(3, maxSizeSessionCacheDecorator.getSessionIds().size()); | ||
|
||
List<ValidationEngine> newEngines = getMockedEngines(2); | ||
|
||
for (ValidationEngine engine : newEngines) { | ||
maxSizeSessionCacheDecorator.cacheSession(engine); | ||
} | ||
|
||
Assertions.assertEquals(4, maxSizeSessionCacheDecorator.getSessionIds().size()); | ||
|
||
Assertions.assertTrue(maxSizeSessionCacheDecorator.getSessionIds().contains() | ||
} | ||
|
||
} |