0

I am making caching solution for a method located in a servlet.

Servlet looks like following:

public class DataOptimizedServlet extends DataServlet {
    ...
    @Override
    @Cacheable(value = "dataOptimized", key = "#req.getRequestURI()")
    public byte[] getData(HttpServletRequest req) {
        // data retrieval logic
    }
    ...
}

in ehcache.xml I have following configuration:

<cache alias="dataOptimized">
    <expiry>
        <ttl unit="hours">30</ttl>
    </expiry>
    <resources>
        <heap unit="entries">20000</heap>
        <offheap unit="MB">200</offheap>
    </resources>
</cache>

Also in ctx-cache.xml file is following configuration (and this file is imported in web.xml):

<cache:annotation-driven cache-manager="ehCacheManager" />

<bean id="ehCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
    <property name="cacheManager">
        <bean class="org.springframework.cache.jcache.JCacheManagerFactoryBean" p:cacheManagerUri="classpath:ehcache.xml" />
    </property>
</bean>

But it does not work, method getData still gets hit for the same URLs. I have many @Cacheables working in a project and configured in the same manner, but none is in servlet directly (they're in services).

What am I missing?

michal.jakubeczy
  • 4,457
  • 1
  • 30
  • 43

1 Answers1

0

are you using Spring? IF yes, consider that spring documentation spring doc says: "When using proxies, you should apply the @Cache* annotations only to methods with public visibility. If you do annotate protected, private or package-visible methods with these annotations, no error is raised, but the annotated method does not exhibit the configured caching settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods as it changes the bytecode itself." So you should change your method visibility to protected to public

maffix82
  • 1
  • 1
  • good point, but I was aware of this and changed visibility to public. Unfotunately, the same result. I am more worried it is somehow because of it is in Servlet, but cannot find any resource which proves my assumption. – michal.jakubeczy Mar 31 '20 at 13:08