简体   繁体   English

EhCache - servlet 缓存

[英]EhCache - servlet caching

I am making caching solution for a method located in a servlet.我正在为位于 servlet 中的方法制作缓存解决方案。

Servlet looks like following: Servlet 如下所示:

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:在 ehcache.xml 我有以下配置:

<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 ):同样在ctx-cache.xml文件中还有以下配置(并且这个文件是在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.但它不起作用,方法getData仍然会被相同的 URL 命中。 I have many @Cacheables working in a project and configured in the same manner, but none is in servlet directly (they're in services).我有很多@Cacheables在一个项目中工作并以相同的方式配置,但没有一个直接在 servlet 中(它们在服务中)。

What am I missing?我错过了什么?

are you using Spring?你在用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."如果是,请考虑 spring 文档spring doc说:“使用代理时,您应该仅将 @Cache* 注释应用于具有公共可见性的方法。如果您使用这些注释对受保护、私有或包可见的方法进行注释,则不会出现错误提出,但注释的方法不显示配置的缓存设置。如果您需要注释非公共方法,因为它更改字节码本身,请考虑使用 AspectJ(见下文)。” So you should change your method visibility to protected to public因此,您应该将方法可见性更改为 protected to public

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM