Added a resource cache between PlanFiles and FileResponse

This commit is contained in:
Rsl1122 2019-08-31 11:23:32 +03:00
parent d47dccc0f7
commit b78d3b139e
5 changed files with 177 additions and 1 deletions

View File

@ -40,6 +40,10 @@ public class JSONCache {
.expireAfterAccess(2, TimeUnit.MINUTES)
.build();
private JSONCache() {
// Static class
}
public static Response getOrCache(String identifier, Supplier<JSONResponse> jsonResponseSupplier) {
String found = cache.getIfPresent(identifier);
if (found == null) {

View File

@ -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<File> attemptToFind(String resourceName) {

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, String> cache = Caffeine.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.build();
private ResourceCache() {
// Static class
}
public static Resource getOrCache(String resourceName, Supplier<Resource> 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);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String> asLines() throws IOException {
return implementation.asLines();
}
@Override
public String asString() throws IOException {
String got = implementation.asString();
ResourceCache.cache(implementation.getResourceName(), got);
return got;
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String> asLines() {
return Arrays.asList(StringUtils.split(resource, "\r\n"));
}
@Override
public String asString() {
return resource;
}
}