Why Spring message converters still being invoked in @Cachable methods

lets say I have two methods in my controller to support both json and xml.

@RequestMapping(value = "/get/response.json", method = RequestMethod.GET)
@Cacheable(JSON_CACHE)
public @ResponseBody JSONResponse getJsonResponse(){
    return responseService.getJsonResponse();
}
@RequestMapping(value = "/get/response.xml", method = RequestMethod.GET)
@Cacheable(XML_CACHE)
public  @ResponseBody XMLResponse getXmlResponse(){
    return responseService.getXmlResponse();
}

And two message converters, marshalling my objects into suitable response.

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter"/>
            <ref bean="xmlConverter" />
        </list>
    </property>
</bean>

The problem is that Spring 3.1, even though method is annotated with @Cachable, still invokes marshaller for every call. It caches state of the object before marshalling. This is not acceptable because performance is crucial here and marshalling is too expensive for me. I expected Spring to cache the final response in such case. Am I doing something wrong here?

Answers


To avoid this issue ehcache web caching can be used: http://www.ehcache.org/documentation/user-guide/web-caching

It works by simply adding filter to web.xml and provides caching of HTTP responses.

<filter>
    <filter-name>SimpleCachingHeadersPageCachingFilter</filter-name>
    <filter-class>net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilter
    </filter-class>
    <init-param>
        <param-name>suppressStackTrace</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>cacheName</param-name>
        <param-value>CachedPage2Cache</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>SimpleCachingHeadersPageCachingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Need Your Help

Performance of canvas in xcode simulator with retina display

xcode ipad html5 canvas ios-simulator

I have an html5 canvas game, limited to 30FPS. This runs fine (@30FPS) on the iPad2 and the xcode simulator when set to iPad2.

Check if file exist Linux bash

linux bash file

So I'm trying to check if a file exists or not and then the script is supposed to do something if it does. The problem I'm having is actually getting it to recognize that something is actually ther...