Removed Google Guava from PageCache #218

This commit is contained in:
Rsl1122 2018-01-11 16:26:12 +02:00
parent eeadbd2efb
commit b5f16a4400

View File

@ -2,9 +2,8 @@ package com.djrapitops.plan.systems.webserver.pagecache;
import com.djrapitops.plan.systems.webserver.response.InspectPageResponse; import com.djrapitops.plan.systems.webserver.response.InspectPageResponse;
import com.djrapitops.plan.systems.webserver.response.Response; import com.djrapitops.plan.systems.webserver.response.Response;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -14,16 +13,13 @@ import java.util.function.Predicate;
* It caches all Responses with their matching identifiers. * It caches all Responses with their matching identifiers.
* This reduces CPU cycles and the time to wait for loading the pages. * This reduces CPU cycles and the time to wait for loading the pages.
* This is especially useful in situations where multiple clients are accessing the server. * This is especially useful in situations where multiple clients are accessing the server.
* <p>
* This cache uses the Google Guava {@link Cache}.
* *
* @author Fuzzlemann * @author Fuzzlemann
* @since 3.6.0 * @since 3.6.0
*/ */
public class PageCache { public class PageCache {
private static final Cache<String, Response> pageCache = CacheBuilder.newBuilder() private static final Map<String, Response> pageCache = new HashMap<>();
.build();
/** /**
* Constructor used to hide the public constructor * Constructor used to hide the public constructor
@ -65,7 +61,7 @@ public class PageCache {
* @return The Response that was cached or {@code null} if it wasn't * @return The Response that was cached or {@code null} if it wasn't
*/ */
public static Response loadPage(String identifier) { public static Response loadPage(String identifier) {
return pageCache.getIfPresent(identifier); return pageCache.get(identifier);
} }
/** /**
@ -104,7 +100,7 @@ public class PageCache {
* @return true if the page is cached * @return true if the page is cached
*/ */
public static boolean isCached(String identifier) { public static boolean isCached(String identifier) {
return pageCache.asMap().containsKey(identifier); return pageCache.containsKey(identifier);
} }
/** /**
@ -113,11 +109,9 @@ public class PageCache {
* @param filter a predicate which returns true for entries to be removed * @param filter a predicate which returns true for entries to be removed
*/ */
public static void removeIf(Predicate<String> filter) { public static void removeIf(Predicate<String> filter) {
Map<String, Response> pageCacheMap = pageCache.asMap(); for (String identifier : pageCache.keySet()) {
for (String identifier : pageCacheMap.keySet()) {
if (filter.test(identifier)) { if (filter.test(identifier)) {
pageCache.invalidate(identifier); pageCache.remove(identifier);
} }
} }
} }
@ -126,6 +120,6 @@ public class PageCache {
* Clears the cache from all its contents. * Clears the cache from all its contents.
*/ */
public static void clearCache() { public static void clearCache() {
pageCache.invalidateAll(); pageCache.clear();
} }
} }