HIbernate 的 ehCache 和 Spring MVC 错误
我试图在服务层方法上进行缓存,但它仍然进入其中并调用数据库。是我的设置错误吗?
@Cacheable(cacheName="apiActivitiesCache", keyGenerator = @KeyGenerator (
name = "ListCacheKeyGenerator",
properties = {
@Property( name="useReflection", value="true" ),
@Property( name="checkforCycles", value="true" ),
@Property( name="includeMethod", value="false" )
}
)
)
public GetMemberActivitiesResponse getActivities(GetMemberActivitiesRequest request) {
servlet-context.xml
<ehcache:annotation-driven cache-manager="ehCacheManager" create-missing-caches="true"/>
<ehcache:config cache-manager="ehCacheManager">
<ehcache:evict-expired-elements interval="60" />
</ehcache:config>
<beans:bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<beans:property name="configLocation" value="/WEB-INF/spring/ehcache.xml"/>
</beans:bean>
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
<cache name="apiActivitiesCache" eternal="false"
maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
I am trying to get caching working on a Service layer method, but it still goes into it and calls the database. Is my setup wrong?
@Cacheable(cacheName="apiActivitiesCache", keyGenerator = @KeyGenerator (
name = "ListCacheKeyGenerator",
properties = {
@Property( name="useReflection", value="true" ),
@Property( name="checkforCycles", value="true" ),
@Property( name="includeMethod", value="false" )
}
)
)
public GetMemberActivitiesResponse getActivities(GetMemberActivitiesRequest request) {
servlet-context.xml
<ehcache:annotation-driven cache-manager="ehCacheManager" create-missing-caches="true"/>
<ehcache:config cache-manager="ehCacheManager">
<ehcache:evict-expired-elements interval="60" />
</ehcache:config>
<beans:bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<beans:property name="configLocation" value="/WEB-INF/spring/ehcache.xml"/>
</beans:bean>
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" />
<cache name="apiActivitiesCache" eternal="false"
maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否使用 Ehcache Spring Annotations (http://groups.google.com/group/ehcache-spring-annotations)?
这不适用于 Hibernate。
Are you using Ehcache Spring Annotations (http://groups.google.com/group/ehcache-spring-annotations)?
This does not work with Hibernate.
我用你的确切配置尝试了这个,它起作用了。您的服务类是否将 getActivities 方法配置为 Spring bean?
另外, getActivities 方法是否被另一个类调用,或者是否被同一个类中的另一个方法调用?
I tried this with your exact configuration and it worked. Is your service class that has the getActivities method configured as a Spring bean?
Also, is the getActivities method being called by another class, or is it being called by another method in the same class?
getActivities 是由接口定义的吗? Spring 使用基于接口的 Java 代理作为注释包装器,因此所有带注释的方法都必须由接口定义。
Is getActivities defined by an interface? Spring uses interface based Java proxies for annotation wrappers so all annotated methods must be defined by an interface.