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:
@@ -1,6 +1,7 @@
|
||||
package org.lucares.pdbui;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
@@ -36,7 +37,9 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.http.CacheControl;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -49,9 +52,13 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Controller
|
||||
@EnableAutoConfiguration
|
||||
public class PdbController implements HardcodedValues {
|
||||
public class PdbController implements HardcodedValues, PropertyKeys {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PdbController.class);
|
||||
|
||||
@@ -60,9 +67,12 @@ public class PdbController implements HardcodedValues {
|
||||
|
||||
private final ReentrantLock plotterLock = new ReentrantLock();
|
||||
|
||||
@Value("${mode.production:true}")
|
||||
@Value("${" + PRODUCTION_MODE + ":true}")
|
||||
private boolean modeProduction;
|
||||
|
||||
@Value("${" + CACHE_IMAGES_DURATION_SECONDS + ":" + CACHE_IMAGES_DURATION_SECONDS_DEFAULT + "}")
|
||||
private int cacheDurationInSeconds;
|
||||
|
||||
public PdbController(final PerformanceDb db, final Plotter plotter) {
|
||||
this.db = db;
|
||||
this.plotter = plotter;
|
||||
@@ -79,13 +89,29 @@ public class PdbController implements HardcodedValues {
|
||||
return new ModelAndView(view, model);
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/plots", //
|
||||
method = RequestMethod.GET, //
|
||||
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, //
|
||||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE //
|
||||
)
|
||||
@ResponseBody
|
||||
ResponseEntity<PlotResponse> createPlotGet(@RequestParam(name = "request") final String request)
|
||||
throws InternalPlottingException, InterruptedException, JsonParseException, JsonMappingException,
|
||||
IOException {
|
||||
|
||||
final ObjectMapper objectMapper = new ObjectMapper();
|
||||
final PlotRequest plotRequest = objectMapper.readValue(request, PlotRequest.class);
|
||||
|
||||
return createPlot(plotRequest);
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/plots", //
|
||||
method = RequestMethod.POST, //
|
||||
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, //
|
||||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE //
|
||||
)
|
||||
@ResponseBody
|
||||
PlotResponse createPlot(@RequestBody final PlotRequest request)
|
||||
ResponseEntity<PlotResponse> createPlot(@RequestBody final PlotRequest request)
|
||||
throws InternalPlottingException, InterruptedException {
|
||||
|
||||
final PlotSettings plotSettings = PlotSettingsTransformer.toSettings(request);
|
||||
@@ -107,7 +133,11 @@ public class PdbController implements HardcodedValues {
|
||||
: imageUrl;
|
||||
|
||||
final PlotResponseStats stats = PlotResponseStats.fromDataSeries(result.getDataSeries());
|
||||
return new PlotResponse(stats, imageUrl, thumbnailUrl);
|
||||
final PlotResponse plotResponse = new PlotResponse(stats, imageUrl, thumbnailUrl);
|
||||
|
||||
final CacheControl cacheControl = CacheControl.maxAge(cacheDurationInSeconds, TimeUnit.SECONDS);
|
||||
|
||||
return ResponseEntity.ok().cacheControl(cacheControl).body(plotResponse);
|
||||
} catch (final NoDataPointsException e) {
|
||||
throw new NotFoundException(e);
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user