diff --git a/core/java/src/net/i2p/client/naming/BlockfileNamingService.java b/core/java/src/net/i2p/client/naming/BlockfileNamingService.java index 6d0e1bb6f3bd636e435c1f330fc4ea1efe09f8a9..8172ee7fd2b88b713342f1da52742bac6d6fe57f 100644 --- a/core/java/src/net/i2p/client/naming/BlockfileNamingService.java +++ b/core/java/src/net/i2p/client/naming/BlockfileNamingService.java @@ -92,6 +92,7 @@ public class BlockfileNamingService extends DummyNamingService { private final RAIFile _raf; private final List<String> _lists; private final List<InvalidEntry> _invalid; + private final Map<String, String> _negativeCache; private volatile boolean _isClosed; private final boolean _readOnly; private boolean _needsUpgrade; @@ -118,6 +119,9 @@ public class BlockfileNamingService extends DummyNamingService { private static final String PROP_ADDED = "a"; private static final String PROP_SOURCE = "s"; + private static final String DUMMY = ""; + private static final int NEGATIVE_CACHE_SIZE = 32; + /** * Opens the database at hostsdb.blockfile or creates a new * one and imports entries from hosts.txt, userhosts.txt, and privatehosts.txt. @@ -132,6 +136,7 @@ public class BlockfileNamingService extends DummyNamingService { super(context); _lists = new ArrayList(); _invalid = new ArrayList(); + _negativeCache = new LHM(NEGATIVE_CACHE_SIZE); BlockFile bf = null; RAIFile raf = null; boolean readOnly = false; @@ -628,6 +633,10 @@ public class BlockfileNamingService extends DummyNamingService { } String key = hostname.toLowerCase(Locale.US); + synchronized(_negativeCache) { + if (_negativeCache.get(key) != null) + return null; + } synchronized(_bf) { if (_isClosed) return null; @@ -650,8 +659,13 @@ public class BlockfileNamingService extends DummyNamingService { } deleteInvalid(); } - if (d != null) + if (d != null) { putCache(hostname, d); + } else { + synchronized(_negativeCache) { + _negativeCache.put(key, DUMMY); + } + } return d; } @@ -683,6 +697,9 @@ public class BlockfileNamingService extends DummyNamingService { return false; } String key = hostname.toLowerCase(Locale.US); + synchronized(_negativeCache) { + _negativeCache.remove(key); + } String listname = FALLBACK_LIST; Properties props = new Properties(); props.setProperty(PROP_ADDED, Long.toString(_context.clock().now())); @@ -1031,6 +1048,9 @@ public class BlockfileNamingService extends DummyNamingService { } _isClosed = true; } + synchronized(_negativeCache) { + _negativeCache.clear(); + } clearCache(); } diff --git a/core/java/src/net/i2p/client/naming/DummyNamingService.java b/core/java/src/net/i2p/client/naming/DummyNamingService.java index 21f8a7c7cc1f0de892979ddd57431e84e6450198..63c6cf26484d67f9b94d1d5d1ae06da6a9651428 100644 --- a/core/java/src/net/i2p/client/naming/DummyNamingService.java +++ b/core/java/src/net/i2p/client/naming/DummyNamingService.java @@ -116,7 +116,7 @@ class DummyNamingService extends NamingService { } } - private static class LHM<K, V> extends LinkedHashMap<K, V> { + protected static class LHM<K, V> extends LinkedHashMap<K, V> { private final int _max; public LHM(int max) {