Search code examples
springspring-mvcbrowser-cachecache-control

Spring HTTP cache management


I've seen that you can control cache http headers with the AnnotationMethodHandlerAdapter bean.

My problem is that I need to have a fine grane control on the cache (at method level). The best think would be to have something like an annotation like "@RequestCache(expire=60)".

Is there anything like this? What is the best way to accomplish this task?

Thanks, Andrea

Update: pap suggest to use an HandlerInterceptor, but I've seen multiple forum's post saying that it's not possible to get the target method inside an HandlerInterceptor and suggest to use regular AOP instead (not specifically for caching). The problem is that I don't want to add the request parameter to all my methods, only to make it accessible to the aspect. Is there a way to avoid this?


Solution

  • You can use the following approach described in Spring mvc reference manual

    Support for the 'Last-Modified' Response Header To Facilitate Content Caching

    @RequestMapping(value = "/modified")
    @ResponseBody
    public String getLastModified(WebRequest request) {
        if (request.checkNotModified(lastModified.getTime())) {
            logger.error("Was not modified.");
            return null;
        }
        logger.error("Was modified.");
        //processing
        return "viewName";
    }