1

I'm tryint to set "Cache-control: max-age=60" to don't get data from server more often than 60 seconds. But I really don't know how to do it. I tried something like this:

@GetMapping(value = "/movies")
public List<Movie> getMoviesData() {
    CacheControl.maxAge(60, TimeUnit.SECONDS);
    return (List<Movie>) movieRepository.findAll();
}

But it doesn't display anything in postman headers. Later I tried this:

In pom.xml:

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1-m02</version>
</dependency>

And here is the code:

@GetMapping(value = "/movies")
public List<Movie> getMoviesData() {
    CacheControl cc = new CacheControl();
    cc.setMaxAge(60);
    cc.setPrivate(true);
    return (List<Movie>) movieRepository.findAll();
}

But it also display nothing. Now I tried this:

@GetMapping(value = "/movies")
public List<Movie> getMoviesData(HttpServletResponse response) {
    response.addHeader("Cache-Control", "max-age=60");
    response.addHeader("Last-Modified", new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss").format(new Date()));
    return (List<Movie>) movieRepository.findAll();
}

And it display Cache-Control →max-age=60, but Last-Modified refresh everytime when I send get request. So for example I have:

Last-Modified →21-01-2017_20:00:41

and after few seconds

Last-Modified →21-01-2017_20:00:46

So the max-age=60 doesn't work. Somebody have maybe any idea what I'm doing wrong and how to do it correctly?

2

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.