diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONCache.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONCache.java index 07e506fa0..39fa80b29 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONCache.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONCache.java @@ -40,6 +40,10 @@ public class JSONCache { .expireAfterAccess(2, TimeUnit.MINUTES) .build(); + private JSONCache() { + // Static class + } + public static Response getOrCache(String identifier, Supplier jsonResponseSupplier) { String found = cache.getIfPresent(identifier); if (found == null) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/file/PlanFiles.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/PlanFiles.java index afef3ac7d..c6def1f63 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/file/PlanFiles.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/PlanFiles.java @@ -115,7 +115,10 @@ public class PlanFiles implements SubSystem { * @return a {@link Resource} for accessing the resource, either from the plugin folder or jar. */ public Resource getCustomizableResourceOrDefault(String resourceName) { - return attemptToFind(resourceName).map(file -> (Resource) new FileResource(resourceName, file)).orElse(getResourceFromJar(resourceName)); + return ResourceCache.getOrCache(resourceName, () -> + attemptToFind(resourceName).map(file -> (Resource) new FileResource(resourceName, file)) + .orElse(getResourceFromJar(resourceName)) + ); } private Optional attemptToFind(String resourceName) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/file/ResourceCache.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/ResourceCache.java new file mode 100644 index 000000000..4c7886b33 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/ResourceCache.java @@ -0,0 +1,51 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.storage.file; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; + +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +/** + * In-memory cache for different resources on disk or jar. + * + * @author Rsl1122 + */ +public class ResourceCache { + + private static final Cache cache = Caffeine.newBuilder() + .expireAfterAccess(1, TimeUnit.MINUTES) + .build(); + + private ResourceCache() { + // Static class + } + + public static Resource getOrCache(String resourceName, Supplier resourceSupplier) { + String found = cache.getIfPresent(resourceName); + if (found == null) { + return new StringCachingResource(resourceSupplier.get()); + } + return new StringResource(resourceName, found); + } + + public static void cache(String resourceName, String got) { + cache.put(resourceName, got); + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/file/StringCachingResource.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/StringCachingResource.java new file mode 100644 index 000000000..80979c4ca --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/StringCachingResource.java @@ -0,0 +1,57 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.storage.file; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +/** + * Resource decorator to cache result of asString method call in {@link ResourceCache}. + * + * @author Rsl1122 + */ +public class StringCachingResource implements Resource { + + private final Resource implementation; + + StringCachingResource(Resource implementation) { + this.implementation = implementation; + } + + @Override + public String getResourceName() { + return implementation.getResourceName(); + } + + @Override + public InputStream asInputStream() throws IOException { + return implementation.asInputStream(); + } + + @Override + public List asLines() throws IOException { + return implementation.asLines(); + } + + @Override + public String asString() throws IOException { + String got = implementation.asString(); + ResourceCache.cache(implementation.getResourceName(), got); + return got; + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/file/StringResource.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/StringResource.java new file mode 100644 index 000000000..0f421f7cd --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/StringResource.java @@ -0,0 +1,61 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.storage.file; + +import org.apache.commons.lang3.StringUtils; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; + +/** + * {@link Resource} implementation for a {@link String}. + * + * @author Rsl1122 + */ +public class StringResource implements Resource { + + private final String resourceName; + private final String resource; + + StringResource(String resourceName, String resource) { + this.resourceName = resourceName; + this.resource = resource; + } + + @Override + public String getResourceName() { + return resourceName; + } + + @Override + public InputStream asInputStream() { + return new ByteArrayInputStream(resource.getBytes(StandardCharsets.UTF_8)); + } + + @Override + public List asLines() { + return Arrays.asList(StringUtils.split(resource, "\r\n")); + } + + @Override + public String asString() { + return resource; + } +} \ No newline at end of file