geolocationMap = geolocationCache.asMap();
- String country = geolocationMap.get(ipAddress);
-
- Log.debug("Got country from " + ipAddress + " out of cache: " + country + " (if null, country wasn't cached)");
+ String country = getCachedCountry(ipAddress);
if (country != null) {
+ Log.debug("Got cached country from IP Address " + ipAddress + ": " + country);
return country;
} else {
country = getUncachedCountry(ipAddress);
@@ -77,7 +74,7 @@ public class GeolocationCacheHandler {
* @see http://freegeoip.net
* @see #getCountry(String)
*/
- private static String getUncachedCountry(String ipAddress) {
+ public static String getUncachedCountry(String ipAddress) {
Benchmark.start("getUncachedCountry");
URL url;
@@ -104,4 +101,24 @@ public class GeolocationCacheHandler {
Benchmark.stop("getUncachedCountry");
}
}
+
+ /**
+ * Returns the cached country
+ *
+ * @param ipAddress The IP Address which is retrieved out of the cache
+ * @return The cached country, {@code null} if the country is not cached
+ */
+ public static String getCachedCountry(String ipAddress) {
+ return geolocationCache.getIfPresent(ipAddress);
+ }
+
+ /**
+ * Checks if the IP Address is cached
+ *
+ * @param ipAddress The IP Address which is checked
+ * @return true if the IP Address is cached
+ */
+ public static boolean isCached(String ipAddress) {
+ return geolocationCache.asMap().containsKey(ipAddress);
+ }
}
diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java
index e913c34c9..55af7836a 100644
--- a/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java
+++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java
@@ -4,6 +4,9 @@ import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import main.java.com.djrapitops.plan.ui.webserver.response.Response;
+import java.util.Map;
+import java.util.function.Predicate;
+
/**
* This class contains the page cache.
*
@@ -67,10 +70,35 @@ public class PageCacheHandler {
pageCache.put(identifier, response);
}
+ /**
+ * Checks if the page is cached.
+ *
+ * @param identifier The identifier of the page
+ * @return true if the page is cached
+ */
+ public static boolean isCached(String identifier) {
+ return pageCache.asMap().containsKey(identifier);
+ }
+
+ /**
+ * Removes all of the elements of this cache that satisfy the given predicate.
+ *
+ * @param filter a predicate which returns true for entries to be removed
+ */
+ public static void removeIf(Predicate filter) {
+ Map pageCacheMap = pageCache.asMap();
+
+ for (String identifier : pageCacheMap.keySet()) {
+ if (filter.test(identifier)) {
+ pageCache.invalidate(identifier);
+ }
+ }
+ }
+
/**
* Clears the cache from all its contents.
*/
public static void clearCache() {
- pageCache.asMap().clear();
+ pageCache.invalidateAll();
}
}
diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java
index 4d4d35b4c..23e521a86 100644
--- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java
+++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java
@@ -50,8 +50,7 @@ public class DataCacheGetQueue extends Queue