Changed ResponseCache to use caffeine (5 minute invalidation) #685

This commit is contained in:
Rsl1122 2018-08-30 16:36:14 +03:00
parent fd5e02e1c9
commit c30650c0ee
3 changed files with 23 additions and 23 deletions

View File

@ -306,10 +306,6 @@
<artifactId>guice</artifactId>
<groupId>com.google.inject</groupId>
</exclusion>
<exclusion>
<artifactId>caffeine</artifactId>
<groupId>com.github.ben-manes.caffeine</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.github.ben-manes.caffeine</groupId>

View File

@ -122,6 +122,13 @@
<version>2.9.0</version>
</dependency>
<!-- Cache with invalidation -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.6.2</version>
</dependency>
<!-- Metrics -->
<dependency>
<groupId>org.bstats</groupId>

View File

@ -1,10 +1,11 @@
package com.djrapitops.plan.system.webserver.cache;
import com.djrapitops.plan.system.webserver.response.Response;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
/**
@ -19,7 +20,9 @@ import java.util.function.Supplier;
*/
public class ResponseCache {
private static final Map<String, Response> cache = new HashMap<>();
private static final Cache<String, Response> cache = Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.build();
/**
* Constructor used to hide the public constructor
@ -41,17 +44,7 @@ public class ResponseCache {
* @return The Response that was cached or created by the the {@link Response} {@link Supplier}
*/
public static Response loadResponse(String identifier, Supplier<Response> loader) {
Response response = loadResponse(identifier);
if (response != null) {
return response;
}
response = loader.get();
cache.put(identifier, response);
return response;
return cache.get(identifier, k -> loader.get());
}
/**
@ -61,7 +54,7 @@ public class ResponseCache {
* @return The Response that was cached or {@code null} if it wasn't
*/
public static Response loadResponse(String identifier) {
return cache.get(identifier);
return cache.getIfPresent(identifier);
}
/**
@ -84,21 +77,25 @@ public class ResponseCache {
* @return true if the page is cached
*/
public static boolean isCached(String identifier) {
return cache.containsKey(identifier);
return cache.getIfPresent(identifier) != null;
}
/**
* Clears the cache from all its contents.
*/
public static void clearCache() {
cache.clear();
cache.invalidateAll();
}
public static Set<String> getCacheKeys() {
return cache.keySet();
return cache.asMap().keySet();
}
public static long getEstimatedSize() {
return cache.estimatedSize();
}
public static void clearResponse(String identifier) {
cache.remove(identifier);
cache.invalidate(identifier);
}
}