Skip to content

Commit

Permalink
Cache unavailable URI's
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#201

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Jan 4, 2019
1 parent 23f8355 commit 75f780f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions org.eclipse.lsp4xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.5.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import org.eclipse.lsp4xml.utils.FilesUtils;
import org.eclipse.lsp4xml.utils.URIUtils;

Expand All @@ -38,6 +42,10 @@
*
*/
public class CacheResourcesManager {
private final Cache<String, Boolean> unavailableURICache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.maximumSize(100)
.build();

private static final String CACHE_PATH = "cache";
private static final Logger LOGGER = Logger.getLogger(CacheResourcesManager.class.getName());
Expand Down Expand Up @@ -83,10 +91,16 @@ public CacheResourcesManager() {
}

public Path getResource(final String resourceURI) throws IOException {
LOGGER.info("DELETE THIS !@#");
Path resourceCachePath = getResourceCachePath(resourceURI);
if (Files.exists(resourceCachePath)) {
return resourceCachePath;
}

if(unavailableURICache.getIfPresent(resourceURI) != null) {
return null;
}

CompletableFuture<Path> f = null;
synchronized (resourcesLoading) {
if (resourcesLoading.containsKey(resourceURI)) {
Expand Down Expand Up @@ -139,11 +153,12 @@ private CompletableFuture<Path> downloadResource(final String resourceURI, Path
LOGGER.info("Downloaded " + resourceURI + " to " + resourceCachePath + " in " + elapsed + "ms");
} catch (Exception e) {
// Do nothing
unavailableURICache.put(resourceURI, true);
Throwable rootCause = getRootCause(e);
String error = "[" + rootCause.getClass().getTypeName() + "] " + rootCause.getMessage();
LOGGER.log(Level.SEVERE,
"Error while downloading " + resourceURI + " to " + resourceCachePath + " : " + error);
throw new CacheResourceDownloadedException("Error while downloading '" + resourceURI + "'.", e);
throw new CacheResourceDownloadedException("Error while downloading '" + resourceURI + "' to " + resourceCachePath + ".", e);
} finally {
synchronized (resourcesLoading) {
resourcesLoading.remove(resourceURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import java.io.IOException;

import org.junit.Test;

Expand All @@ -32,4 +35,9 @@ private void testCanUseCache(boolean useCacheEnabled) {
assertFalse(cacheResourcesManager.canUseCache("file:///foo"));
}

@Test
public void testUnavailableCache() {

}

}

0 comments on commit 75f780f

Please sign in to comment.