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