mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-24 10:11:53 +01:00
Changed ResponseCache to use caffeine (5 minute invalidation) #685
This commit is contained in:
parent
fd5e02e1c9
commit
c30650c0ee
@ -306,10 +306,6 @@
|
|||||||
<artifactId>guice</artifactId>
|
<artifactId>guice</artifactId>
|
||||||
<groupId>com.google.inject</groupId>
|
<groupId>com.google.inject</groupId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
<exclusion>
|
|
||||||
<artifactId>caffeine</artifactId>
|
|
||||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||||
|
@ -122,6 +122,13 @@
|
|||||||
<version>2.9.0</version>
|
<version>2.9.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Cache with invalidation -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||||
|
<artifactId>caffeine</artifactId>
|
||||||
|
<version>2.6.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Metrics -->
|
<!-- Metrics -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bstats</groupId>
|
<groupId>org.bstats</groupId>
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package com.djrapitops.plan.system.webserver.cache;
|
package com.djrapitops.plan.system.webserver.cache;
|
||||||
|
|
||||||
import com.djrapitops.plan.system.webserver.response.Response;
|
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.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,7 +20,9 @@ import java.util.function.Supplier;
|
|||||||
*/
|
*/
|
||||||
public class ResponseCache {
|
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
|
* 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}
|
* @return The Response that was cached or created by the the {@link Response} {@link Supplier}
|
||||||
*/
|
*/
|
||||||
public static Response loadResponse(String identifier, Supplier<Response> loader) {
|
public static Response loadResponse(String identifier, Supplier<Response> loader) {
|
||||||
Response response = loadResponse(identifier);
|
return cache.get(identifier, k -> loader.get());
|
||||||
|
|
||||||
if (response != null) {
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
response = loader.get();
|
|
||||||
|
|
||||||
cache.put(identifier, response);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,7 +54,7 @@ public class ResponseCache {
|
|||||||
* @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 loadResponse(String identifier) {
|
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
|
* @return true if the page is cached
|
||||||
*/
|
*/
|
||||||
public static boolean isCached(String identifier) {
|
public static boolean isCached(String identifier) {
|
||||||
return cache.containsKey(identifier);
|
return cache.getIfPresent(identifier) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the cache from all its contents.
|
* Clears the cache from all its contents.
|
||||||
*/
|
*/
|
||||||
public static void clearCache() {
|
public static void clearCache() {
|
||||||
cache.clear();
|
cache.invalidateAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<String> getCacheKeys() {
|
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) {
|
public static void clearResponse(String identifier) {
|
||||||
cache.remove(identifier);
|
cache.invalidate(identifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user