enable client side caching for plot requests

Doesn't work perfectly yet, because the height/width sometimes changes
by one or two pixels.
This commit is contained in:
2018-04-29 19:16:13 +02:00
parent 024c14435c
commit 2903b5a828
5 changed files with 73 additions and 11 deletions

View File

@@ -1,19 +1,25 @@
package org.lucares.pdbui;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter implements HardcodedValues, PropertyKeys {
public class WebConfiguration implements WebMvcConfigurer, HardcodedValues, PropertyKeys {
private final String outputDir;
private final int cacheDurationInSeconds;
public WebConfiguration(@Value("${" + PATH_GENERATED_IMAGES + "}") final String outputDir) {
public WebConfiguration(@Value("${" + PATH_GENERATED_IMAGES + "}") final String outputDir,
@Value("${" + CACHE_IMAGES_DURATION_SECONDS + ":" + CACHE_IMAGES_DURATION_SECONDS_DEFAULT
+ "}") final int cacheDurationInSeconds) {
this.outputDir = outputDir;
this.cacheDurationInSeconds = cacheDurationInSeconds;
}
@Override
@@ -22,6 +28,7 @@ public class WebConfiguration extends WebMvcConfigurerAdapter implements Hardcod
final String pathPattern = "/" + WEB_IMAGE_OUTPUT_PATH + "/**";
final String resourceLocation = "file:" + Paths.get(outputDir).toAbsolutePath() + "/";
registry.addResourceHandler(pathPattern).addResourceLocations(resourceLocation);
final CacheControl cacheControl = CacheControl.maxAge(cacheDurationInSeconds, TimeUnit.SECONDS);
registry.addResourceHandler(pathPattern).addResourceLocations(resourceLocation).setCacheControl(cacheControl);
}
}