Fix excessive loading of the model notes from the server

This commit is contained in:
0xb00bface 2023-04-25 20:45:23 +02:00
parent 19850eab31
commit c62634de92
1 changed files with 16 additions and 9 deletions

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@ -32,6 +33,9 @@ public class RemoteModelNotesService extends RemoteService implements ModelNotes
private final HttpClient httpClient;
private final Config config;
private Map<String, String> notesCache = Collections.emptyMap();
private Instant lastUpdate = Instant.EPOCH;
private final LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.expireAfterWrite(3, TimeUnit.SECONDS)
.maximumSize(10000)
@ -67,16 +71,19 @@ public class RemoteModelNotesService extends RemoteService implements ModelNotes
});
}
private String updateCache(String modelUrl) {
try {
var modelNotes = loadAllModelNotes();
for (Map.Entry<String, String> entry : modelNotes.entrySet()) {
cache.put(entry.getKey(), entry.getValue());
private synchronized String updateCache(String modelUrl) {
if (lastUpdate.isBefore(Instant.now().minusSeconds(3))) {
try {
notesCache = loadAllModelNotes();
lastUpdate = Instant.now();
for (Map.Entry<String, String> entry : notesCache.entrySet()) {
cache.put(entry.getKey(), entry.getValue());
}
} catch (Exception e) {
throw new CacheLoader.InvalidCacheLoadException("Loading of model notes from server failed");
}
return modelNotes.get(modelUrl);
} catch (Exception e) {
throw new CacheLoader.InvalidCacheLoadException("Loading of model notes from server failed");
}
return Optional.ofNullable(notesCache.get(modelUrl)).orElse("");
}
@Override
@ -108,7 +115,7 @@ public class RemoteModelNotesService extends RemoteService implements ModelNotes
public Optional<String> loadModelNotes(String modelUrl) throws IOException {
try {
log.trace("Loading model notes for {}", modelUrl);
return Optional.of(cache.get(modelUrl));
return Optional.ofNullable(cache.get(modelUrl));
} catch (ExecutionException e) {
throw new IOException(e);
}