Announcement Announcement Module
Collapse
No announcement yet.
Having problems with @Cacheable. Can someone see what I'm doing wrong? Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Having problems with @Cacheable. Can someone see what I'm doing wrong?

    Hi,

    I've tried setting up a simple test to use @Cacheable, but it doesn't seem to be working.

    I have a test class named CacheTest:

    Code:
    package mypackage;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"classpath:spring-test-context.xml"})
    @Component
    public class CacheTest {
    
    @Test
    public void testRetrieveIncidentCache()
    {
    Worker worker = new Worker();
    String result = worker.cacheTestMethod("hello");
    
    result = worker.cacheTestMethod("hello");
    }
    
    }
    Then, I have the class that I want to cache:

    Code:
    package mypackage;
    public class Worker {
    @Cacheable(value = "testCache")
    public String cacheTestMethod(String name) {
    System.out.println("Method executed");
    
    return "Hello World!";
    }
    }
    In spring-text-context,xml, I have:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd
    ">
    
    <context:component-scan base-package="mypackage" />
    
    <!-- Cache implementation -->
    <cache:annotation-driven/>
    
    <bean id="cacheManager"
    class="org.springframework.cache.ehcache.EhCacheCacheManager"
    p:cacheManager-ref="ehcache"/>
    
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    p:configLocation="file:src/main/resources/ehcache.xml"
    p:shared="true"/>
    
    </beans>
    And, in src/main/resources/ehcache.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <diskStore path="java.io.tmpdir/cachetest" />
    
    <defaultCache
    eternal="true"
    maxElementsInMemory="100"
    overflowToDisk="false" />
    <cache
    name="testCache"
    maxElementsInMemory="100"
    eternal="false"
    overflowToDisk="false"
    diskPersistent="true"
    timeToIdleSeconds="0"
    timeToLiveSeconds="300"
    memoryStoreEvictionPolicy="LRU" />
    </ehcache>
    In my pom.xml, I have:

    Code:
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.1.4.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.1.4.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.1.4.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>3.1.4.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.1.4.RELEASE</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.6.0</version>
    <type>pom</type>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2.2</version>
    </dependency>
    Basically, I see that the cacheManager bean is created and there are no other exceptions in the log. However, when I invoke the cacheable method twice, I see that the body of the method is called both times. So, I'm just trying to figure out what's missing.

    Thanks in advance for any help!

    Regards,
    Eric

  • #2
    Ok, I found the answer. The problem was that I was instantiating the class directly in the code. You need to inject the bean into your method. So, this:

    Code:
    Worker worker = new Worker();
    will bypass the cache.

    Instead, use AutoWired to inject the bean:

    Code:
    package mypackage;
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"classpath:spring-test-context.xml"})
    @Component
    public class CacheTest {
    
        @Autowired
        Worker worker;
    
        @Test
        public void testRetrieveIncidentCache()
        {
    
            String result = worker.cacheTestMethod("hello");
    
            result = worker.cacheTestMethod("hello");
        }
    }
    Now, it will cache the method.

    Comment

    Working...
    X