0

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>

3 Answers 3

1

Are you using Ehcache Spring Annotations (http://groups.google.com/group/ehcache-spring-annotations)? This does not work with Hibernate.

Sign up to request clarification or add additional context in comments.

Comments

0

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?

1 Comment

The getActivities method is in my service layer, ActivitiesService.java. It is called from my controller layer, ActivitiesController.java. It is being called from the controller class, would that even matter?
0

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.

2 Comments

Yes it is defined by an interface. I have to decorate the interface with the ecache attribute?
No, the annotations can be specified on either the interface method or the implementation method and it will be found. The other question is are you calling getActivities from another bean that has the getActivities bean injected in as a dependency? With the way Spring's proxies work self-invocation is not intercepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.